python MySQLdb Windows下安装教程及问题解决方法(python2.7)
使用python访问mysql,需要一系列安装
linux下MySQLdb安装见
Python MySQLdb在Linux下的快速安装
http://www.jb51.net/article/65743.htm
-------------------------------------------------------------
以下是windows环境下的:
1. 安装数据库mysql
下载地址:http://www.mysql.com/downloads/
可以顺带装个图形工具,我用的是MySQL-Navicat
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编译环境,直接
2.2.3问题汇总
A. 64位系统,无法读取注册表的问题
异常信息如下:
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的安装位置
#mysql_root, dummy = _winreg.QueryValueEx(serverKey,'Location')
mysql_root = r"F:\devtools\MySQL\MySQL Server 5.5"
B.没有gcc编译环境
解决方法:安装编译环境(一个老外的帖子)
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中加入:
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 = [ '' ]
目前就遇到这几个问题,望补充
3. 增删改查代码示例及结果(just for test)
`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;
#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()
原文:http://www.jb51.net/article/65746.htm
python MySQLdb Windows下安装教程及问题解决方法(python2.7)的更多相关文章
- Python 在Windows下安装matplotlib
windows下安装很麻烦,使用easy_install 安装报错 提示缺少freetype 和png 后经多方查询,最终安装成功 以下是安装过程 前提你的Python环境已经搭建好了 1.前提需要 ...
- redis在windows下安装教程
安装过程 1.首先先把下载的压缩包解压到一个文件夹中2.打开cmd指令窗口3.输入你刚才解压的文件路径4.然后输入redis-server redis.windows.conf 命令接下来部署Redi ...
- Python 在 Windows 下安装第三方包,报 Python 未注册的问题解决
保存一些代码为 reg.py,运行之即可. #/usr/bin/env python # -*- coding: utf-8 -*- import sys from _winreg import * ...
- python在windows下安装
打开python官方网站:https://www.python.org/downloads/ 点击下载 翻到底下的file目录下 选择对应的32,64位系统进行安装 一般来说选择Windows x86 ...
- Python在Windows下安装第三方库浅谈
在用python编写代码时,往往需要用到第三方库,那么python如何去用第三方库呢,首先我们先来看看是如何安装的,方法可能会很多,但这边只介绍一种,其它请百度或google 比如asyncio,这里 ...
- [python]解决Windows下安装第三方插件报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0:
系统:win7IDE:pycharm Python版本:2.7 安装第三方插件是报错: 报错原因与编码有关,pip把下载的临时文件存放在了用户临时文件中,这个目录一般是C:\Users\用户名\Ap ...
- [python]记录Windows下安装matplot的经历
最近学习在看<机器学习实战>一书,第二章的时候要用到Natplotlib画图,于是便开始安装Matplotlib.本文所用到的所有安装包都可以在文末的链接中找到. 首先从Matplotli ...
- 【python】windows下安装xgboost的python库
傻瓜教程 主要参考了https://www.hongweipeng.com/index.php/archives/826/ 和 https://github.com/dmlc/xgboost/iss ...
- python在windows下安装paramiko模块和安装pycrypto模块(3步搞定)(转)
Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,我们需要先安装pycr ...
随机推荐
- BusyBox 简化嵌入式 Linux 系统【转】
转自:http://www.cnblogs.com/hnrainll/archive/2011/06/10/2077393.html BusyBox 的诞生 BusyBox 最初是由 Bruce Pe ...
- bytearray和file的后端上传方式
public static String readAndUpload(String serverpath,String imgid) { if(serverpath==null){ serverpat ...
- JDBC-oracle(登陆)
题目: 第一步:创建用户表,并插入数据(插入后记得commit) create table users ( name ), password ) ); '); '); 第二步:编写登陆界面(index ...
- TypeError at /post/ render_to_response() got an unexpected keyword argument 'context_instance'
Exception Type: TypeError at /post/ Exception Value: render_to_response() got an unexpected keyword ...
- context:exclude-filter spring事宜【经典-转】
context:exclude-filter spring事务 如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行. ...
- 【京东账户】——Mysql/PHP/Ajax爬坑之购物车删除选项
一.引言 做京东账户项目中的购物车模块,功能之三就是删除购物车中的选项.要用到的是Apach环境,Mysql.PHP以及Ajax. 二.依据功能创建库.表.记录 创建库:jd 创建表:购物车表 jd ...
- tomcat启动项目,起不起来
右键tomcat 选择publish
- Linux安装httpd2.4.10
1. cd /mnt tar zxvf httpd-2.4.10.tar.gz ./configure --prefix=/mnt/apache2 --enable-dav --enable-modu ...
- struts2实现文件查看、下载
CreateTime--2017年9月7日10:25:33 Author:Marydon struts2实现文件查看.下载 1.界面展示 <a style="color: #199 ...
- docker创建私有仓库及存储image
Docker官方的Docker hub尽管提供了有非常多image,也基本上包括了我们须要使用的,可是其訪问起来比較慢.假设自己要定制image.多台server之间的共享使用此image非常不方便. ...