pip install pymysql
创建数据库和表
CREATE DATABASE IF NOT EXISTS school;
SHOW DATABASES;
USE school;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
首先,必须建立一个连接数据库通道 --连接
pymysql.connect()方法返回的是Connections模块下的connection类实例,connection方法传参就是给Connection类的__init__提供参数
connect = Connection = Connect #
def Connect(*args, **kwargs):
from .connections import Connection
return Connection(*args, **kwargs)
class Connection(object):
def __init__(self, host=None, user=None, password="",
database=None, port=0, unix_socket=None,
charset='', sql_mode=None,
read_default_file=None, conv=None, use_unicode=None,
client_flag=0, cursorclass=Cursor, init_command=None,
connect_timeout=10, ssl=None, read_default_group=None,
compress=None, named_pipe=None,
autocommit=False, db=None, passwd=None, local_infile=False,
max_allowed_packet=16*1024*1024, defer_connect=False,
auth_plugin_map=None, read_timeout=None, write_timeout=None,
bind_address=None, binary_prefix=False, program_name=None,
server_public_key=None):
建立连接如下:
import pymysql
IP ='192.168.176.134'
USERNAME = 'yu'
PASSWORD = '1234'
DATABASE = 'test'
PORT = 3306
conn = pymysql.connect(host=IP, user=USERNAME, password=PASSWORD,database=DATABASE, port=PORT) # conn为一个连接对象
Connection初始化常用参数 | 说明 |
---|---|
host | 主机 |
user | 用户名 |
password | 密码 |
database | 数据库 |
port | 端口 |
Connection.ping()方法,测试数据库服务器是否还活着,有一个参数reconnect表示断开与服务器连接是否重连,连接关闭出现异常.(ping)并不能解决连接的问题,只是检查是否连接.
import pymysql
conn = None
try:
conn = pymysql.connect('192.168.142.10','xw','xw','dbs1')
conn.ping(False)
finally:
if conn:
conn.close()
游标Cursor
操作数据库,必须使用游标,需要先获取一个游标对象,
Connection.cursor(cursor=None)方法返回一个新的游标对象.
连接没有关闭前,游标对象可以反复使用.
cursor参数,可以指定一个Cursor类,如果为None,则使用默认的Cursor类.
操作数据库
数据库操作需要使用Cursor类实例,提供execute()方法,执行SQL语句,成功返回影响的行数
新增记录
使用insert into语句插入数据
import pymysql
IP ='172.198.11.11'
USERNAME = 'yu'
PASSWORD = '123'
DATABASE = 'test'
PORT = 3306
conn = None
cursor = None #游标,对结果集的操作
try:
conn = pymysql.connect(host=IP, user=USERNAME, password=PASSWORD,database=DATABASE, port=PORT) # 建立连接对象(socket)
cursor = conn.cursor() #建立游标对象
# sql = "select * from reg" #sql 语句
# sql = "insert into reg (long_name,name,password) values('cc','cc','123456')" #sql 插入语句
sql = "update reg set name = 'xx' where id = 3" #sql 更新语句
row = cursor.execute(sql) #结果集,影响的行数
conn.commit() #操作完后,一定要提交
print(row)
finally: # cursor 和conn 都是对象,都资源需要释放
if conn:
conn.close()
if cursor:
cursor.close()
Connection类有三个方法:
begin 开始事务
commit 将变更提交
rollback 回滚事务批量增加数据
import pymysql
IP ='172.198.11.11'
USERNAME = 'yu'
PASSWORD = '123'
DATABASE = 'test'
PORT = 3306
conn = None
cursor = None #游标,对结果集的操作
try:
conn = pymysql.connect(host=IP, user=USERNAME, password=PASSWORD,database=DATABASE, port=PORT) # 建立连接对象(socket)
cursor = conn.cursor() #建立游标对象
sql = "select id from reg where long_name = 'rose'"
row = cursor.execute(sql) #结果集,影响的行数
if row ==1:
userid = cursor.fetchone()[0] #定位到ID字段
sql1 = "update reg set name = 'rose' where id = '{}'".format(userid)
conn.commit() #操作完后,一定要提交
except: #中途有异常,则调用回滚操作
conn.rollback()
finally: # cursor 和conn 都是对象,都资源需要释放
if conn:
conn.close()
if cursor:
cursor.close()
Cursor类的获取查询结果集的方法有:fetchone()、fetchmany(size=None)、fetchall()
名称 | 说明 |
---|---|
fetchone() | 获取结果集的下一行 |
fetchmany(size=None) | size指定返回的行数,None则返回空元组 |
fetchall() | 返回剩余所有行,如果走到末尾,就返回空元组,否则返回一个元组,其元素是每一行的记录封装的一个元组 |
cursor.rownumber | 返回当前行号 |
cursor.rowccount | 返回总行数 |
注意:fetch操作的是结果集,结果集是保存在客户端的,也就是说fetch的时候,查询已经结束了。
conn = None
cursor = None #游标,对结果集的操作
conn = pymysql.connect(host=IP, user=USERNAME, password=PASSWORD,database=DATABASE, port=PORT) # 建立连接对象(socket)
cursor = conn.cursor() #建立游标对象
sql = "select * from reg" #sql 语句
print(cursor.fetchone(),111)
print(cursor.fetchmany(2))
print(cursor.fetchall())
Cursor类有一个Mixin的子类DictCursor
只需要cursor = conn.cursor(DictCursor)就可以了
from pymysql.cursors import DictCursor
conn = pymysql.connect(host='172.198.11.11', user='yu', password='123',database=dbas, port=3306)
cursor = conn.cursor(DictCursor)
print(cursor.fetchone())
print(cursor.fetchmany(2))
print(cursor.fetchall())
返回一行,是一个字典。返回多行,放在列表中,元素是字典,代表一行。
SQL注入攻击
conn = pymysql.connect(host='172.198.11.11', user='yu', password='123',database=dbas, port=3306)
cursor = conn.cursor(DictCursor)
sql = "select * from reg where id = {}".format('2 or 1 = 1') #当攻击者通过1=1拼好字符串后,就可以看到所有记录
cursor.execute(sql)
for i in cursor.fetchall() :
print(i)
------------------------------------------------------------------------
{'id': 2, 'long_name': 'tom', 'name': 'bb', 'password': '123'}
{'id': 3, 'long_name': 'jurry', 'name': 'xx', 'password': '12345'}
{'id': 4, 'long_name': 'jake', 'name': 'bend', 'password': '12345'}
{'id': 5, 'long_name': 'jake', 'name': 'divd', 'password': '12345'}
{'id': 6, 'long_name': 'dbs', 'name': 'dbs', 'password': '2345'}
{'id': 7, 'long_name': 'rose', 'name': 'rose', 'password': '12345'}
{'id': 8, 'long_name': 'wx', 'name': 'wx', 'password': '1234567'}
解决的方法如下:
conn = pymysql.connect(host='172.198.11.11', user='yu', password='123',database=dbas, port=3306)
cursor = conn.cursor(DictCursor)
id = '3 or 1=1'
sql = "select * from reg where id=%s"
cursor.execute(sql,(id,))
print(cursor.fetchall())
------------------------------------------------------------------------
F:\pycharm\venv1\lib\site-packages\pymysql\cursors.py:329: Warning: (1292, "Truncated incorrect DOUBLE value: '3 or 1=1'") #结果产生了一个警告
self._do_get_result()
[{'id': 3, 'long_name': 'jurry', 'name': 'xx', 'password': '12345'}] # 参数化查询,最后只查到一条记录,id = '3 or 1=1',只截取了前面的3
conn = pymysql.connect(host='172.198.11.11', user='yu', password='123',database=dbas, port=3306)
cursor = conn.cursor(DictCursor)
sql = "select * from reg where id=%(a)s and password=%(b)s" # 参数化查询,解决注入攻击问题
cursor.execute(sql,{'a':'3','b':'12345'})
print(cursor.fetchall())
------------------------------------------------------------------------
[{'id': 3, 'long_name': 'jurry', 'name': 'xx', 'password': '12345'}]
参数化查询为什么提高效率?
conn = pymysql.connect(host='172.198.11.11', user='yu', password='123',database=dbas, port=3306)
cursor = conn.cursor(DictCursor)
sql = "insert into reg (long_name,name,password) values ('jake',%s,%s)"
cursor.executemany(sql,(("jake{}".format(i),'axc{}'.format(i)) for i in range(5)))
conn.commit()
查看连接类和游标类的源码
# 连接类
class Connection(object)
def __enter__(self):
"""Context manager that returns a Cursor"""
warnings.warn(
"Context manager API of Connection object is deprecated; Use conn.begin()",
DeprecationWarning)
return self.cursor() #返回一个游标对象 ,占用资源
def __exit__(self, exc, value, traceback):
"""On successful exit, commit. On exception, rollback"""
if exc:
self.rollback() #退出有异常,回滚操作
else:
self.commit() #退出没有异常,提交OK
# 游标类
class Cursor(object):
def __enter__(self):
return self
def __exit__(self, *exc_info):
del exc_info
self.close() # 退出时会关闭游标对象,释放资源
连接类进入上下文的时候会返回一个游标对象,退出时如果没有异常会提交更改。游标类也使用上下文,在退出时关闭游标对象。
conn = pymysql.connect(host='172.198.11.11', user='yu', password='123',database=dbas, port=3306)
try:
with conn.cursor(DictCursor) as cursor: # 此with是操作新产生的cursor 当with结束时,新产生的cursor会关闭
for i in range(5):
insert_sql = "insert into reg (long_name,name,password) values ('rose','rose{}','abx{}')".format(i,uuid.uuid4())
cursor.execute(insert_sql)
conn.commit()
except Exception as er:
conn.rollback()
finally:
conn.close()
conn = pymysql.connect(host='172.198.11.11', user='yu', password='123',database=dbas, port=3306)
with conn as cursor: #源码中有return self.cursor() ==>即conn.cursor() ==>得到cursor游标对象
for i in range(3):
insert_sql = "insert into reg (name,password) values('tom{0}',20+{0})".format(i)
rows = cursor.execute(insert_sql)
sql = "select * from reg"
cursor.execute(sql)
print(cursor.fetchall())
cursor.close() #最后关闭cursor和conn对象
conn.close()