mysql 字符串拼接_day06python数据库mysql之pymysql

安装pymysql模块

两种方法:

第一种:pip安装

pip3 install pymysql

第二种:

07c1b6b757dbf89e510ba89df25dab65.png

1047219f066e233e9853dd1cd5fc442c.png

链接,执行sql,关闭(游标)

5ca229a58d3fee72e03ecd89d7eb451d.png

import pymysqluser= input('用户名:>>').strip()pwd= input('密码:>>').strip()#先链接,拿到游标conn=pymysql.connect(host='localhost',user='root',password='123456',             database='day47',charset='utf8')cursor=conn.cursor() #拿到游标,即mysql >#执行sqlsql='select * from user where user="%s" and password="%s";'%(user,pwd)print(sql) #注意%s需要加双引号rows = cursor.execute(sql)  #拿到受影响的行数cursor.close()conn.close()if rows:    print('登录成功')else:    print('登录失败')

execute()之sql注入

注意:符号--会注释掉它之后的sql

正确的语法:--后至少有一个任意字符

根本原理:就根据程序的字符串拼接name='%s',我们输入一个xxx' -- haha,用我们输入的xxx加'在程序中拼接成一个判断条件name='xxx' -- haha',最后那一个空格,在一条sql语句中,如果遇到select *from t1 where id > 3 -- and name='egon';则--之后的条件被注释掉了。

sql注入之:用户存在,绕过密码

egon' -- 任意字符

sql注入之:用户不存在,绕过用户与密码

xxx' or 1=1 -- 任意字符

70fa72adf9116090d3bd2e073f1e4f2a.png

22310d133e7cd9fae0583c55c3273d2f.png

688e0b8eed97abffb57ad70c4430f0f5.png

解决注入:

# 原来是我们对sql进行字符串拼接sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd)print(sql)rows=cursor.execute(sql)#改写为(execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了)sql="select * from userinfo where name=%s and password=%s"  # 注意%s需要去掉引号,因为pymysql会自动为我们加上rows=cursor.execute(sql,[user,pwd])# pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。

增、删、改:conn.commit()

增:

import pymysql# 先链接,拿到游标conn=pymysql.connect(host='localhost',user='root',password='123456',database='day47')cursor=conn.cursor() #拿到游标,即mysql >#执行sql 增:sql='insert into user1(user,password) VALUES (%s,%s)'print(sql)# rows = cursor.execute(sql,('xixi',123))  #插入一条记录rows = cursor.executemany(sql,[('xixi',123),('aaa',456),('ttt',147)]) #插入多行记录print('%s row in set (0.00 sec)'%rows)conn.commit() #提交到数据库cursor.close()conn.close()

删:

import pymysql#先链接,拿到游标name=input('>>').strip()conn=pymysql.connect(host='localhost',user='root',password='123456',database='day47')cursor=conn.cursor() #拿到游标,即mysql >#执行sql   删:sql='delete from user1 where user =%s;'  #删除数据print(sql)rows = cursor.execute(sql,(name))print('%s row in set (0.00 sec)'%rows)conn.commit() #提交到数据库cursor.close()conn.close()

改:

import pymysql#先链接,拿到游标id=input('>>').strip()conn=pymysql.connect(host='localhost',user='root',password='123456',database='day47')cursor=conn.cursor() #拿到游标,即mysql >#执行sql   改:sql=' update user1 set password = "5555555" where id=%s;'print(sql)rows = cursor.execute(sql,(id))print('%s row in set (0.00 sec)'%rows)conn.commit() #提交到数据库cursor.close()conn.close()

查:fetchone,fetchmany,fetchall

# 查fetchone,fetchmany,fetchallimport pymysqlconn=pymysql.connect(host='localhost',user='root',password='123456',database='day47')cursor=conn.cursor() #拿到游标,即mysql >#执行sql   查:sql='select * from user1;'rows = cursor.execute(sql)#查单条fetchoneres1=cursor.fetchone()res2=cursor.fetchone()res3=cursor.fetchone()print(res1)print(res2)print(res3)print(res3[0])#查多条fetchmanyprint(cursor.fetchmany(3))print(cursor.fetchone())#查所有fetchallprint(cursor.fetchall())print(cursor.fetchone())#-------光标的移动--------#1.绝对路径:从文件的开头位置算起print(cursor.fetchall())cursor.scroll(1,mode='absolute')print(cursor.fetchone())cursor.scroll(3,mode='absolute')print(cursor.fetchone())#2.相对路径:print(cursor.fetchone())print(cursor.fetchone())cursor.scroll(2,mode='relative') #相对于上面的两条向后移两条print(cursor.fetchone())print('%s row in set (0.00 sec)' %rows)cursor.close()conn.close()

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

------查看表中最后一行的iDimport pymysqlconn=pymysql.connect(host='localhost',user='root',password='123456',             database='day47',charset='utf8')cursor=conn.cursor()sql='insert into user1(user,password) values(%s,%s);'rows=cursor.execute(sql,('alex','123'))# rows=cursor.executemany(sql,[('yuanhao','123'),('laowu','123'),('kgf','12323')])conn.commit()print(cursor.lastrowid)  #查看表中最后一行的iDcursor.close()conn.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值