我的環境是 anaconda python 3.8.5。首先安裝 mysql_connector_python。試了幾種方法都不靈。直接下載:
mysql_connector_python-8.0.30-py2.py3-none-any.whl
$ wget https://files.pythonhosted.org/packages/94/42/5f1c4974d346ff015af67a5b1b1818487caaee60659aefd7263af3e2e52a/mysql_connector_python-8.0.30-py2.py3-none-any.whl
然後使用命令:
$ pip install mysql_connector_python-8.0.30-py2.py3-none-any.whl
需要聯網自動下載一些依賴。安裝完成。
Python 3.8.5 (default, Sep ...)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>> conn = mysql.connector.connect(host="mysql db ip地址", user="root", password="****", database="workflow_workflow1", port=3316, charset=1)
>>> cmd = conn.cursor()
>>> rs = cmd.fetchall()
>>> print(rs)
>>> conn.close()
如果不指定 charset=1 或者 指定 charset=‘utf8’ 都會報錯誤:
raise ProgrammingError(f"Character set ‘{charset}’ unsupported")
總結
簡單用用沒問題。復雜的業務用 java 吧!
參考
https://dev.mysql.com/doc/connector-python/en/connector-python-tutorial-cursorbuffered.html
https://blog.csdn.net/weixin_42361635/article/details/115889694