diff --git a/backend/api/db.py b/backend/api/db.py index e12437639b679b9e0b2dbd256802b8aeb8356354..21b1feafe54e486ad5c647df3a6c476fb5d2ba9d 100644 --- a/backend/api/db.py +++ b/backend/api/db.py @@ -1,4 +1,5 @@ import pymysql +import threading class DB: """ @@ -10,9 +11,9 @@ class DB: """ self.db = pymysql.connect( user='root', - password='123456', + password='Zyl20020128', host='localhost', - db='软工管') + db='Digout') self.cursor = self.db.cursor(pymysql.cursors.DictCursor) def execute(self, command) -> list: """ @@ -85,8 +86,13 @@ class Database(DB): :param course: 课程名 :return: 查询结果 """ + self.db.ping() command = "select course, time from article where uploader = '%s' order by time desc limit 2" % username - return self.execute(command) + lock = threading.Lock() + lock.acquire() + res = self.execute(command) + lock.release() + return res def query_course_comments(self, course) -> list: """ 查询特定课程的所有评论 @@ -111,7 +117,11 @@ class Database(DB): """ self.db.ping() command = "select count(id) from comment where commenter = '%s'" % commenter - return self.execute(command) + lock = threading.Lock() + lock.acquire() + res = self.execute(command) + lock.release() + return res def query_user_comment_likes_number(self, commenter) -> str: """ 查询特定用户的所有评论 @@ -120,7 +130,11 @@ class Database(DB): """ self.db.ping() command = "select sum(likes) from comment where commenter = '%s'" % commenter - return self.execute(command) + lock = threading.Lock() + lock.acquire() + res = self.execute(command) + lock.release() + return res def query_article_scores(self, id) -> list: """ 查询特定文章的评分