为什么要使用pymysql
在使用Python工作与学习中难免会使用到mysql数据库,使用pymysql三方库可以让我们轻松的对数据库的记录进行操作,如创建、修改,删除表,如增加、删除、修改、查询数据表中的记录,下边记录一下pymysql的使用方法:
连接方法 Connect = connect = Connection
python
# 创建mysql数据库连接
conn = Connection(
user='root',
host='192.168.109.135',
password='test_12345',
port=3306,
database='test_db',
charset='utf8',
cursorclass=cursors.DictCursor
)
CURSOR种类
cursor 的种类有四种,默认使用的是Cursor,还有DictCursor,SSCursor,SSDictCursor,Cursor 返回的结果是以元组的形式返回的,DictCursor,是以字典的形式返回的。DictCursor是继承了Cursor与DictCursorMixin,DictCursorMixin里面只有函数两个方法。(可以选择自己需要的返回形式)
INSERT INTO语句(增)
单条数据插入
python
data = (1, 'group1', 'group1/M00/00/00/wKhthmT1PGuAXV09AAABFcYZOGg5350.py', 'Upload successed.', 'fdfs.py', '277B', '192.168.109.134', 'fdfs.py')
with conn.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `upload_file` (`Group name`, `Remote file_id`, `Status`, `Local file name`, `Uploaded size`, `Storage IP`, `Filename`)" \
" VALUES (%s, %s, %s, %s, %s, %s, %s)"
cursor.execute(sql, data)
conn.commit()
多条数据插入
python
data = ((1, 'group1', 'group1/M00/00/00/wKhthmT1PGuAXV09AAABFcYZOGg5350.py', 'Upload successed.', 'fdfs.py', '277B', '192.168.109.134', 'fdfs.py'), (2, 'group1', 'group1/M00/00/00/wKhthmT1TMKAH20TAAAD-MJMli03267.py', 'Upload successed.', 'fdfs.py', '1016B', '192.168.109.134', 'fdfs.py'), (3, 'group1', 'group1/M00/00/00/wKhthmT1TSqAFhCyAAxZcPR00vw.tar.gz', 'Upload successed.', 'fastdfs-6.06.tar.gz', '790.36KB', '192.168.109.134', 'fastdfs-6.06.tar.gz'), (4, 'group1', 'group1/M00/00/00/wKhthmT1TTCABEUgAABN8HAGbcY.tar.gz', 'Upload successed.', 'fastdfs-nginx-module-1.22.tar.gz', '19.48KB', '192.168.109.134', 'fastdfs-nginx-module-1.22.tar.gz'), (5, 'group1', 'group1/M00/00/00/wKhthmT1TTWAU-W3ABBjHPqFuIs.tar.gz', 'Upload successed.', 'nginx-1.22.1.tar.gz', '1.02MB', '192.168.109.134', 'nginx-1.22.1.tar.gz'), (6, 'group1', 'group1/M00/00/00/wKhthmT1TUCAaierAAKKfr3HBzE.tar.gz', 'Upload successed.', 'libfastcommon-1.0.43.tar.gz', '162.62KB', '192.168.109.134', 'libfastcommon-1.0.43.tar.gz'), (7, 'group1', 'group1/M00/00/00/wKhthmT1TVeANtONAAKKfr3HBzE.tar.gz', 'Upload successed.', '/tmp/libfastcommon-1.0.43.tar.gz', '162.62KB', '192.168.109.134', 'libfastcommon-1.0.43.tar.gz'))
with conn.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `upload_file` (`Group name`, `Remote file_id`, `Status`, `Local file name`, `Uploaded size`, `Storage IP`, `Filename`)" \
" VALUES (%s, %s, %s, %s, %s, %s, %s)"
cursor.executemany(sql, data)
conn.commit()
DELETE 语句(删)
python
def delete(pk: int):
"""
delete from table where filed1='';
:param pk:
:return:
"""
with conn.cursor() as cursor:
# delete a record
SQL = "delete from `upload_file` where id=%d" % pk
cursor.execute(SQL)
conn.commit()
UPDATE SET 语句(改)
python
def update(pk):
"""update table set column1=value1, column2=value2"""
with conn.cursor() as cursor:
# delete a record
SQL = "update upload_file set Filename='test.txt' where id=%d" % pk
cursor.execute(SQL)
conn.commit()
SELECT 语句(查)
多记录查询
python
def select(self, pk=None):
with conn.cursor() as cursor:
sql = """ SELECT * from `upload_file`"""
cursor.execute(sql)
rows = cursor.fetchall()
print(rows)
单记录查询
python
def select(self, pk):
with conn.cursor() as cursor:
sql = """ SELECT * from `upload_file` where id=%d""" % pk
cursor.execute(sql)
rows = cursor.fetchone()
print(rows)