python 操作mysql数据库
1 代码
#!/usr/bin/python import mysql.connector import sys, os user = 'root' pwd='root' host='localhost' db='amydb' select_sql = 'select * from book' cnx = mysql.connector.connect(user = user, password = pwd, host = host, database = db) cursor = cnx.cursor() try: cursor.execute(select_sql) for(name) in cursor: print(name) except mysql.connector.Error as err: print("error:" + err.msg) sys.exit() cnx.commit() cursor.close() cnx.close()
2 结果
('20140418101955036908', '数学', '张三', '0', datetime.datetime(1, 1, 1, 0, 0), 0, 0, datetime.datetime(2014, 4, 18, 10, 19, 56)) ('20140418102116049939', '算法设计与分析', '未知', '0', datetime.datetime(1, 1, 1, 0, 0), 0, 0, datetime.datetime(2014, 4, 18, 10, 21, 30))
3 环境
mysql
python3.3
mysql.connector 地址:http://pan.baidu.com/s/1i31JiBz
作者:BestNow
出处:http://www.cnblogs.com/BestNow/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/BestNow/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。