Python 安装 MySQLdb

http://www.blogjava.net/huyi2006/articles/247966.html http://eatsalt.blog.163.com/blog/static/87940266201102311504421/ http://hi.baidu.com/hevensun/item/51ee140eca4b3d26a0312d81 http://blog.csdn.net/showljj/article/details/8510871 http://scelong.iteye.com/blog/837055 MySQL 是十分流行的開源資料庫系統,很多網站也是使用 MySQL 作為後台資料儲存,而 Python 要連接 MySQL 可以使用 MySQL 模組。MySQLdb 模組可以讓 Python 程式連線到 MySQL server, 執行 SQL 語句及擷取資料等。 開始前要確定系統內的 Python 有安裝 MySQLdb 模式,你可以 Python command line interpreter 檢查,在指令模式輸入 python,然後便可以開始檢查: Python 2.5.1 (r251:54863, May 2 2007, 16:56:35) [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb Traceback (most recent call last): File "", line 1, in ImportError: No module named MySQLdb >>> exit() 如果見以上面的 "ImportError: No module named MySQLdb" 一句,便表示系統沒有安裝,到 MySQLdb 官方網站 下載 MySQLdb,並用以下方法安裝: http://nchc.dl.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz $ tar zxvf MySQL-python-1.2.2.tar.gz $ cd MySQL-python-1.2.2 $ python setup.py build $ python setup.py install shell中输入: wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz tar zxvf setuptools-0.6c11.tar.gz cd setuptools-0.6c11 python setup.py build python setup.py install 还要安装 python-devel (yum install python-devel*) 代码:root@vpser:~# cd MySQL-python-1.2.3 root@vpser:~/MySQL-python-1.2.3# python setup.py install sh: mysql_config: not found Traceback (most recent call last): File "setup.py", line 15, in <module> metadata, options = get_config() File "/root/MySQL-python-1.2.3/setup_posix.py", line 43, in get_config libs = mysql_config("libs_r") File "/root/MySQL-python-1.2.3/setup_posix.py", line 24, in mysql_config raise EnvironmentError("%s not found" % (mysql_config.path,)) EnvironmentError: mysql_config not found 首先查找mysql_config的位置,使用find / -name mysql_config ,比如我的在/usr/local/mysql/bin/mysql_config 修改setup_posix.py文件,在26行: mysql_config.path = "mysql_config" 修改为: mysql_config.path = "/usr/local/mysql/bin/mysql_config" 修改完成后保存,再执行 python setup.py build python setup.py install python setup.py bdist_rpm mysql_config not found 配置MySQL-Python的时候系统报错,提示: EnvironmentError: mysql_config not found Google后得知mysql_config是属于MySQL开发用的文件,而使用apt-get安装的MySQL是没有这个文件的,于是在包安装器里面寻找 libmysqld-dev libmysqlclient-dev 这两个包安装后问题即可解决 or: #apt-get -y install mysql-server mysql-client libmysqlclient-dev mytop
   今日,在安装Pylons时,前面的安装都非常的顺利,但是在安装MySQLdb工具时,在安装的时候就出现了一大陀错误,
Python代码  收藏代码
  1. mysql.c:2810: error: expected declaration specifiers before ‘init_mysql’
  2. _mysql.c:2888: error: expected ‘{’ at end of input
  3. error: <span style="color: #0000ff;">command 'gcc' failed with exit status 1</span>
在网上搜寻一番,和结合自己的实际情况,发现少安装了几个只要的软件包:
apt-get install python-dev
apt-get install libmysqlclient-dev
如此,安装就顺利完成了
Extract it and run:
python setup.py build
sudo python setup.py install
If you get this error you need to install python-dev package:
In file included from _mysql.c:29:
pymemcompat.h:10:20: error: Python.h: No such file or directory
_mysql.c:30:26: error: structmember.h: No such file or directory
In file included from /usr/include/mysql/mysql.h:44,
from _mysql.c:40:
.
.
.
_mysql.c:2808: warning: return type defaults to 'int'
_mysql.c: In function 'DL_EXPORT':
_mysql.c:2808: error: expected declaration specifiers before 'init_mysql'
_mysql.c:2886: error: expected '{' at end of input
error: command 'gcc' failed with exit status 1
Installing the python-dev package on Debian is done with apt-get or synaptic:
apt-get install python-dev
Installing the library should now work:
python setup.py build
python setup.py install
Next test the library in the python console:
import MySQLdb
# Note that this example uses UTF-8 encoding
conn = MySQLdb.connect(host='localhost', user='...', passwd='...', db='...', charset = "utf8", use_unicode = True)
cursor = conn.cursor()
cursor.execute ("SELECT * FROM cities")
rows = cursor.fetchall ()
for row in rows:
print "%s, %s" % (row[0], row[1].encode('utf-8'))
print "Number of rows returned: %d" % cursor.rowcount
Don’t forget to close the cursor and connection, and if you’re inserting data commit before closing, because autocommit is disabled by default:
原文地址:https://www.cnblogs.com/adodo1/p/4327453.html