您的位置:首页 > 编程语言 > Python开发

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

2017-09-03 21:38 441 查看
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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: