在Win7中使用Python的MySQLdb模組,win7mysqldb
概述:
Linux上對這一塊的處理還是不錯的,不過在Windows上就有一點小麻煩,麻煩的點不在於安裝過程,而是在安裝的過程中可能會有一些問題。
步驟:1.安裝MySQLdb模組
我們在網上下載相應的MySQLdb的版本檔案,例如我的就是MySQL-python-1.2.3.win-amd64-py2.7。此檔案是exe檔案,直接點擊運行即可。
2.解決python version 2.7 required,which was not found in the registry報錯
在我想下載完MySQL-python-1.2.3.win-amd64-py2.7.exe進行安裝時,程式給我報了這樣一個錯誤資訊:
原因分析:
win7是64位的原因,在安裝python時,如果選擇只為目前使用者,以上問題是不會出現的,如果選擇所有使用者,那就用上面的方法解決吧
解決方案:
1.複製下面的代碼,儲存至register.py:
## 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 necessaryversion = 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()
2.運行register.py,搞定。
3.解決DLL load failed: %1 不是有效 Win32 應用程式報錯
出現上述問題的原因是因為我們的Python和MySQLdb的版本不對應造成的。我的問題是Python是32位的,而MySQLdb卻是64位的。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。http://blog.csdn.net/lemon_tree12138