OS X 安裝 MySQLdb for Python
解决mysql_config not found錯誤
剛剛要安裝MySQLdb for python on mac. 結果中途出現 [EnvironmentError: mysql_config not found]
似乎是找不到mysql_config位置找不到
收尋一下自己mac上mysql_config位置是在:/usr/local/mysql/bin/mysql_config
完整步驟如下:
1. Download MySQLdb (MySQL-python-1.2.4b4.tar.gz)
2. tar -zxvf MySQL-python-1.2.4b4.tar.gz
3. cd MySQL-python-1.2.4b4
4. python setup.py build ==> error
5. vim site.cfg 找到 mysql_config = xxxxxx (換成自己mysql_config位置)
6. python setup.py build
7. sudo python setup.py install
以上
解決Reason: image not found 錯誤
在終端機下$ sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib $ sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql
再來就是測試一下python是否可以連mysql
mysql_test.py
----------------------------------------------------
# -*- coding: utf-8-*-#!/usr/bin/python
#引入 mysql module
import MySQLdb
# connect to mysql
db = MySQLdb.connect(host="localhost", user="user", passwd="passwd", db="dbname")
cursor = db.cursor()
# exec sql
cursor.execute("select * from db_table_name")
result = cursor.fetchall()
# output
for record in result:
print record[0]
print record[1]
print record[2]
----------------------------------------------------
my result :
-------------------------
1
The title
This is the post body.
2
A title once again
And the post body follows.
3
Title strikes back
This is really exciting! Not.
----------------------------
搞定
lichain . 2013/10/22
沒有留言:
張貼留言