python操作mysql

MySQL一直是我最爱的数据库,用Java编程时经常对MySQL进行操作。最近在学习Python,也想用Python操作一下MySQL数据库。

首先当然是安装Python和MySQL,本人是python2.7,然后是安装MySQLdb,它就相当 于用Java连接数据库时的那个驱动一样,如果是ubuntu可直接使用命令

 

sudo apt-get install python-mysqldb


即可安装

 

如果是windows则可进去http://www.codegood.com/downloads下载相应版本的MySQLdb,下载完成后直接安装即可。

 

创建一个数据库:

 

Python代码   收藏代码
  1. #coding=utf-8  
  2.   
  3. import MySQLdb  
  4.   
  5. #建立和数据库系统的连接  
  6. conn = MySQLdb.connect(host='localhost', user='root', passwd='root', db='test')  
  7.   
  8. #获取操作游标  
  9. cursor = conn.cursor()  
  10. #执行SQL,创建一个数据库  
  11. cursor.execute("""create database python;""")  
  12.   
  13. #关闭连接,释放资源  
  14. cursor.close()  

 

 

插入,修改数据:

 

 

Python代码   收藏代码
  1. # -*- coding: utf-8 -*-  
  2.   
  3. import MySQLdb  
  4.   
  5. conn = MySQLdb.connect(host='localhost', user='root', passwd='root')  
  6.   
  7. cursor = conn.cursor()  
  8.   
  9. #cursor.execute("""create database if not exists python""")  
  10.   
  11. #选择数据库  
  12. conn.select_db('python')  
  13.   
  14. #执行SQL,创建一个数据表  
  15. cursor.execute("""create table test(id int, info varchar(100)) """)  
  16.   
  17. value = [1,"我是中文,会不会乱码呢?"]  
  18.   
  19. #插入一条记录  
  20. cursor.execute("insert into test values(%s, %s)", value)  
  21.   
  22. values = []  
  23.   
  24. #生成插入参数值  
  25. for i in range(20):  
  26.     values.append((i, 'Hello mysqldb, I am recoder ' + str(i)))  
  27.   
  28. #插入多条记录  
  29. cursor.executemany("""insert into test values(%s, %s) """, values)  
  30.   
  31. #修改记录  
  32. cursor.execute("""update test set info='asdl;kfjalfkj' where id=3;""")  
  33. cursor.close()  

 

 

上面的代码不知道为什么在windows下插入不了,但却不报错。但Linux下却非常正常。

 

 

MySQL的乱码在我以前用Java时就非常头疼,不过解决办法还是很简单的:

 

1、windows下:修改my.ini文件,有两处default-character-set=latin1都改为default-character-set=utf8重启既可。

2、linux下:vim /etc/my.cnf(若没有可查看/etc/mysql/my.cnf)在[mysqld]下加入 default-character-set = utf8,[client]下加入default-character-set = utf8

:wq保存退出

 

然尔这次用python也出现令人头疼的乱码码问题:数据库中是UTF-8编码,用上面的代码插入数据后在数据库中查看时,中文会乱码,但再用python查询时中文却会正确显示。

 

 

查询:

 

 

Python代码   收藏代码
  1. # -*- coding: utf-8 -*-  
  2. ''''' 
  3. Created on 2011-9-23 
  4.  
  5. @author: HuangHua 
  6. '''  
  7. import MySQLdb  
  8.   
  9. conn = MySQLdb.Connect(host='localhost', user='root', passwd='root', db='python')  
  10.   
  11. cursor = conn.cursor()  
  12.   
  13. count = cursor.execute('select * from test')  
  14.   
  15. print '总共有%s条记录',count  
  16.   
  17. #获取一条记录,每条记录做为一个无组返回   
  18. result = cursor.fetchone();  
  19. print result  
  20. print 'ID: %s info: %s' % result  
  21.   
  22. #获取多条记录,注意由于之前执行了fetchone(),所以游标已经指到第二条记录了 ,也就是从第二条开始的所有记录  
  23. results = cursor.fetchmany(2)  
  24. for r in results:  
  25.     print r  
  26.   
  27. print '获取 所有结果 :'  
  28. #重置游标位置,0为偏移量,mode=absolute | relative,默认为relative  
  29. cursor.scroll(0, mode='absolute')  
  30. #获取所有结果  
  31. results = cursor.fetchall()  
  32. for r in results:  
  33.     print r[1]  
  34.   
  35. cursor.close()  

 

查询后中文会正确显示,但在数据库中却是乱码的。经过我从网上查找,发现用一个属性有可搞定:


Python代码   收藏代码
  1. conn = MySQLdb.Connect(host='localhost', user='root', passwd='root', db='python')  

 

中加一个属性:

 

Python代码   收藏代码
  1. conn = MySQLdb.Connect(host='localhost', user='root', passwd='root', db='python',charset='utf8') 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值