用的是 Python 3.6.0
windows 用戶
windows 用戶可以直接在 pycharm 中的preference 中安裝符合當前版本的 mysql-connector-python 和 mysqlclient 包
mac 用戶
方法1
mac 用戶可以在 /Users/XXX/Downloads/pycharmqt/mysql 里面先安裝一個mysql-connector-python-2.1.5-macos10.12 (資源請去官網(wǎng)搜索酸舍;同事找我來拿)
brew install mysql
export PATH=$PATH:/usr/local/mysql/bin
pip3 install mysqlclient
方法2
同 windows 用戶
詳情請參見:
http://stackoverflow.com/questions/25459386/mac-os-x-environmenterror-mysql-config-not-found
測試代碼
import MySQLdb
db = MySQLdb.connect(host="192.168.1.251",user="root",passwd="root", db="LangLibUser")
c = db.cursor()
c.execute("SELECT Id from SNS_UserBaseInfo limit 1")
print(c.fetchone())
測試成功結果如下:
/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/bin/python3.6
/Users/liuyuewei/Downloads/PyCharm/test0206/test.py
('O10171638’,)
Process finished with exit code 0