Python真是个装b的语言,居然连mysql这么流行的数据库,都不提供官方支持
有第三方库,MySQLdb, 但这个库的帮助里面,只支持到mysql 5.1
操蛋的是,MySQLdb 库安装的时候,居然需要先安装一个mysql server
更操蛋的是,如果你安装的是高版本mysql,比如现在的5.5,居然不能直接安装,需要修改一堆配置
幸好,互联网上有其他的好心人,做了一个自动安装并且无需预先安装mysql server的版本
到这里下载吧:http://www.codegood.com/archives/129
sample code:
import MySQLdb, MySQLdb.cursors
def test():
conn = MySQLdb.connect(host='localhost', user='root', passwd='', db='test', cursorclass=MySQLdb.cursors.DictCursor)
cursor = conn.cursor()
cursor.execute('SELECT * from test')
row = cursor.fetchone()
print row['field1']
# print cursor.description
cursor.close()
conn.close()
if __name__=='main':
test()
--------------------------------
还有第二个容易的选择:pymysql, http://code.google.com/p/pymysql/
pymysql库和MySQLdb的区别是: pymysql是纯python的,而MySQLdb是c写的
所以pymysql安装、使用起来相对容易一点
但pymysql,速度要慢一点
--------------------------------
另外一个可能的选择,pyodbc, http://code.google.com/p/pyodbc/wiki/FAQs
--------------------------------
参考文档
- DB-API FAQ: http://wiki.python.org/moin/DbApiFaq, 上面说的3个库,都是基于Python的 DB-API 的
- Python 官方MySQL库指南:http://wiki.python.org/moin/MySQL
|