<table style="height: 30px; background-color: #afeeee; width: 1266px; ; width: 1266px;" border="0">
<tr><td><span style="font-size: 16px;">一、python与mysql交互</td>
</tr>
</table>
因版本不同python操作mysql有两个模块,python3不再支持MySQL-python,模块使用都一样:
python2.7:MySQL-python
python3:pymysql
安装:
pip install Mysql-python
pip install pymysql
pymysql介绍:
1.执行原生sql语句
=pymysql.connect(host=,port=3307,user=,passwd=,db=)
cursor=conn.cursor()
num=cursor.execute()
cursor.close()
conn.close()
2.批量执行sql
实际是循环调用execute
def executemany(self,query,args): # type: (str,list) -> int
=pymysql.connect(host=,db=)
cursor=conn.cursor()
num=cursor.executemany(,[(,),(,18,
(num)
conn.commit()
new_id = cursor.lastrowid
conn.close()
3.查询操作:fetch
<span style="color: #0000ff;">import<span style="color: #000000;"> pymysql
conn=pymysql.connect(host=<span style="color: #800000;">'<span style="color: #800000;">10.0.0.241<span style="color: #800000;">',db=<span style="color: #800000;">'<span style="color: #800000;">student<span style="color: #800000;">')<span style="color: #008000;">#<span style="color: #008000;">创建连接
cursor=conn.cursor()<span style="color: #008000;">#<span style="color: #008000;">创建游标
cursor =<span style="color: #000000;"> conn.cursor()
cursor.execute(<span style="color: #800000;">"<span style="color: #800000;">select * from student<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #008000;">#<span style="color: #008000;"> 获取第一行数据
row_1=<span style="color: #000000;">cursor.fetchone()
<span style="color: #0000ff;">print<span style="color: #000000;">(row_1)
<span style="color: #008000;">#<span style="color: #008000;"> 获取前n行数据
row_2=cursor.fetchmany(3<span style="color: #000000;">)
<span style="color: #0000ff;">print<span style="color: #000000;">(row_2)
<span style="color: #008000;">#<span style="color: #008000;"> 获取所有数据<span style="color: #008000;">
<span style="color: #008000;"> row_3=cursor.fetchall()
cursor.scroll(0,mode=<span style="color: #800000;">'<span style="color: #800000;">absolute<span style="color: #800000;">')<span style="color: #008000;">#<span style="color: #008000;">将游标重新移至开始处
row_new=<span style="color: #000000;">cursor.fetchone()
<span style="color: #0000ff;">print<span style="color: #000000;">(row_new)
cursor.close()<span style="color: #008000;">#<span style="color: #008000;">关闭游标
conn.close()<span style="color: #008000;">#<span style="color: #008000;">关闭连接
TIPS:使用fetchone获取数据如同读取文件一样,如果读一行游标会下移一行,
可以使用cursor.scroll(num,mode)来移动游标位置,如:
- cursor.scroll(1,mode='relative') # 相对当前位置移动
- cursor.scroll(2,mode='absolute') # 相对绝对位置移动
4.设置fetch获取数据类型
默认使用fetch查询结果是tuple,我们还可以设置获取的数据返回时dict
<span style="color: #0000ff;">import<span style="color: #000000;"> pymysql
conn = pymysql.connect(host=<span style="color: #800000;">'<span style="color: #800000;">10.0.0.241<span style="color: #800000;">',db=<span style="color: #800000;">'<span style="color: #800000;">student<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #008000;">#<span style="color: #008000;"> 游标设置为字典类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)<span style="color: #008000;">#<span style="color: #008000;">设置游标类型为字典
r = cursor.execute(<span style="color: #800000;">"<span style="color: #800000;">select * from student<span style="color: #800000;">"<span style="color: #000000;">)
res =<span style="color: #000000;"> cursor.fetchone()
<span style="color: #0000ff;">print<span style="color: #000000;">(res)
cursor.close()
conn.close()
结果:
{<span style="color: #800000;">'<span style="color: #800000;">name<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">wd<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">age<span style="color: #800000;">': 22,<span style="color: #800000;">'<span style="color: #800000;">date<span style="color: #800000;">': datetime.date(1993,5,22)}