# coding=utf-8 import pymysql class Mysql_insert(): def __init__(self): try: self.conn = pymysql.connect(host="192.168.1.86", user="root", password="pwd123", db="TEST", charset="utf8") self.cursor = self.conn.cursor() except pymysql.err as e: print("error" % e) def insert_one(self): try: sql = ("insert into api_test value(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)") # 插入数据 self.cursor.execute( sql, (7, 'login', 'http://api.9dcj.com.cn/Api/TAccount/login', 1, 'post', 0, '', '', '', '', '', 'status:0') ) # 提交事务 self.conn.commit() except: print("error") # 回滚 self.conn.rollback() # 关闭cursor链接 self.cursor.close() if __name__ == "__main__": opp = Mysql_insert() opp.insert_one()