mysqlforpython27
這兩個完全兼容,沒問題的。
python連接mysql常用的庫如下:
1,mysql-python也就是MySQLdb;
2,PyMySQL。支持jython,IronPython,CPython等;
3,MySQL Connector/Python。純python實現的MySQL介面,由Oracle維護;
2. python2.7中如果腳本文件放在中文目錄下,pymysql連接資料庫會報錯,怎麼解決(不要修改中文目錄名為英文)
解決方法為:
創建數據表時指定charset=utf8,如:
usehongxiudb;
createtableifnotexistshongxiu(
nametext,
authortext,
introtext
)engine=InnoDBdefaultcharset=utf8;
3. 如何安裝python的MySQLdb模塊
到官方下載MySQL for Python
然後解壓,打開README:
裡面有安裝過程:
$tarxfzMySQL-python-1.2.1.tar.gz
$cdMySQL-python-1.2.1
$#editsite.cfgifnecessary
$pythonsetup.pybuild
$sudopythonsetup.pyinstall#orsufirst
不過在python setup.py build時報錯:
ImportError: No mole named setuptools
ubuntu下安裝:
sudo apt-get install python-setuptools
python-setuptools : Python Distutils Enhancements (setuptools compatibility)
然後再次python setup.py build,又報錯:
EnvironmentError: mysql_config not found
因為mysql_config是屬於MySQL開發用的文件,而使用apt-get安裝的MySQL是沒有這個文件的,於是在包安裝器裡面尋找
sudo apt-get install libmysqld-dev
libmysqld-dev : MySQL embedded database development files
再次運行python setup.py build,報錯:
building 『_mysql』 extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,3,』final』,0) -D__version__=1.2.3 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-i686-2.7/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUX -DUNIV_LINUX
In file included from _mysql.c:29:0:
pymemcompat.h:10:20: fatal error: Python.h: No such file or directory
解決方案,
sudo apt-get install python-dev
python-dev : header files and a static library for Python (default)
然後就按照README里的:
$ python setup.py build
$ sudo python setup.py install
測試:
>>>import MySQLdb
沒有報錯即可。
4. python怎麼安裝mysqldb
在配置Django時,選擇的是mysql資料庫,要安裝MySQLdb模塊,不過安裝過程中,遇到了很多errors,記錄一下。
系統:ubuntu 11.10
mysql:直接apt-get安裝的,version:5.1.62
到官方下載MySQL for Python
然後解壓,打開README:
裡面有安裝過程:
tarxfzMySQL?python?1.2.1.tar.gz cd MySQL-python-1.2.1# edit site.cfg if necessary# edit site.cfg if necessary python setup.py build$ sudo python setup.py install # or su first不過在python setup.py build時報錯:
ImportError: No mole named setuptools
ubuntu下安裝:
sudo apt-get install python-setuptools
python-setuptools : Python Distutils Enhancements (setuptools compatibility)然後再次python setup.py build,又報錯:
EnvironmentError: mysql_config not found
因為mysql_config是屬於MySQL開發用的文件,而使用apt-get安裝的MySQL是沒有這個文件的,於是在包安裝器裡面尋找sudo apt-get install libmysqld-dev
libmysqld-dev : MySQL embedded database development files再次運行python setup.py build,報錯:
building 『_mysql』 extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,3,』final』,0) -D__version__=1.2.3 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-i686-2.7/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUX -DUNIV_LINUXIn file included from _mysql.c:29:0:
pymemcompat.h:10:20: fatal error: Python.h: No such file or directory解決方案,
sudo apt-get install python-dev
python-dev : header files and a static library for Python (default)然後就按照README里的:
pythonsetup.pybuild sudo python setup.py install測試:
>>>import MySQLdb
沒有報錯即可。
5. Python2.7安裝mysql-python報錯,求教
# 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/procts/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/[email protected]/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()
6. mac環境下安裝mysql for python失敗
你需要安裝 Xcode 的 Command Line Tools 後才行。
下載地址 https://developer.apple.com/downloads/index.action
需要用 Apple Developer 帳號登錄
同時要確保機器上安裝的 mysql 時正常的,否則安裝 MySQL-python 又會出現另一個錯誤
7. 如何在mac上安裝mysql for python
在 Mac OS X 下安裝 python-mysql方法如下:
1、先把之前裝的卸載干凈:
pip uninstall mysql-python
brew uninstall mysql-connector-c
2、現在設置下mysql_config路徑:
首先修改系統配置文件vim ~/.bash_profile或者有些人是 ~/.profile,export PATH=$PATH:/Applications/MAMP/Library/bin
這里/Applications/MAMP/Library/bin是MAMP的mysql配置文件路徑
3、安裝brew install mysql-connector-cpip install mysql-python即可。
8. 如何用python的pip安裝mysqldb
如何用python的pip安裝mysqldb
python2.7和python3.4你可以看成是兩個軟體,你在一個里裝了,另一個里當然不能用,你需要在python3.4里也安裝mysqldb才可以,你可以通過pip install mysql-python,或者python3 setup.py install 來安裝。
到官方下載MySQL for Python
然後解壓,打開README:
裡面有安裝過程:
$ tar xfz MySQL-python-1.2.1.tar.gz
$ cd MySQL-python-1.2.1
$ # edit site.cfg if necessary
$ python setup.py build
$ sudo python setup.py install # or su first
9. mysql5.6支持python2.7嗎
python 2.7是萬能的,能不能支持要看你用的模塊,目測pymysql 和mysqldb模塊應該都是支持的