diff --git a/工具v4/Ui_新增基础知识梳理.py b/工具v4/Ui_新增基础知识梳理.py index 92d62985..bc020edf 100644 --- a/工具v4/Ui_新增基础知识梳理.py +++ b/工具v4/Ui_新增基础知识梳理.py @@ -25,23 +25,28 @@ class Ui_Form(object): Form.resize(760, 490) self.label = QLabel(Form) self.label.setObjectName(u"label") - self.label.setGeometry(QRect(200, 160, 91, 16)) + self.label.setGeometry(QRect(120, 140, 91, 16)) self.lineEdit_filepath = QLineEdit(Form) self.lineEdit_filepath.setObjectName(u"lineEdit_filepath") - self.lineEdit_filepath.setGeometry(QRect(200, 180, 381, 20)) + self.lineEdit_filepath.setGeometry(QRect(120, 160, 471, 20)) self.lineEdit_filepath.setReadOnly(True) self.pushButton_selectfilepath = QPushButton(Form) self.pushButton_selectfilepath.setObjectName(u"pushButton_selectfilepath") - self.pushButton_selectfilepath.setGeometry(QRect(494, 150, 91, 24)) + self.pushButton_selectfilepath.setGeometry(QRect(500, 130, 91, 24)) self.label_2 = QLabel(Form) self.label_2.setObjectName(u"label_2") - self.label_2.setGeometry(QRect(200, 210, 291, 51)) + self.label_2.setGeometry(QRect(120, 190, 291, 51)) self.pushButton_exec = QPushButton(Form) self.pushButton_exec.setObjectName(u"pushButton_exec") - self.pushButton_exec.setGeometry(QRect(500, 210, 81, 51)) + self.pushButton_exec.setGeometry(QRect(420, 190, 81, 51)) font = QFont() font.setBold(True) self.pushButton_exec.setFont(font) + self.pushButton_commit = QPushButton(Form) + self.pushButton_commit.setObjectName(u"pushButton_commit") + self.pushButton_commit.setGeometry(QRect(510, 190, 81, 51)) + self.pushButton_commit.setFont(font) + self.pushButton_commit.setStyleSheet(u"background-color: rgb(255, 255, 127);") self.retranslateUi(Form) @@ -55,6 +60,8 @@ class Ui_Form(object): self.label_2.setText(QCoreApplication.translate("Form", u"LaTeX\u6587\u4ef6\u4e2d\u6709\u4e00\u4e2aenumerate\u73af\u5883.\n" "\u6bcf\u4e00\u4e2aitem\u540e\u9762\u6709\u4e00\u4e2a\u4e2d\u62ec\u53f7\u5305\u56f4\u7684\u4fe1\u606f, \u4e4b\u540e\u662f\u5185\u5bb9.\n" "\u4f8b\u5982: \\item [K0101/K0101001B,K0101002B] \u5185\u5bb9", None)) - self.pushButton_exec.setText(QCoreApplication.translate("Form", u"\u6dfb\u52a0\u57fa\u7840\u77e5\u8bc6", None)) + self.pushButton_exec.setText(QCoreApplication.translate("Form", u"\u9884\u5bfc\u5165\n" +"\u57fa\u7840\u77e5\u8bc6", None)) + self.pushButton_commit.setText(QCoreApplication.translate("Form", u"\u63d0\u4ea4\u81f3\u6570\u636e\u5e93", None)) # retranslateUi diff --git a/工具v4/新增基础知识梳理.py b/工具v4/新增基础知识梳理.py index f08cb940..2e82f214 100644 --- a/工具v4/新增基础知识梳理.py +++ b/工具v4/新增基础知识梳理.py @@ -14,49 +14,74 @@ class MyWindow_tjjc(QWidget,Ui_Form): self.database_name = string # print(self.database_name) def bind(self): + self.errorflag = True self.pushButton_exec.setDisabled(True) self.pushButton_selectfilepath.clicked.connect(self.selectfilepath) - self.pushButton_exec.clicked.connect(self.exec) + self.pushButton_exec.clicked.connect(self.execstep1) + self.pushButton_commit.clicked.connect(self.commitstep) + self.pushButton_commit.setDisabled(True) + self.mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name) + def selectfilepath(self): self.filepath = QFileDialog.getOpenFileName(self,"选择.tex文件",os.getcwd(),"tex文件(*.tex);;所有文件(*)")[0] self.lineEdit_filepath.setText(self.filepath) self.pushButton_exec.setEnabled(True) - def exec(self): - mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name) - mycursor = mydb.cursor() + def execstep1(self): + mycursor = self.mydb.cursor() mycursor.execute("SELECT bn_id FROM basic_knowledges;") bnids = [ret[0] for ret in mycursor.fetchall()] bkdata_raw = ReadTextFile(self.filepath) bkdatabody = re.findall(r"\\begin\{enumerate\}([\s\S]*?)\\end\{enumerate\}",bkdata_raw)[0].strip().split("\\item") bkdata = [line.strip() for line in bkdatabody if "[" in line and "]" in line] + print(f"目前数据库中基础知识编号的最大值: {int(max(bnids)[1:])}") currentid = int(max(bnids)[1:])+1 - - - - for line in bkdata: - if not line.strip() == "": - bnid = f"B{str(currentid).zfill(5)}" - pos = line.index("]") - head = line[1:pos] - content = line[pos+1:].strip() - lesson,objs = head.split("/") - obj_list = objs.split(",") - print(f"{bnid} {obj_list} {content}") - sql = "INSERT INTO basic_knowledges (bn_id,bn_content) VALUE (%s,%s);" - val = (bnid,content) - mycursor.execute(sql,val) - for objid in obj_list: - sql = "INSERT INTO bn_obj_corresp (bn_id,obj_id) VALUE (%s,%s);" - val = (bnid,objid) + self.errorflag = False + try: + for line in bkdata: + if not line.strip() == "": + bnid = f"B{str(currentid).zfill(5)}" + pos = line.index("]") + head = line[1:pos] + content = line[pos+1:].strip() + lesson,objs = head.split("/") + obj_list = objs.split(",") + print(f"{bnid} {obj_list} {content}") + sql = "INSERT INTO basic_knowledges (bn_id,bn_content) VALUE (%s,%s);" + val = (bnid,content) mycursor.execute(sql,val) - sql = "INSERT INTO logs (DATE,TIME,username,action,db_content) VALUE (%s,%s,%s,%s,%s);" - val = (GetDate(),GetTime(),get_git_username(),"新增基础",f"{bnid} {obj_list} {content}") - mycursor.execute(sql,val) - currentid += 1 - mydb.commit() - mydb.close() - print("已录入完成") - + print(f"已导入基础知识 编号: {bnid},\n 内容: {content}.") + for objid in obj_list: + sql = "SELECT * FROM lessonobj WHERE objid = %s;" + val = (objid,) + mycursor.execute(sql,val) + ret =mycursor.fetchall() + if ret == []: + print(f"!!!目标编号: {objid} 有误") + raise ValueError + else: + print(f"对应目标编号 {objid}, 目标内容 {ret[0][1]}") + sql = "INSERT INTO bn_obj_corresp (bn_id,obj_id) VALUE (%s,%s);" + val = (bnid,objid) + mycursor.execute(sql,val) + sql = "INSERT INTO logs (DATE,TIME,username,action,db_content) VALUE (%s,%s,%s,%s,%s);" + val = (GetDate(),GetTime(),get_git_username(),"新增基础",f"{bnid} {obj_list} {content}") + mycursor.execute(sql,val) + currentid += 1 + print("---------- ---------- ----------") + self.pushButton_commit.setEnabled(True) + except Exception as e: + print(f"错误: {type(e).__name__}") + print(f"内容为 {line} 的输入有误.") + self.errorflag = True + self.mydb.rollback() + print(f"数据库 {self.database_name} 已回滚") + + def commitstep(self): + if self.errorflag == False: + self.mydb.commit() + self.mydb.close() + print(f"提交至数据库 {self.database_name} 已完成.") + self.pushButton_commit.setDisabled(True) diff --git a/工具v4/新增基础知识梳理.ui b/工具v4/新增基础知识梳理.ui index 77161e74..bf1a18d7 100644 --- a/工具v4/新增基础知识梳理.ui +++ b/工具v4/新增基础知识梳理.ui @@ -16,8 +16,8 @@ - 200 - 160 + 120 + 140 91 16 @@ -29,9 +29,9 @@ - 200 - 180 - 381 + 120 + 160 + 471 20 @@ -42,8 +42,8 @@ - 494 - 150 + 500 + 130 91 24 @@ -55,8 +55,8 @@ - 200 - 210 + 120 + 190 291 51 @@ -70,8 +70,8 @@ - 500 - 210 + 420 + 190 81 51 @@ -82,7 +82,29 @@ - 添加基础知识 + 预导入 +基础知识 + + + + + + 510 + 190 + 81 + 51 + + + + + true + + + + background-color: rgb(255, 255, 127); + + + 提交至数据库