首页 > 代码库 > python在windows下连接mysql数据库
python在windows下连接mysql数据库
一,安装MySQL-python
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数据库
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。