From 08554667455eff4c99075cb7dc81f04a7f569e9f Mon Sep 17 00:00:00 2001 From: juanboy <2980526980@qq.com> Date: Thu, 4 Jul 2024 12:00:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=88=86=E6=83=85=E5=87=BD=E6=95=B0=E5=AE=9A?= =?UTF-8?q?=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/topicdefine.py | 70 +++++++++++++++++++++++++++++++++++++++ utils/getTopicPageData.py | 2 +- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 model/topicdefine.py diff --git a/model/topicdefine.py b/model/topicdefine.py new file mode 100644 index 0000000..66ad8db --- /dev/null +++ b/model/topicdefine.py @@ -0,0 +1,70 @@ +from utils.query import query +from utils.getPublicData import * +articleList = getAllArticleData() +commentList = getAllCommentsData() + +def column_exists(table_name, column_name): + sql = "SHOW COLUMNS FROM {} LIKE %s".format(table_name) + params = [column_name] + result = query(sql, params, type='select') + return len(result) > 0 +# 添加新的标注列 +def add_label_article(): # 为文章添加标注列 + if not column_exists('article', 'label'): + sql = "ALTER TABLE article ADD COLUMN label TEXT NULL" + params = [] + query(sql, params) + +def add_label_comments(): # 为评论添加标注列 + if not column_exists('comments', 'label'): + sql = "ALTER TABLE comments ADD COLUMN label TEXT NULL" + params = [] + query(sql, params) + +def drop_label():# 删除标注列 + sql = "ALTER TABLE article DROP COLUMN label " + params = [] + query(sql, params) + +def drop_label1():# 删除标注列 + sql = "ALTER TABLE comments DROP COLUMN label " + params = [] + query(sql, params) + + +# 处理数据并添加标注 +def process_data(data): + processed_data =testmodel(data) + return processed_data + +def testmodel(test): + return "wicao" + +def topicdefine(): + label_article=[] + label_comments=[] + for x in articleList: + label_article.append((x[0],process_data(x[5]))) + for x in commentList: + label_comments.append((x[5],process_data(x[4]))) + return label_article,label_comments + +# 更新数据库 +def update_data(label_article,label_comments): + add_label_comments() + add_label_article() + for row in label_article: + id, label = row + sql = "UPDATE article SET label = %s WHERE id = %s" + params = [str(label),str(id)] + query(sql, params) + for row in label_comments: + label, id = row + sql = "UPDATE comments SET label = %s WHERE authorName = %s" + params = [str(label),str(id)] + query(sql, params) + +# x,y=topicdefine() +# update_data(x,y) +drop_label() +drop_label1() \ No newline at end of file diff --git a/utils/getTopicPageData.py b/utils/getTopicPageData.py index 64ddd5e..9ead217 100644 --- a/utils/getTopicPageData.py +++ b/utils/getTopicPageData.py @@ -5,7 +5,7 @@ def getTopicLen(topic):# 统计特定话题下的评论数目 topic_len = 0 for i in commentsList: if i[9] == topic: - topicLen+=1 + topic_len+=1 return topic_len def getTopicPageCreatedAtCharData(topic):# 统计特定话题的评论在每个日期的数量,并返回日期和对应的评论数量