`
这些年
  • 浏览: 389062 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

python 操作mysql (转)

 
阅读更多

首先,安装需要的环境,Mysql和Python就不说了,必备的东西。

主要是安装的MySQLdb,可以去sf.net下载,具体地址是http://sourceforge.net/projects/mysql-python/

如果用Ubuntu,直接

 

sudo apt-get install python-mysqldb

 

安装完成之后可以在Python解释器中测试一下

输入

Python代码   收藏代码
  1. import MySQLdb #注意大小写!!  

 如果不报错,就证明安装成功了,可能继续了


MySQLdb在Python中也就相当于JAVA中的MySQL的JDBC Driver,Python也有类似的数据接口规范Python DB API,MySQLdb就是Mysql的实现。操作也比较简单和其它平台或语言操作数据库一样,就是建立和数据库系统的连接,然后给数据库输入SQL,再从数据库获取结果。

先写一个最简单的,创建一个数据库:

 

Python代码   收藏代码
  1. #!/usr/bin/env python  
  2. #coding=utf-8  
  3. ###################################  
  4. # @author migle  
  5. # @date 2010-01-17  
  6. ##################################  
  7. #MySQLdb 示例  
  8. #  
  9. ##################################  
  10. import MySQLdb  
  11.   
  12. #建立和数据库系统的连接  
  13. conn = MySQLdb.connect(host='localhost', user='root',passwd='longforfreedom')  
  14.   
  15. #获取操作游标  
  16. cursor = conn.cursor()  
  17. #执行SQL,创建一个数据库.  
  18. cursor.execute("""create database python """)  
  19.   
  20. #关闭连接,释放资源  
  21. cursor.close();  

 

 

创建数据库,创建表,插入数据,插入多条数据

 

Python代码   收藏代码
  1. #!/usr/bin/env python  
  2. #coding=utf-8  
  3. ###################################  
  4. # @author migle  
  5. # @date 2010-01-17  
  6. ##################################  
  7. #MySQLdb 示例  
  8. #  
  9. ##################################  
  10. import MySQLdb  
  11.   
  12. #建立和数据库系统的连接  
  13. conn = MySQLdb.connect(host='localhost', user='root',passwd='longforfreedom')  
  14.   
  15. #获取操作游标  
  16. cursor = conn.cursor()  
  17. #执行SQL,创建一个数据库.  
  18. cursor.execute("""create database if not exists python""")  
  19.   
  20. #选择数据库  
  21. conn.select_db('python');  
  22. #执行SQL,创建一个数据表.  
  23. cursor.execute("""create table test(id int, info varchar(100)) """)  
  24.   
  25. value = [1,"inserted ?"];  
  26.   
  27. #插入一条记录  
  28. cursor.execute("insert into test values(%s,%s)",value);  
  29.   
  30. values=[]  
  31.   
  32.   
  33. #生成插入参数值  
  34. for i in range(20):  
  35.     values.append((i,'Hello mysqldb, I am recoder ' + str(i)))  
  36. #插入多条记录  
  37.   
  38. cursor.executemany("""insert into test values(%s,%s) """,values);  
  39.   
  40. #关闭连接,释放资源  
  41. cursor.close();  

 

 

查询和插入的流程差不多,只是多了一个得到查询结果的步骤

 

Python代码   收藏代码
  1. #!/usr/bin/env python  
  2. #coding=utf-8  
  3. ######################################  
  4. #  
  5. # @author migle  
  6. # @date 2010-01-17  
  7. #  
  8. ######################################  
  9. #  
  10. # MySQLdb 查询  
  11. #  
  12. #######################################  
  13.   
  14. import MySQLdb  
  15.   
  16. conn = MySQLdb.connect(host='localhost', user='root', passwd='longforfreedom',db='python')  
  17.   
  18. cursor = conn.cursor()  
  19.   
  20. count = cursor.execute('select * from test')  
  21.   
  22. print '总共有 %s 条记录',count  
  23.   
  24. #获取一条记录,每条记录做为一个元组返回  
  25. print "只获取一条记录:"  
  26. result = cursor.fetchone();  
  27. print result  
  28. #print 'ID: %s   info: %s' % (result[0],result[1])  
  29. print 'ID: %s   info: %s' % result   
  30.   
  31. #获取5条记录,注意由于之前执行有了fetchone(),所以游标已经指到第二条记录了,也就是从第二条开始的所有记录  
  32. print "只获取5条记录:"  
  33. results = cursor.fetchmany(5)  
  34. for r in results:  
  35.     print r  
  36.   
  37. print "获取所有结果:"  
  38. #重置游标位置,0,为偏移量,mode=absolute | relative,默认为relative,  
  39. cursor.scroll(0,mode='absolute')  
  40. #获取所有结果  
  41. results = cursor.fetchall()  
  42. for r in results:  
  43.     print r  
  44. conn.close()  
事务:
在py的mysqldb中,一旦创建了一个cursor,那么就默认创建了一个事务。直到提交事务为止,数据库的数据才会进行更改。但是对于不支持事务的数据库来说,事务不起作用。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics