利用Python创建链接mysql数据库个性化对象

来源:互联网 发布:w7 mysql更改密码 编辑:程序博客网 时间:2024/06/15 09:59
import pymysqlclass Sql:    db = None    cursor = None    def __init__(self):        print('connecting to mysql...')        self.db = pymysql.connect("localhost", "root", "root", "edu", charset="utf8")        self.cursor = self.db.cursor()        print('connected!')    def select(self,selectSql):        try:            results = self.cursor.execute(selectSql)            res = self.cursor.fetchmany(results)            return res        except:            print("Select error: unable to fecth data!")    def update(self,updateSql):        try:            self.cursor.execute(updateSql)            self.db.commit()        except:            print("Update error: unable to update data!")    def delete(self,deleteSql):        try:            self.cursor.execute(deleteSql)        except:            print("Delete error:unable to delete data!")    def getCursor(self):        return self.cursor    def closedb(self): # 关闭数据链接        self.db.close()
原创粉丝点击