import pymysql
class MYdb:
config = {
"host":"localhost",
"user":"root",
"password":"123456",
"db":"music_project",
"charset":"utf8"
}
def __init__(self):
self.connection = pymysql.connect(**MYdb.config)
self.cursor = self.connection.cursor()
def close(self):
if self.cursor:
self.cuesor.close()
if self.connection:
self.connection.close()
#插入 修改 删除调用
def exeDML(self,sql,*args):
try:
#执行sql
count = self.cursor.execute(sql,args)
#提交事务
self.connection.commit()
return count
except Exception as e:
print(e)
if self.connection:
self.connection.rollback()
finally:
self.close()
def query_one(self,sql,*args):
try:
self.cursor.execute(sql,args)
return self.cursor.fetchone()
except Exception as e:
print(e)
finally:
self.close()
def query_all(self,sql,*args):
try:
self.cursor.execute(sql,args)
return self.cursor.fetchall()
except Exception as e:
print(e)
finally:
self.close()
if __name__ == "__main__":
dbutil = MYdb()
from dbutil import MYdb
class MyService:
def login(self,uname,password):
sql = "select * from t_user where uname = %s and password = %s"
user = MYdb().query_one(sql,uname,password)
if user:
return True
else:
return False

老师这块总是显示这个错误,跟着视频上的代码敲的,麻烦老师看看有什么问题?上次说不要建在子目录下,现在也改过来了。