python MySQLdb Windows下安裝教程及問題解決方法
使用python訪問mysql,需要一系列安裝
linux下MySQLdb安裝見
Python MySQLdb在Linux下的快速安裝
http://www.dbjr.com.cn/article/65743.htm
-------------------------------------------------------------
以下是windows環(huán)境下的:
1. 安裝數(shù)據(jù)庫mysql
下載地址:http://www.mysql.com/downloads/
可以順帶裝個(gè)圖形工具,我用的是MySQL-Front
2. 安裝MySQLdb
好了,到了這一步,你有兩個(gè)選擇
A. 安裝已編譯好的版本(一分鐘)
B. 從官網(wǎng)下,自己編譯安裝(介個(gè)…..半小時(shí)到半天不等,取決于你的系統(tǒng)環(huán)境以及RP)
若是系統(tǒng)32位的,有c++編譯環(huán)境的,自認(rèn)為RP不錯的,可以選擇自己編譯安裝,當(dāng)然,遇到問題還是難免的,一步步搞還是能搞出來的
若是系統(tǒng)64位的,啥都木有的,建議下編譯版本的,甭折騰
2.1安裝已編譯版本:
http://www.codegood.com/downloads
根據(jù)自己系統(tǒng)下載,雙擊安裝,搞定
然后import MySQLdb,查看是否成功
我的,win7,64位,2.7版本
MySQL-python-1.2.3.win-amd64-py2.7.exe
2.2自己編譯安裝
話說搞現(xiàn)成的和自己編譯差距不一一點(diǎn)半點(diǎn)的,特別是64位win7,搞死了
2.2.1安裝setuptools
在安裝MySQLdb之前必須安裝setuptools,要不然會出現(xiàn)編譯錯誤
http://pypi.python.org/pypi/setuptools
http://peak.telecommunity.com/dist/ez_setup.py 使用這個(gè)安裝(64位系統(tǒng)必須用這個(gè))
2.2.2安裝MySQLdb
下載MySQLdb
http://sourceforge.net/projects/mysql-python/
解壓后,cmd進(jìn)入對應(yīng)文件夾
如果32位系統(tǒng)且有g(shù)cc編譯環(huán)境,直接
python setup.py build
2.2.3問題匯總
A. 64位系統(tǒng),無法讀取注冊表的問題
異常信息如下:
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
解決方法:
其實(shí)分析代碼,發(fā)現(xiàn)只是尋找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.沒有g(shù)cc編譯環(huán)境
unable to find vcvarsall.bat
解決方法:安裝編譯環(huán)境(一個(gè)老外的帖子)
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.(安裝路徑加入環(huán)境變量)
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
參數(shù)是vc特有的編譯參數(shù),如果使用mingw的話因?yàn)槭莋cc所以不支持。可以在setup_windows.py中去掉
/Zl
解決方法:
修改setup_windows.py 改為空的
#extra_compile_args = [ '/Zl' ]
extra_compile_args = [ '' ]
目前就遇到這幾個(gè)問題,望補(bǔ)充
3. 增刪改查代碼示例及結(jié)果(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()
#關(guān)閉
conn.close()
相關(guān)文章
Python中使用urllib2防止302跳轉(zhuǎn)的代碼例子
這篇文章主要介紹了Python中使用urllib2防止302跳轉(zhuǎn)的代碼例子,即避免302跳轉(zhuǎn)的實(shí)現(xiàn),需要的朋友可以參考下2014-07-07用python實(shí)現(xiàn)操縱mysql數(shù)據(jù)庫插入
大家好,本篇文章主要講的是用python實(shí)現(xiàn)操縱mysql數(shù)據(jù)庫插入,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01Python實(shí)現(xiàn)的遠(yuǎn)程登錄windows系統(tǒng)功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的遠(yuǎn)程登錄windows系統(tǒng)功能,結(jié)合實(shí)例形式分析了Python基于wmi模塊的遠(yuǎn)程連接與進(jìn)程操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06利用Python實(shí)現(xiàn)批量打包程序的工具
auto-py-to-exe與pyinstaller都無法直接一次性打包多個(gè)程序,想打包多個(gè)程序需要重新操作一遍。所以本文將用Python實(shí)現(xiàn)批量打包程序的工具,感興趣的可以了解一下2022-07-07