pythonmysqldbwindows下安装教程及问题解决方法

使用python访问mysql,需要一系列安装

linux下mysqldb安装见
python mysqldb在linux下的快速安装
http://www.jb51.net/article/65743.htm

————————————————————-
以下是windows环境下的:

1. 安装数据库mysql
下载地址:http://www.mysql.com/downloads/
可以顺带装个图形工具,我用的是mysql-front

2. 安装mysqldb

好了,到了这一步,你有两个选择
a. 安装已编译好的版本(一分钟)
b. 从官网下,自己编译安装(介个…..半小时到半天不等,取决于你的系统环境以及rp)

若是系统32位的,有c++编译环境的,自认为rp不错的,可以选择自己编译安装,当然,遇到问题还是难免的,一步步搞还是能搞出来的
若是系统64位的,啥都木有的,建议下编译版本的,甭折腾

2.1安装已编译版本:

Downloads


根据自己系统下载,双击安装,搞定
然后import mysqldb,查看是否成功

我的,win7,64位,2.7版本

mysql-python-1.2.3.win-amd64-py2.7.exe

2.2自己编译安装
话说搞现成的和自己编译差距不一一点半点的,特别是64位win7,搞死了

2.2.1安装setuptools

在安装mysqldb之前必须安装setuptools,要不然会出现编译错误
http://pypi.python.org/pypi/setuptools
http://peak.telecommunity.com/dist/ez_setup.py 使用这个安装(64位系统必须用这个)

2.2.2安装mysqldb

下载mysqldb
http://sourceforge.net/projects/mysql-python/

解压后,cmd进入对应文件夹
如果32位系统且有gcc编译环境,直接

代码如下:

python setup.py build

2.2.3问题汇总
a. 64位系统,无法读取注册表的问题
异常信息如下:

代码如下:

f:\devtools\mysql-python-1.2.3>pythonsetup.py build
traceback (most recent call last):
file “setup.py”, line 15, in
metadata, options = get_config()
file “f:\devtools\mysql-python-1.2.3\setup_windows.py”, line7, in get_config
serverkey = _winreg.openkey(_winreg.hkey_local_machine, options[‘ registry_ke
y’] )
windowserror: [error 2] the system cannotfind the file specified

解决方法:
其实分析代码,发现只是寻找mysql的安装地址而已 修改setup_windows.py如下
注解两行,加入一行,为第一步mysql的安装位置

代码如下:

#serverkey = _winreg.openkey(_winreg.hkey_local_machine,options[‘registry_key’] )
#mysql_root, dummy = _winreg.queryvalueex(serverkey,’location’)
mysql_root = r”f:\devtools\mysql\mysql server 5.5″

b.没有gcc编译环境

代码如下:

unable to find vcvarsall.bat

解决方法:安装编译环境(一个老外的帖子)
1) first ofall download mingw. youneed g++compiler and mingw make in setup.
2) if youinstalled mingw for example to “c:\mingw” then add “c:\mingw\bin”to your path in windows.(安装路径加入环境变量)
3) now startyour command prompt and go the directory where you have your setup.py residing.
4) last andmost important step:
setup.py install build –compiler=mingw32
或者在setup.cfg中加入:

代码如下:

[build]
compiler = mingw32

c.gcc: /zl: no suchfile or directory错误
异常信息如下
f:\devtools\mingw\bin\gcc.exe -mno-cygwin-mdll -o -wall -dversion_info=(1,2,3,’
final’,0) -d__version__=1.2.3″-if:\devtools\mysql\mysql server 5.5\include” -ic
:\python27\include -ic:\python27\pc -c_mysql.c -o build\temp.win-amd64-2.7\rele
ase\_mysql.o /zl
gcc: error: /zl: no such file or directory
error: command ‘gcc’ failed with exitstatus 1

参数是vc特有的编译参数,如果使用mingw的话因为是gcc所以不支持。可以在setup_windows.py中去掉
/zl

解决方法:
修改setup_windows.py 改为空的

代码如下:

#extra_compile_args = [ ‘/zl’ ]
extra_compile_args = [ ” ]

目前就遇到这几个问题,望补充

3. 增删改查代码示例及结果(just for test)

代码如下:

create table `user` (
`id` int(11) not null auto_increment,
`name` varchar(255) default null,
`age` varchar(255) default null,
primary key (`id`)
) engine=innodb auto_increment=7 default charset=utf8;

代码如下:

#-*- coding:utf-8 -*-
#dbtest.py
#just used for a mysql test
””’
created on 2012-2-12

@author: ken
”’
#mysqldb
import time, mysqldb, sys

#connect
conn=mysqldb.connect(host=”localhost”,user=”root”,passwd=”test_pwd”,db=”school”,charset=”utf8″)
cursor = conn.cursor()

#add
sql = “insert into user(name,age) values(%s,%s)”
param = (“tom”,str(20))
n = cursor.execute(sql,param)
print n

#更新
sql = “update user set name=%s where
param = (“ken”)
n = cursor.execute(sql,param)
print n

#查询
n = cursor.execute(“select * from user”)
for row in cursor.fetchall():
for r in row:
print r,
print “”

#删除
sql = “delete from user where name=%s”
param =(“ted”)
n = cursor.execute(sql,param)
print n
cursor.close()

#关闭
conn.close()

Posted in 未分类

发表评论