python与mysql(四)

使用pymysql来将sql语句嵌套进入python

安装pymysql

pip3 install mysql

https://pypi.org/ pip管理组织 这个网站有所有的python模块

https://mirrors.tuna.tsinghua.edu.cn/help/pypi/ 清华大学开源镜像站

使用操作

1、执行SQL

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import pymysql

  

# 创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')

# 创建游标

cursor = conn.cursor()

  

# 执行SQL,并返回收影响行数

effect_row = cursor.execute("update hosts set host = '1.1.1.2'")

  

# 执行SQL,并返回受影响行数

#effect_row = cursor.execute("update hosts set host = '1.1.1.2' where nid > %s", (1,))

  

# 执行SQL,并返回受影响行数

#effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])

  

  

# 提交,不然无法保存新建或者修改的数据

conn.commit()

  

# 关闭游标

cursor.close()

# 关闭连接

conn.close()

2、获取新创建数据自增ID

1

2

3

4

5

6

7

8

9

10

11

12

13

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import pymysql

  

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')

cursor = conn.cursor()

cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])

conn.commit()

cursor.close()

conn.close()

  

# 获取最新自增ID

new_id = cursor.lastrowid

3、获取查询数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import pymysql

  

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')

cursor = conn.cursor()

cursor.execute("select * from hosts")

  

# 获取第一行数据

row_1 = cursor.fetchone()

  

# 获取前n行数据

# row_2 = cursor.fetchmany(3)

# 获取所有数据

# row_3 = cursor.fetchall()

  

conn.commit()

cursor.close()

conn.close()

注:在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:

  • cursor.scroll(1,mode='relative')  # 相对当前位置移动
  • cursor.scroll(2,mode='absolute') # 相对绝对位置移动(向下)
  • cursor.scroll(-2,mode='absolute') # 相对绝对位置移动(向上)
  • 类似于内部使用指针会将所有的数据写入内存 与分页相比 数据量较大时同样会导致宕机

4、fetch数据类型

  关于默认获取的数据是元祖类型,如果想要或者字典类型的数据,即:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import pymysql

  

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')

  

# 游标设置为字典类型

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

r = cursor.execute("call p1()")

  

result = cursor.fetchone()

  

conn.commit()

cursor.close()

conn.close()

 

#字符串拼接sql(禁止使用会产生sql注入问题)
inp = input('请输入班级:')
sql = 'instert into class(caption) values(%s)'
sql = sql % (inp,)
r = cusor.execute(sql)
print(r)

#参数传递
inp = input('请输入班级:')
r = cursor.execute('instert into class(caption) values(%s)',inp)

所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。具体来说,它是利用现有应用程序,将(恶意的)SQL命令注入到后台数据库引擎执行的能力,它可以通过在Web表单中输入(恶意)SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句。 [1]  比如先前的很多影视网站泄露VIP会员密码大多就是通过WEB表单递交查询字符暴出的,这类表单特别容易受到SQL注入式攻击

eg: 

 

此时--将后面的password注释 or 1=1 恒成立

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值