pymysql运行自定义语句
import pymysql pymysql.install_as_MySQLdb() host = '127.0.0.1' user = 'user ' password = 'password ' port = 3306 database = 'database' conn_database = { 'host':host, 'user':user, 'password':password, 'port':port, 'database':database } def select_datas(sql): with pymysql.connect(**conn_database) as connection: with connection.cursor() as cursor: cursor.execute(sql) columns = [col[0] for col in cursor.description] datas = [dict(zip(columns, row)) for row in cursor.fetchall()] return datas def insert_datas(sql): with pymysql.connect(**conn_database) as connection: with connection.cursor() as cursor: cursor.execute(sql) issucc = cursor.rowcount if issucc > 0: return True else: return False def update_datas(sql): with pymysql.connect(**conn_database) as connection: with connection.cursor() as cursor: cursor.execute(sql) issucc = cursor.rowcount if issucc > 0: return True else: return False def delete_datas(sql): with pymysql.connect(**conn_database) as connection: with connection.cursor() as cursor: cursor.execute(sql) issucc = cursor.rowcount if issucc > 0: return True else: return False
如需要添加自动提交
autocommit = True
本文来自博客园,作者:CodeYaSuo,转载请注明原文链接:https://www.cnblogs.com/hany-postq473111315/p/16422432.html