[python3 - package] PyMySQL

1. 安装 pip install pymysql

2. 官方文档 https://pypi.python.org/pypi/PyMySQL

3. 基本用法

import pymysql

#连接mySQL server, 返回Connection对象
connect = pymysql.connect(host='127.0.0.1', user='root', passwd='pw', db='mysql')

#连接server下的database,返回Cursor对象
cur = conn.cursor()

#执行SQL语句
cur.execute('SELECT * FROM pages WHERE title=%s', ('New Page'))

#获取查询结果
cur.fetchone() #返回包含最近一条查询结果所有返回字段的tuple
cur.fetchall()/cur.fetchsize(3) #返回包含所有/特定数量的查询结果tuple的tuple

#提交所执行的SQL语句(SELECT语句不需要commit),也可以在连接server的时候,设置autocommit=True
connect.commit()

 

posted @ 2018-03-25 23:04  break大蜗牛  阅读(155)  评论(0编辑  收藏  举报