pymysql模块

2021/11/14 Python

pymysql模块,python连接数据库

1、pymysql模块

支持python代码操作数据库

安装

pip3 install pymysql

2、使用

import pymysql
from pymysql.cursors import Cursor

conn = pymysql.connect(
  host ='127.0.0.1',
  port = 3306,
  user = 'root',
  password ='mysql',
  database= 'db1',
  charset ='utf8'
)
 # 产生一个游标对象
cursor = conn.cursor()

sql = 'select * from t1;'

res = cursor.execute(sql)

# print(res)  # 返回sql语句影响的条数

# print(cursor.fetchone())  # 只拿一条数据
print(cursor.fetchall())  # 全拿 
# print(cursor.fetchmany(2))  # 指定拿多少条
# ((1, 'www'), (2, 'eee'))

这种数据不太好处理,可以修改为字典格式,方便看。

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

数据每次读取都是索引递增的,所以每获取一次,下次获取都是接着上次的获取,想要修改读取位置的话可以使用cursor.scroll()

# 相对移动(relative) 在当前索引的位置向后移动一位
cursor.scroll(1, 'relative')

# 绝对(absolute)  在起始位置开始移动
cursor.scroll(1, 'absolute')

3、sql注入

sql注入就是利用语法的特性,书写一些特定的语句实现固定的语法,比如利用MySQL的注释

如有以下代码:

import pymysql
from pymysql.cursors import Cursor

conn = pymysql.connect(
  host ='127.0.0.1',
  port = 3306,
  user = 'root',
  password ='mysql',
  database= 'db1',
  charset ='utf8'
)

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

username = input('请输入账号:')
password = input('请输入密码:')
print(username, password)
sql = "select * from user where name='%s' and password='%s';"%(username, password)
rows = cursor.execute(sql)

if rows:
    print('登录成功')
    print(cursor.fetchall())
else:
    print('用户名密码错误')

数据库的内容是:

mysql> select * from user;
+------+------+----------+
| id   | name | password |
+------+------+----------+
|    1 | aaa  | 123      |
|    2 | bbb  | 123      |
+------+------+----------+

运行python代码,执行以下输入

xxx' or 1=1  -- adads

执行后的sql语句为select * from user where name='xxx' or 1=1 -- adads' and password='';

然后执行,命令行返回的内容如下

登录成功
[{'id': 1, 'name': 'aaa', 'password': '123'}, {'id': 2, 'name': 'bbb', 'password': '123'}]

成功的注入了一条sql

如果知道用户名还可以简单的注入

aaa' -- ddd

执行后的sql语句为select * from user where name='aaa' -- ddd' and password='';

输出

登录成功
[{'id': 1, 'name': 'aaa', 'password': '123'}]

3.1 解决办法

敏感的数据不要自己拼接,交给execute()方法,sql语句内用百分号占位即可

  sql = "select * from user where name=%s and password=%s"
  # 自动识别%s,并且替换,而且还会过滤敏感符号
  rows = cursor.execute(sql,(username,password))

4、pymysql增删改

# 增
sql = 'insert into user(name, password) values(%s, %s)'
rows = cursor.execute(sql, ('aaa', '123'))
conn.commit()

# 改
sql = 'update user set name="abc" where id =1'
rows = cursor.execute(sql)
conn.commit()

# 删
sql = 'delete from user where id = 2'
rows = cursor.execute(sql)
conn.commit()

如果不想每次都commit()一次的话可以配置为自动提交

conn = pymysql.connect(
  host ='127.0.0.1',
  port = 3306,
  user = 'root',
  password ='mysql',
  database= 'db1',
  charset ='utf8',
  autocommit = True
)

如果有多次插入的需求,可以使用executemany()方法

sql = 'insert into user(name, password) values(%s, %s)'
rows = cursor.execute(sql, [('aaa', '123'), ('aaa2', '123'), ('aaa3', '123')])

Search

    Table of Contents