步骤
安装mysql-connector-python
pip3 install mysql-connector-python
Python
import mysql.connector
config = {
'user': 'scott',
'password': 'password',
'host': '127.0.0.1',
'database': 'employees',
'raise_on_warnings': True
}
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
query = "select id, cn_name, `order` from tb_region where id =1 or id = 2"
try:
cursor.execute(query)
for (id, cn_name, order) in cursor:
print(id)
print(cn_name)
print(order)
finally:
cursor.close()
cnx.close()
参考:
- 5.1 Connecting to MySQL Using Connector/Python
- Python MySQL Select From