返回python连......登陆

python连接mysql实例分享

巴扎黑2017-01-09 16:53:14313

本文给大家汇总介绍了使用python连接mysql的几个实例,非常的简单实用

示例一

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#coding=UTF-8 

import sys

import MySQLdb

import time 

reload(sys)

sys.setdefaultencoding('utf-8'

def connectDemo():

  return MySQLdb.Connection("127.0.0.1","root","root","demo",3306,charset="utf8")  

if __name__ == '__main__':

  begin=time.time() 

  conn=connectDemo()

  cursor = conn.cursor()

  sql="""

    show tables

    """

  count = cursor.execute(sql)

  rows = cursor.fetchall()

  cursor.close()

  conn.close()

  print "========demo库共:%s 张表============" % (count) 

  print '耗时:%s 秒' % (time.time()-begin)

示例二

1

2

3

4

5

6

7

8

9

10

11

12

import MySQLdb

conn = MySQLdb.connect(host="localhost",

user="root",

passwd="123456",

db="test")

cursor = conn.cursor()

cursor.execute("SELECT * from hard")

res = cursor.fetchall()

for in res:

print x

cursor.close()

conn.close()

示例三

1 安装Python的Mysql包

1

2

3

4

5

6

7

root@10.1.1.45:~# apt-get install python-mysqldb

root@10.1.1.45:~# python

Python 2.5.2 (r252:60911, Jan 4 200921:59:32

[GCC 4.3.2] on linux2

Type "help""copyright""credits" or "license" for more information.

>>> import MySQLdb

>>>

这里导入MySQLdb没有报错,就说明安装成功.

2 下面就可以连接数据库,可以进行增删改操作.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

root@10.1.1.45:python# cat create.py 

#!/usr/bin/env python

#coding=utf-8

   

#导入相关模块

import MySQLdb

   

#建立和mysql数据库的连接

conn = MySQLdb.connect(host='localhost',user='root',passwd='davehe')

#获取游标

curs = conn.cursor()

#执行SQL,创建一个数据库

curs.execute("create database pythondb")

#选择连接哪个数据库

conn.SELECT_db('pythondb')

#执行SQL,创建一个表

curs.execute("create table test(id int,message varchar(50))")

#插入一条记录

value = [1,"davehe"]

curs.execute("INSERT into test values(%s,%s)",value)

#插入多条记录

values = []

for in range(20):

  values.append((i,'hello mysqldb' + str(i)))

curs.executemany("INSERT into test values(%s,%s)",values)

#提交修改                

conn.commit()

#关闭游标连接,释放资源

curs.close()

#关闭连接

conn.close()

root@10.1.1.45:python# ./create.py

3 下面利用python查看mysql里刚添加的记录.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

root@10.1.1.45:python# cat SELECT.py 

#!/usr/bin/env python

#coding=utf-8

   

#导入相关模块

import MySQLdb

   

#建立和mysql数据库的连接

conn = MySQLdb.connect(host='localhost',user='root',passwd='hc1226')

#获取游标

curs = conn.cursor()

#选择连接哪个数据库

conn.SELECT_db('pythondb')

#查看共有多少条记录

count = curs.execute('SELECT * from test')

print "一共有%s条记录" % count

#获取一条记录,以一个元组返回

result = curs.fetchone()

print "当前的一条记录 ID:%s message:%s" % result

#获取后10条记录,由于之前执行了getchone(),所以游标已经指到第二条记录,下面也就从第二条记录开始返回

results = curs.fetchmany(10)

for in results:

  print r

#重置游标位置,0,为偏移量,mode = relative(默认)

curs.scroll(0,mode='absolute')

#获取所有记录

results = curs.fetchall()

for in results:

  print r

   

#提交修改

conn.commit()

#关闭游标连接,释放资源

curs.close()

#关闭连接

conn.close()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

root@10.1.1.45:python# ./SELECT.py 

一共有21条记录

当前的一条记录 ID:1 message:davehe

(0L'hello mysqldb0')

(1L'hello mysqldb1')

(2L'hello mysqldb2')

(3L'hello mysqldb3')

(4L'hello mysqldb4')

(5L'hello mysqldb5')

(6L'hello mysqldb6')

(7L'hello mysqldb7')

(8L'hello mysqldb8')

(9L'hello mysqldb9')

(1L'davehe')

(0L'hello mysqldb0')

(1L'hello mysqldb1')

(2L'hello mysqldb2')

(3L'hello mysqldb3')

(4L'hello mysqldb4')

(5L'hello mysqldb5')

(6L'hello mysqldb6')

(7L'hello mysqldb7')

(8L'hello mysqldb8')

(9L'hello mysqldb9')

(10L'hello mysqldb10')

(11L'hello mysqldb11')

(12L'hello mysqldb12')

(13L'hello mysqldb13')

(14L'hello mysqldb14')

(15L'hello mysqldb15')

(16L'hello mysqldb16')

(17L'hello mysqldb17')

(18L'hello mysqldb18')

(19L'hello mysqldb19')

更多关于python连接mysql实例分享请关注PHP中文网(www.php.cn)其他文章!

最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送