python MySQLdb Windows下安装教程及问题解决方法


Posted in Python onMay 09, 2015

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

linux下MySQLdb安装见 
Python MySQLdb在Linux下的快速安装
https://3water.com/article/65743.htm

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

1. 安装数据库mysql
下载地址:http://www.mysql.com/downloads/
可以顺带装个图形工具,我用的是MySQL-Front
 
2. 安装MySQLdb
 
好了,到了这一步,你有两个选择
A. 安装已编译好的版本(一分钟)
B. 从官网下,自己编译安装(介个…..半小时到半天不等,取决于你的系统环境以及RP)
 
若是系统32位的,有c++编译环境的,自认为RP不错的,可以选择自己编译安装,当然,遇到问题还是难免的,一步步搞还是能搞出来的
若是系统64位的,啥都木有的,建议下编译版本的,甭折腾
 
2.1安装已编译版本:
http://www.codegood.com/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 <module>

   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 Id=9001"     

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() 
Python 相关文章推荐
Django框架中render_to_response()函数的使用方法
Jul 16 Python
Python只用40行代码编写的计算器实例
May 10 Python
使用pandas读取csv文件的指定列方法
Apr 21 Python
python抓取网站的图片并下载到本地的方法
May 22 Python
Django框架自定义session处理操作示例
May 27 Python
详解python 利用echarts画地图(热力图)(世界地图,省市地图,区县地图)
Aug 06 Python
pytorch获取vgg16-feature层输出的例子
Aug 20 Python
python wav模块获取采样率 采样点声道量化位数(实例代码)
Jan 22 Python
pytorch 中的重要模块化接口nn.Module的使用
Apr 02 Python
Python telnet登陆功能实现代码
Apr 16 Python
使用python创建生成动态链接库dll的方法
May 09 Python
使用PyCharm官方中文语言包汉化PyCharm
Nov 18 Python
Python MySQLdb Linux下安装笔记
May 09 #Python
python实现类的静态变量用法实例
May 08 #Python
python使用pil生成图片验证码的方法
May 08 #Python
python实现连接mongodb的方法
May 08 #Python
python写入xml文件的方法
May 08 #Python
python从sqlite读取并显示数据的方法
May 08 #Python
python创建一个最简单http webserver服务器的方法
May 08 #Python
You might like
关于Zend Studio 配色方案插件的介绍
2013/06/24 PHP
PHP设计模式之适配器模式原理与用法分析
2018/04/25 PHP
PHP之多条件混合筛选功能的实现方法
2019/10/09 PHP
Document:getElementsByName()使用方法及示例
2013/10/28 Javascript
js控制网页前进和后退的方法
2015/06/08 Javascript
JavaScript的函数式编程基础指南
2016/03/19 Javascript
Bootstrap中的fileinput 多图片上传及编辑功能
2016/09/05 Javascript
Node.js搭建小程序后台服务
2018/01/03 Javascript
webpack4 处理SCSS的方法示例
2018/09/03 Javascript
vue项目打包后上传至GitHub并实现github-pages的预览
2019/05/06 Javascript
微信小程序页面传多个参数跳转页面的实现方法
2019/05/17 Javascript
JQuery+Bootstrap 自定义全屏Loading插件的示例demo
2019/07/03 jQuery
Vue配置marked链接添加target=&quot;_blank&quot;的方法
2019/07/19 Javascript
基于layui轮播图满屏是高度自适应的解决方法
2019/09/16 Javascript
使用 Github Actions 自动部署 Angular 应用到 Github Pages的方法
2020/07/20 Javascript
解决Python传递中文参数的问题
2015/08/04 Python
Python 使用requests模块发送GET和POST请求的实现代码
2016/09/21 Python
Python实现抢购IPhone手机
2018/02/07 Python
Python socket非阻塞模块应用示例
2019/09/12 Python
python实现机器人卡牌
2019/10/06 Python
浅谈Python协程
2020/06/17 Python
如何在pycharm中安装第三方包
2020/10/27 Python
html5 css3 动态气泡按钮实例演示
2012/12/02 HTML / CSS
详解移动端HTML5音频与视频问题及解决方案
2018/08/22 HTML / CSS
X/HTML5 和 XHTML2
2008/10/17 HTML / CSS
麦德龙官方海外旗舰店:德国麦德龙超市
2017/12/23 全球购物
SKECHERS斯凯奇中国官网:来自美国的运动休闲品牌
2018/11/14 全球购物
团队拓展活动总结
2014/08/27 职场文书
小学教师先进事迹材料
2014/12/15 职场文书
幼师辞职信范文
2015/02/27 职场文书
被告答辩状范文
2015/05/22 职场文书
一个都不能少观后感
2015/06/04 职场文书
教师节获奖感言
2015/07/31 职场文书
阳光体育运动标语口号
2015/12/26 职场文书
《分一些蚊子进来》读后感3篇
2020/01/09 职场文书
vue-router中hash模式与history模式的区别
2021/06/23 Vue.js