在使用Python工作与学习中难免会使用到mysql数据库,使用pymysql三方库可以让我们轻松的对数据库的记录进行操作,如创建、修改,删除表,如增加、删除、修改、查询数据表中的记录,下边记录一下pymysql的使用方法:
# 创建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,还有DictCursor,SSCursor,SSDictCursor,Cursor 返回的结果是以元组的形式返回的,DictCursor,是以字典的形式返回的。DictCursor是继承了Cursor与DictCursorMixin,DictCursorMixin里面只有函数两个方法。(可以选择自己需要的返回形式)
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()
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()
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()
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()
def select(self, pk=None):
with conn.cursor() as cursor:
sql = """ SELECT * from `upload_file`"""
cursor.execute(sql)
rows = cursor.fetchall()
print(rows)
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)