0
是否有任何其他方法可以改进此代码?我正在尝试使用select和update方法创建一个类,然后将其称为将来使用。Python:为SQL创建一个类select和update语句
connect.py
import pymysql
class SQL:
def __init__(self):
self.cnx = pymysql.connect(host...)
self.c = self.cnx.cursor()
def select(self, sql):
self.c.execute(sql)
self.sel = self.c.fetchone()
self.c.close()
self.cnx.close()
return self.sel
def update(self, sql):
self.c.execute(sql)
self.upd = self.cnx.commit()
self.c.close()
self.cnx.close()
return self.upd
from connect import SQL
stmt = "SELECT * FROM name;"
result = SQL().select(stmt)
如何将声明放入课堂? –
这是Android教程,但你可以参考https://developer.android.com/training/basics/data-storage/databases.html。 –