加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

pymysql模块

发布时间:2020-12-12 00:03:05 所属栏目:MySql教程 来源:网络整理
导读:一、模块安装 pip3 install pymysql 二、链接、执行sql、关闭(游标) =input( =input( span style="color: #008000;"# span style="color: #008000;"链接 conn=pymysql.connect(host=span style="color: #800000;"' span style="color: #800000;"localhost

一、模块安装

pip3 install pymysql

二、链接、执行sql、关闭(游标)

=input(=input(<span style="color: #008000;">#<span style="color: #008000;">链接
conn=pymysql.connect(host=<span style="color: #800000;">'
<span style="color: #800000;">localhost
<span style="color: #800000;">'
,user=<span style="color: #800000;">'
<span style="color: #800000;">root
<span style="color: #800000;">'
,password=<span style="color: #800000;">'
<span style="color: #800000;">123
<span style="color: #800000;">'
,database=<span style="color: #800000;">'<span style="color: #800000;">egon<span style="color: #800000;">',charset='utf8<span style="color: #000000;">) #指定字符编码,可显示中文
<span style="color: #008000;">#<span style="color: #008000;">游标
cursor=<span style="color: #000000;">conn.cursor()

<span style="color: #008000;">#<span style="color: #008000;">执行sql语句
sql=<span style="color: #800000;">'<span style="color: #800000;">select * from userinfo where name="%s" and password="%s"<span style="color: #800000;">' %(user,pwd) <span style="color: #008000;">#<span style="color: #008000;">注意%s需要加引号
<span style="color: #0000ff;">print<span style="color: #000000;">(sql)
res=cursor.execute(sql) <span style="color: #008000;">#<span style="color: #008000;">执行sql语句,返回sql查询成功的记录数目
<span style="color: #0000ff;">print<span style="color: #000000;">(res)

cursor.close()
conn.close()

<span style="color: #0000ff;">if<span style="color: #000000;"> res:
<span style="color: #0000ff;">print(<span style="color: #800000;">'<span style="color: #800000;">登录成功<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">print(<span style="color: #800000;">'<span style="color: #800000;">登录失败<span style="color: #800000;">')

三、execute()之sql注入

注意:符号--会注释掉它之后的sql,正确的语法:--后至少有一个任意字符

根本原理:就根据程序的字符串拼接name='%s',我们输入一个xxx' -- haha,用我们输入的xxx加'在程序中拼接成一个判断条件name='xxx' -- haha'

最后那一个空格,在一条sql语句中如果遇到select * t1 where id > 3 -- name=;则--<span style="color: #008000;">#<span style="color: #008000;">1、sql注入之:用户存在,绕过密码
egon<span style="color: #800000;">'
<span style="color: #800000;"> -- 任意字符

<span style="color: #008000;">#<span style="color: #008000;">2、sql注入之:用户不存在,绕过用户与密码
xxx<span style="color: #800000;">'<span style="color: #800000;"> or 1=1 -- 任意字符

解决方法:

<span style="color: #008000;">#<span style="color: #008000;">改写为(execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了)
sql=<span style="color: #800000;">"<span style="color: #800000;">select * from userinfo where name=%s and password=%s<span style="color: #800000;">" <span style="color: #008000;">#<span style="color: #008000;">!!!注意%s需要去掉引号,因为pymysql会自动为我们加上

<span style="color: #ff0000;">单条数据执行sql语句
cursor.execute(sql,[user,pwd]) <span style="color: #008000;">#<span style="color: #008000;">pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。

)

四、增、删、改:conn.commit()

conn=pymysql.connect(host=,database= cursor=<span style="color: #008000;">#<span style="color: #008000;">执行sql语句<span style="color: #008000;">

<span style="color: #008000;">part1<span style="color: #008000;">

<span style="color: #008000;"> sql='insert into userinfo(name,password) values("root","123456");'<span style="color: #008000;">

<span style="color: #008000;"> res=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数<span style="color: #008000;">

<span style="color: #008000;"> print(res)

<span style="color: #008000;">#<span style="color: #008000;">part2<span style="color: #008000;">

<span style="color: #008000;"> sql='insert into userinfo(name,password) values(%s,%s);'<span style="color: #008000;">

<span style="color: #008000;"> res=cursor.execute(sql,("root","123456")) #执行sql语句,返回sql影响成功的行数<span style="color: #008000;">

<span style="color: #008000;"> print(res)

<span style="color: #008000;">#<span style="color: #008000;">part3
sql=<span style="color: #800000;">'<span style="color: #800000;">insert into userinfo(name,%s);<span style="color: #800000;">'<span style="color: #000000;">
res=cursor.executemany(sql,[(<span style="color: #800000;">"<span style="color: #800000;">root<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">123456<span style="color: #800000;">"),(<span style="color: #800000;">"<span style="color: #800000;">lhf<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">12356<span style="color: #800000;">"),(<span style="color: #800000;">"<span style="color: #800000;">eee<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">156<span style="color: #800000;">")]) <span style="color: #008000;">#<span style="color: #008000;">执行sql语句,返回sql影响成功的行数
<span style="color: #0000ff;">print<span style="color: #000000;">(res)

conn.commit() <span style="color: #008000;">#<span style="color: #ff0000;">提交后才发现表中插入记录成功
<span style="color: #000000;">cursor.close()
conn.close()

五、查:fetchone,fetchmany,fetchall

conn=pymysql.connect(host=,charset='utf8' cursor=
设置查询的结果为字典格式

sql==cursor.execute(sql) 

#查单条
res1=
res2=
res3=  (3,‘root’,'123456')

res4=cursor.fetchmany(2 ,

res5=( %conn.commit() <span style="color: #008000;">#<span style="color: #008000;">提交后才发现表中插入记录成功
<span style="color: #000000;">cursor.close()
conn.close()

<span style="color: #800000;">'''<span style="color: #800000;">
(1,'root','123456')
(2,'123456')
(3,'123456')
((4,'123456'),(5,'123456'))
((6,(7,'lhf','12356'),(8,'eee','156'))
8 rows in set (0.00 sec)
<span style="color: #800000;">'''

六、获取插入的最后一条数据的自增ID

=pymysql.connect(host=,charset='utf8=sql=<span style="color: #800000;">'<span style="color: #800000;">insert into userinfo(name,password) values("xxx","123");<span style="color: #800000;">'<span style="color: #000000;">
rows
=<span style="color: #000000;">cursor.execute(sql)

conn.commit()
<span style="color: #0000ff;">print(<span style="color: #ff0000;">cursor.lastrowid) <span style="color: #008000;">#<span style="color: #008000;">在commit之前和之后都可以查看
<span style="color: #000000;">cursor.close()
conn.close()

七、自定义sqlhelper!

<span style="color: #0000ff;">class<span style="color: #000000;"> SQLHelper(object):

</span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__init__</span><span style="color: #000000;"&gt;(self):
    self.conn </span>=<span style="color: #000000;"&gt; None
    self.cursor </span>=<span style="color: #000000;"&gt; None

</span><span style="color: #0000ff;"&gt;def</span> open(self,cursor=<span style="color: #000000;"&gt;pymysql.cursors.DictCursor):
    self.conn </span>=<span style="color: #000000;"&gt; db_pool.POOL.connection()
    self.cursor </span>= self.conn.cursor(cursor=<span style="color: #000000;"&gt;cursor)

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; close(self):
    self.cursor.close()
    self.conn.close()

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; fetchone(self,sql,params):
    cursor </span>=<span style="color: #000000;"&gt; self.cursor
    cursor.execute(sql,params)
    result </span>=<span style="color: #000000;"&gt; cursor.fetchone()

    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; result

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; fetchall(self,params)
    result </span>=<span style="color: #000000;"&gt; cursor.fetchall()
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; result

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; create(self,params)
    self.conn.commit()
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; self.cursor.lastrowid

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; delete(self,params)
    self.conn.commit()

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; update(self,params)
    self.conn.commit()

</span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__enter__</span><span style="color: #000000;"&gt;(self):
    self.open()
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; self

</span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__exit__</span><span style="color: #000000;"&gt;(self,exc_type,exc_val,exc_tb):
    self.close()</span></pre>

<div class="cnblogs_code">

         SQLHelper()  helper.fetchone(,
        result2 </span><span style="color: #808080;"&gt;=</span> helper.fetchall(<span style="color: #ff0000;"&gt;'</span><span style="color: #ff0000;"&gt;select * from users </span><span style="color: #ff0000;"&gt;'</span>,<span style="color: #ff0000;"&gt;[]</span><span style="color: #000000;"&gt;)  <span style="color: #008000;"&gt;#查所有记录</span>

        helper.</span><span style="color: #0000ff;"&gt;create</span>(<span style="color: #ff0000;"&gt;'</span><span style="color: #ff0000;"&gt;insert into user (name,pwd) value(%s,%s)</span><span style="color: #ff0000;"&gt;'</span><span style="color: #000000;"&gt;,</span><span style="color: #ff0000;"&gt;[</span><span style="color: #ff0000;"&gt;'hc',123</span><span style="color: #ff0000;"&gt;]</span>)   <span style="color: #008000;"&gt; # 创建记录</span></pre>

使用时

(编辑:李大同)

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

    推荐文章
      热点阅读