1、场景:python连接mysql的API使用pymysql库
https://pypi.python.org/pypi/PyMySQL/
安装:pip install pymysql 即可
2、代码示例:
#!/usr/bin/env python
# --coding = utf-8
# Author Jason.F
import pymysql
#创建链接对象
conn = pymysql.connect(host='XX', port=3306, user='XX', passwd='XX', db='XX')
#创建游标
cursor = conn.cursor()
#执行sql,更新单条数据,并返回受影响行数
cursor.execute("select user_uid, course_uid, created_at from study_log limit 10")
watched_list = cursor.fetchall()
print (list(watched_list))
#关闭游标
cursor.close()
#关闭连接
conn.close()