from PySide6.QtWidgets import QWidget, QApplication, QFileDialog, QTableWidgetItem from Ui_删除课时目标关系 import Ui_Form from database_tools_2 import * class MyWindow_scgx(QWidget,Ui_Form): def __init__(self,database_name): super().__init__() self.database_name = database_name self.current_database_name = database_name self.setupUi(self) self.bind() def setdbname(self,string): self.database_name = string self.pushButton_del.setDisabled(True) self.pushButton_commit.setDisabled(True) try: self.db.close() except: pass self.db = connect(hostname=db_host, port=db_port, username= db_user, pwd= db_pwd, db=self.database_name) # print(self.database_name) def bind(self): self.tableWidget_prec.setColumnWidth(0,20) self.tableWidget_prec.setColumnWidth(1,100) self.tableWidget_prec.setColumnWidth(2,300) self.tableWidget_post.setColumnWidth(0,20) self.tableWidget_post.setColumnWidth(1,100) self.tableWidget_post.setColumnWidth(2,300) self.pushButton_del.setDisabled(True) self.pushButton_commit.setDisabled(True) self.pushButton_getobj.clicked.connect(self.getobj) self.pushButton_del.clicked.connect(self.delete) self.pushButton_commit.clicked.connect(self.commit) self.lineEdit_todelete.textChanged.connect(self.enable_del) self.db = connect(hostname=db_host, port=db_port, username= db_user, pwd= db_pwd, db=self.database_name) self.mycursor = self.db.cursor() self.mycursor.execute("SELECT objid,obj_content FROM lessonobj WHERE not obsolete;") ret_list = self.mycursor.fetchall() self.allobjs = "" for ret in ret_list: self.allobjs += f"{ret[0]}\t{ret[1]}\n" self.plainTextEdit_objs.setPlainText(self.allobjs) def getobj(self): objid = self.lineEdit_objid.text().strip() self.mycursor.execute("SELECT objid,obj_content FROM lessonobj WHERE objid = %s AND not obsolete;",(objid,)) ret_list = self.mycursor.fetchall() if len(ret_list) == 0 or len(ret_list) >= 2: self.plainTextEdit_objs.setPlainText("目标编号有误, 请重试!!!\n\n"+self.allobjs) else: self.objid,self.objcontent = ret_list[0] self.plainTextEdit_objs.setPlainText(f"{self.objid}\t{self.objcontent}") count = 0 self.corresp_list = [] self.tableWidget_prec.clearContents() self.tableWidget_prec.setRowCount(0) self.tableWidget_post.clearContents() self.tableWidget_post.setRowCount(0) self.mycursor.execute("SELECT inherit_from FROM objinherit WHERE objid = %s AND not obsolete;",(self.objid,)) ret_list = self.mycursor.fetchall() for ret in ret_list: # print(ret) count += 1 self.mycursor.execute("SELECT obj_content FROM lessonobj WHERE objid = %s AND not obsolete;",(ret[0],)) prec_content = self.mycursor.fetchall()[0][0] self.tableWidget_prec.insertRow(count-1) self.tableWidget_prec.setItem(count-1,0,QTableWidgetItem(str(count))) self.tableWidget_prec.setItem(count-1,1,QTableWidgetItem(ret[0])) self.tableWidget_prec.setItem(count-1,2,QTableWidgetItem(prec_content)) self.corresp_list.append((count,self.objid,ret[0])) prec_count = count self.mycursor.execute("SELECT objid FROM objinherit WHERE inherit_from = %s AND not obsolete;",(self.objid,)) ret_list = self.mycursor.fetchall() count = 0 for ret in ret_list: # print(ret) count += 1 self.mycursor.execute("SELECT obj_content FROM lessonobj WHERE objid = %s AND not obsolete;",(ret[0],)) post_content = self.mycursor.fetchall()[0][0] self.tableWidget_post.insertRow(count-1) self.tableWidget_post.setItem(count-1,0,QTableWidgetItem(str(count+prec_count))) self.tableWidget_post.setItem(count-1,1,QTableWidgetItem(ret[0])) self.tableWidget_post.setItem(count-1,2,QTableWidgetItem(post_content)) self.corresp_list.append((count+prec_count,ret[0],self.objid)) def enable_del(self): self.pushButton_del.setEnabled(True) def delete(self): self.tableWidget_prec.clearContents() self.tableWidget_prec.setRowCount(0) self.tableWidget_post.clearContents() self.tableWidget_post.setRowCount(0) index = int(self.lineEdit_todelete.text().strip()) for item in self.corresp_list: if item[0] == index: objid = item[1] inherit_from = item[2] break self.mycursor = self.db.cursor() self.mycursor.execute("UPDATE objinherit SET obsolete = TRUE WHERE objid = %s AND inherit_from = %s;",(objid,inherit_from)) sql = "INSERT INTO logs (DATE,TIME,username,action,db_content) VALUE (%s,%s,%s,%s,%s);" val = (GetDate(),GetTime(),get_git_username(),f"删除目标关联",f"{objid} <- {inherit_from}") self.mycursor.execute(sql,val) self.pushButton_commit.setEnabled(True) def commit(self): self.db.commit() self.pushButton_del.setDisabled(True) self.pushButton_commit.setDisabled(True) if __name__ == '__main__': app = QApplication([]) windows = MyWindow_scgx("tikutest") windows.show() app.exec()