windows安裝mysqldb

使用Python訪問MySQL,須要一系列安裝html


linux下MySQLdb安裝見  python

Python MySQLdb在Linux下的快速安裝mysql

http://blog.csdn.net/wklken/article/details/7271019
linux


-------------------------------------------------------------c++

如下是windows環境下的:sql


1.      安裝數據庫mysql數據庫

下載地址:http://www.mysql.com/downloads/windows

能夠順帶裝個圖形工具,我用的是MySQL-Frontpython2.7

 

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)

 

[sql] view plain copy
print?

    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; 

 

 

[python] view plain copy
print?

    #-*- 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 version 2.7 required, which was not found in the registry

安裝setuptools的時候,不能再註冊表中識別出來python2.7

在網上找了方法,僅做筆記,供下次使用

 

方法:

 

新建一個register.py 文件,把一下代碼貼進去,保存(G盤)

#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html
 
import sys
 
from _winreg import *
 
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
 
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
    installpath, installpath, installpath
)
 
def RegisterPy():
    try:
        reg = OpenKey(HKEY_CURRENT_USER, regpath)
    except EnvironmentError as e:
        try:
            reg = CreateKey(HKEY_CURRENT_USER, regpath)
            SetValue(reg, installkey, REG_SZ, installpath)
            SetValue(reg, pythonkey, REG_SZ, pythonpath)
            CloseKey(reg)
        except:
            print "*** Unable to register!"
            return
        print "--- Python", version, "is now registered!"
        return
    if (QueryValue(reg, installkey) == installpath and
        QueryValue(reg, pythonkey) == pythonpath):
        CloseKey(reg)
        print "=== Python", version, "is already registered!"
        return
    CloseKey(reg)
    print "*** Unable to register!"
    print "*** You probably have another Python installation!"
 
if __name__ == "__main__":
    RegisterPy()

 

 輸入如下命令(register.py 存放在 G盤底下)

 

顯示「python 2.7 is already registered」

再安裝setuptools的時候,就能自動識別出來python2.7了。

win7是 64的緣由,在安裝python(32位)時,若是選擇只爲當前用戶,以上問題是不會出現的,若是選擇全部用戶,那就用上面的方法解決吧。

相關文章
相關標籤/搜索