import pymysql class 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()