import pymysql
class Mydb():
    def __init__(self):
        try:
            self.conn = pymysql.connect('127.0.0.1','root','密码','库名',charset='utf8')#数据库连接
            # 使用 cursor() 方法创建一个游标对象 cursor
            self.cursor = self.conn.cursor()
        except Exception as e:
            print(e)

    def execute(self,sql,data):
        try:
            # 使用 execute()  方法执行 SQL 查询
            self.cursor.execute(sql,data)
            self.conn.commit()
        except Exception as e:
            self.conn.rollback()
            print(e)
if __name__=='__main__':
    Mydb()

 

转载于:https://www.cnblogs.com/shiweijiang/p/10772959.html

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐