Python的MySQL库

来源:互联网 发布:python策略模型 编辑:程序博客网 时间:2024/05/22 00:46

Python的MySQL库

http://www.cnblogs.com/wutaotao/archive/2011/05/31/2064963.html

Python真是个装b的语言,居然连mysql这么流行的数据库,都不提供官方支持

有第三方库,MySQLdb, 但这个库的帮助里面,只支持到mysql 5.1

操蛋的是,MySQLdb 库安装的时候,居然需要先安装一个mysql server

更操蛋的是,如果你安装的是高版本mysql,比如现在的5.5,居然不能直接安装,需要修改一堆配置

幸好,互联网上有其他的好心人,做了一个自动安装并且无需预先安装mysql server的版本

到这里下载吧:http://www.codegood.com/archives/129

 

MySQL-python 1.2.3 for Windows and Python 2.7, 32bit and 64bit versions

Last Updated on Sunday, 19 September 2010 04:58Written by Ioannis LalopoulosSunday, 19 September 2010 02:27

I have added to the downloads page two distributions of the mysql-python module 1.2.3 for python 2.7

The distributions are:

  MySQL-python-1.2.3.win32-py2.7.exe (1,023.1 KiB)


  MySQL-python-1.2.3.win-amd64-py2.7.exe (1.0 MiB)

Some quick notes:

  1. The choice of 32bit and 64bit depends on the version of python you have installed in your computer and not in the operating system or the server you want to access. So if you have the 32 bit python 2.7 installed on your 64 bit Windows, you will download and install the 32 bit version.
  2. Some python distributions do not make correct registry entries when they install the 64bit python. If this is the case the 64 bit mysql-python installer will not find your python installation. This means that the 64bit python version has been wrongly set in the registry under the Wow6432Node which is under convention for 32bit software only. You will have to add/move the python registry node in the correct place i.e. in HKEY_LOCAL_MACHINE\SOFTWARE  for more info see my post in sourceforge http://bit.ly/c8J6Yr . If your python is from the official distribution (http://www.python.org)  you will not have this problem.

Following are the results  of the test suite that comes with mysql-python on the newly build distributions (I had to edit the tests in three places to replace deprecated test methods in 2.7  that were giving, for that reason, warnings and subsequently two failures in the test_MySQLdb_capabilities.py)


sample code:

view sourceprint?
01import MySQLdb, MySQLdb.cursors
02 
03def test():
04    conn=MySQLdb.connect(host='localhost', user='root', passwd='', db='test', cursorclass=MySQLdb.cursors.DictCursor)
05    cursor=conn.cursor()
06    cursor.execute('SELECT * from test')
07    row=cursor.fetchone()
08    printrow['field1']
09    # print cursor.description
10    cursor.close()
11    conn.close()
12 
13if __name__=='main':
14    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


原创粉丝点击