当前位置:Gxlcms > 数据库问题 > python在windows下连接mysql数据库

python在windows下连接mysql数据库

时间:2021-07-01 10:21:17 帮助过:29人阅读

  python 连接mysql数据库需要 Python interface to Mysql包,包名为

MySQL-python

,PyPI上现在到了1.2.5版本。MySQL-python在windows下是通过.exe文件的installer安装的,

前提是已经安装的python需要写入注册表,参考这篇文章:windows安装python2.7后的注册(registry)问题。

  然后开始安装MySQL-python,不过这里有个坑,从PyPI上下载的MySQL-python版本--

MySQL-python-1.2.5.win32-py2.7.exe (md5)--不一定能用,原因是python环境和MySQL-python在软件位数上可能不一致,

比如,我本地安装的python是64位,从PyPI上下载的MySQL-python就没法使用,

技术分享

会报这样的错误: python ImportError: DLL load failed: %1

只要安装正确位数的MySQL-python就可以了,这里分享下64位的:

MySQL-python-1.2.5.win-amd64-py2.7.exe

 

二,简单使用

参考代码如下:

 1 Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
 2 import MySQLdb
 3 conn = MySQLdb.connect(host=localhost,port=3306,user=root,passwd=123456,db=test)
 4 cur = conn.cursor()
 5 cur.execute(select `title`, `text` from `entries` limit 10)
 6 2L
 7 cur.fetchall()
 8 ((bokeyuan, bokeyuan text...), (google translate, google translate text...))
 9 cur.close()
10 conn.close()

 

python在windows下连接mysql数据库

标签:google   文件   mysqldb   版本   font   get   translate   style   不一致   

人气教程排行