设为首页 收藏本站
查看: 1074|回复: 0

[经验分享] Python学习系列(七)( 数据库编程)

[复制链接]

尚未签到

发表于 2015-4-23 05:35:07 | 显示全部楼层 |阅读模式
Python学习系列(七)( 数据库编程)

       Python学习系列(六)(模块)

一,MySQL-Python插件

      Python里操作MySQL数据库,需要Python下安装访问MySQL数据库接口API包即插件,从而使得Python2.7能访问操作MySQL数据库。MySQL软件可以去官网下载:http://www.mysql.com/; MySQLdb插件下载:http://sourceforge.net/projects/mysql-python/files/latest/download

二,访问MySQL数据库

    1,连接数据库mysql

         基本格式:connect ([host=]'ip',[user=]'user',[passwd=]'password',[db=]'dbname')

2,数据库的基本操作

     1)create创建表



DSC0000.gif DSC0001.gif


1 import MySQLdb
2 #connect to a database 'test'
3 conn=MySQLdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
4 cursor=conn.cursor()
5 #create a table
6 cursor.execute('create table \
7 test(ID int primary key auto_increment,Name char(25))')
8 #Closing database
9 cursor.close()
10 conn.close()
View Code

     2)fetchall访问:






1 import MySQLdb
2 #connect to a database 'test'
3 conn=MySQLdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
4 cursor=conn.cursor()
5 #fetch datas
6 n=cursor.execute('select * from test;')
7 r=cursor.fetchall()
8 print n,r
9 #Closing database
10 cursor.close()
11 conn.close()
12
13 >>> ================================ RESTART ================================
14 >>>
15 3 ((4L, 'zhangbc'), (5L, 'lis08'), (6L, 'wangw'))
16 >>>
View Code



在Mysql5.6环境下运行:

DSC0002.jpg

3)insert向表中插入数据:






1 import MySQLdb
2 #connect to a database 'test'
3 conn=MySQLdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
4 cursor=conn.cursor()
5 #insert data into table 'test'
6 mysql='''insert into test(id,sname) values(4,'zhanghua')'''
7 cursor.execute(mysql)
8 conn.commit()#below mysql5.0 needed
9 #fetch datas
10 n=cursor.execute('select * from test;')
11 r=cursor.fetchall()
12 print n,r
13 #Closing database
14 cursor.close()
15 conn.close()
16
17 >>> ================================ RESTART ================================
18 >>>
19 4 ((1L, 'zhangbc'), (2L, 'lis'), (3L, 'wangw'), (4L, 'zhanghua'))
View Code

注意:一定要写上conn.commit();事物不提交,将回滚。比较:

DSC0003.jpg



DSC0004.jpg

4)update修改表中数据:






1 import MySQLdb
2 #connect to a database 'test'
3 conn=MySQLdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
4 cursor=conn.cursor()
5 #update data of the table 'test'
6 mysql='''update test set sname='Lisi08' where id=2'''
7 cursor.execute(mysql)
8 conn.commit()#below mysql5.0 needed
9 #fetch datas
10 n=cursor.execute('select * from test;')
11 r=cursor.fetchall()
12 print n,r
13 #Closing database
14 cursor.close()
15 conn.close()
16
17 >>> ================================ RESTART ================================
18 >>>
19 4 ((1L, 'zhangbc'), (2L, 'Lisi08'), (3L, 'wangw'), (4L, 'zhanghua'))
View Code

5)delete删除表中数据:






1 import MySQLdb
2 #connect to a database 'test'
3 conn=MySQLdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
4 cursor=conn.cursor()
5 #delete data of the table 'test'
6 mysql='''delete from test where id=4'''
7 cursor.execute(mysql)
8 conn.commit()#below mysql5.0 needed
9 #fetch datas
10 n=cursor.execute('select * from test;')
11 r=cursor.fetchall()
12 print n,r
13 #Closing database
14 cursor.close()
15 conn.close()
16
17 >>> ================================ RESTART ================================
18 >>>
19 3 ((1L, 'zhangbc'), (2L, 'Lisi08'), (3L, 'wangw'))
View Code

     6)关于select及其遍历:

      i)使用元组tuple与fetchone结合






1 import MySQLdb
2 #connect to a database 'test'
3 conn=MySQLdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
4 cursor=conn.cursor()
5 #fetch datas
6 cursor.execute('select * from test;')
7 #获得结果集的记录
8 numrows=int(cursor.rowcount)
9 #循环,取行数据
10 for i in range(numrows):
11     row=cursor.fetchone()
12     print row[0],row[1]
13 #Closing database
14 cursor.close()
15 conn.close()
16
17 >>> ================================ RESTART ================================
18 >>>
19 4 zhangbc
20 5 lis08
21 6 wangw
View Code     ii)使用字典cursor





1 #-*- coding:UTF-8 -*-
2 import MySQLdb as mdb
3 #connect to a database 'test'
4 conn=mdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
5 with conn:
6     #获取连接上的字典cursor,每一个cursor其实都是cursor的子类
7     cur=conn.cursor(mdb.cursors.DictCursor)
8 #fetch datas
9 cur.execute('select * from test;')
10 #获得结果集
11 rows=cur.fetchall()
12 #循环,取行数据
13 for row in rows:
14     print '%s %s'%(row['ID'],row['Name'])
15 #Closing database
16 cur.close()
17 conn.close()
18
19 >>> ================================ RESTART ================================
20 >>>
21 4 zhangbc
22 5 lis08
23 6 wangw
View Code     iii)获取单个表的字段名及其信息





1 #-*- coding:UTF-8 -*-
2 import MySQLdb as mdb
3 #connect to a database 'test'
4 conn=mdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
5 with conn:
6     #获取连接上的字典cursor,每一个cursor其实都是cursor的子类
7     cur=conn.cursor()
8 #fetch datas
9 cur.execute('select * from test;')
10 #获得结果集
11 rows=cur.fetchall()
12 #获得链接对象的描述信息
13 desc=cur.description
14 print 'cur.description:',desc
15 #打印表头
16 print '%2s %3s'%(desc[0][0],desc[1][0])
17 #循环,取行数据
18 for row in rows:
19     print '%2s %3s'%row
20 #Closing database
21 cur.close()
22 conn.close()
23
24 >>> ================================ RESTART ================================
25 >>>
26 cur.description: (('ID', 3, 1, 11, 11, 0, 0), ('Name', 254, 7, 25, 25, 0, 1))
27 ID Name
28  4 zhangbc
29  5 lis08
30  6 wangw
View Code

三,小结

     本文主要介绍了Python下如何访问并操数据库的基本知识,如:如何连接数据库,如何执行执行SQL语句等等。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-59679-1-1.html 上篇帖子: 第一次用python 写的简单爬虫 记录在自己的博客 下篇帖子: python网页抓取之英汉字典
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表