加入收藏 | 设为首页 | 会员中心 | 我要投稿 莱芜站长网 (https://www.0634zz.com/)- 云连接、建站、智能边缘云、设备管理、大数据!
当前位置: 首页 > 编程开发 > Python > 正文

python与MySQL

发布时间:2020-08-02 20:16:51 所属栏目:Python 来源:互联网
导读:一、python与mysql交互因版本不同python操作mysql有两个模块,python3不再支持MySQL-python,模块使用都一样:python2.7:MySQL-pythonpython3:pymysql安装:pip install Mysql-pythonpip install pymysqlpymysql介绍:1.执行原生sql语句#!/usr/bin/env pytho
<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)}

(编辑:莱芜站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

<table style="height: 30px; background-color: #afeeee; width: 1266px; ; width: 1266px;" border="0">

    推荐文章
      热点阅读