我正在设置一个函数,以添加将要输入到数据库中8个“物料表”之一上的新数据行。
def Add_New(self):
pref = self.adding.addcode_text.text()
grade = self.adding.addgrade_text.text()
desc = self.adding.adddesc_text.text()
supp = self.adding.addsupp_text.text()
try:
c.execute('INSERT INTO ***auto detected table*** (prefix,grade,desc1,supplier)VALUES(?,?,?,?)',(pref,grade,desc,supp))
conn.commit()
print('DONE')
self.Load_***Auto detected***Db()
except Exception as error:
print(error)
我不确定如何根据用户的“ Pref”条目自动检测将新数据放入哪个表。 Pref是唯一的3位数代码。每个表都有分配的值的范围....即表1的3位代码均以``1''开头。表2中的3位代码均以“ 2”等开头。
我需要if语句来处理吗?如果第一个数字为1,则在数据库中选择表1进行存储?或者我可以通过让它标识currentwidget及其指向的内容来将其指向正确的表?
我会将功能分配给按钮以触发事件。
为新手道歉的问题是我在编程的第一周就开始使用了,并且真的可以使用大家提供的任何帮助。
物料数据库返回到tablewidgets:
import sqlite3
from mcnc import Ui_MainWindow
import mcnclogo_rc
from PyQt5 import QtCore,QtGui
from PyQt5.QtWidgets import QApplication,QMainWindow,QTableWidgetItem
import sys
conn = sqlite3.connect('codes.db')
c = conn.cursor()
c.execute('')
class MainApp(QMainWindow, Ui_MainWindow):
def __init__(self,parent=None):
super(MainApp,self).__init__(parent)
self.setupUi(self)
self.Load_StainlessDb()
self.Load_SteelDb()
self.Load_aluminiumDb()
self.Load_plasticDb()
self.Load_miscDb()
self.Load_duplexDb()
self.Load_consumablesDb()
self.Load_freeIssueDb()
def Load_StainlessDb(self):
content = 'SELECT * FROM stainless'
res = conn.execute(content)
for row_index, row_data in enumerate(res):
self.tableWidget_9.insertRow(row_index)
for column_index, column_data in enumerate(row_data):
self.tableWidget_9.setItem(row_index, column_index,
QTableWidgetItem(str(column_data)))
conn.close
return
def Load_SteelDb(self):
content = 'SELECT * FROM steel'
res = conn.execute(content)
for row_index, row_data in enumerate(res):
self.tableWidget_2.insertRow(row_index)
for column_index, column_data in enumerate(row_data):
self.tableWidget_2.setItem(row_index, column_index,
QTableWidgetItem(str(column_data)))
conn.close
return
def Load_aluminiumDb(self):
content = 'SELECT * FROM aluminium'
res = conn.execute(content)
for row_index, row_data in enumerate(res):
self.tableWidget_3.insertRow(row_index)
for column_index, column_data in enumerate(row_data):
self.tableWidget_3.setItem(row_index, column_index,
QTableWidgetItem(str(column_data)))
conn.close
return
def Load_plasticDb(self):
content = 'SELECT * FROM plastic'
res = conn.execute(content)
for row_index, row_data in enumerate(res):
self.tableWidget_4.insertRow(row_index)
for column_index, column_data in enumerate(row_data):
self.tableWidget_4.setItem(row_index, column_index,
QTableWidgetItem(str(column_data)))
conn.close
return
def Load_miscDb(self):
content = 'SELECT * FROM misc'
res = conn.execute(content)
for row_index, row_data in enumerate(res):
self.tableWidget_5.insertRow(row_index)
for column_index, column_data in enumerate(row_data):
self.tableWidget_5.setItem(row_index, column_index,
QTableWidgetItem(str(column_data)))
conn.close
return
def Load_duplexDb(self):
content = 'SELECT * FROM duplex'
res = conn.execute(content)
for row_index, row_data in enumerate(res):
self.tableWidget_6.insertRow(row_index)
for column_index, column_data in enumerate(row_data):
self.tableWidget_6.setItem(row_index, column_index,
QTableWidgetItem(str(column_data)))
conn.close
return
def Load_consumablesDb(self):
content = 'SELECT * FROM consumables'
res = conn.execute(content)
for row_index, row_data in enumerate(res):
self.tableWidget_7.insertRow(row_index)
for column_index, column_data in enumerate(row_data):
self.tableWidget_7.setItem(row_index, column_index,
QTableWidgetItem(str(column_data)))
conn.close
return
def Load_freeIssueDb(self):
content = 'SELECT * FROM freeIssue'
res = conn.execute(content)
for row_index, row_data in enumerate(res):
self.tableWidget_8.insertRow(row_index)
for column_index, column_data in enumerate(row_data):
self.tableWidget_8.setItem(row_index, column_index,
QTableWidgetItem(str(column_data)))
conn.close
return
def Add_New(self):
pref = self.adding.addcode_text.text()
grade = self.adding.addgrade_text.text()
desc = self.adding.adddesc_text.text()
supp = self.adding.addsupp_text.text()
try:
c.execute('INSERT INTO t
(prefix,grade,desc1,supplier)VALUES(?,?,?,?)',
(pref,grade,desc,supp))
conn.commit()
print('DONE')
self.Load_freeIssueDb()
except Exception as error:
print(error)
if __name__=="__main__":
app = QApplication(sys.argv)
w = MainApp()
w.show()
sys.exit(app.exec_())
这里是代码,对表名进行了必要的更改...
def Add_New(self):
pref = self.adding.addcode_text.text()
grade = self.adding.addgrade_text.text()
desc = self.adding.adddesc_text.text()
supp = self.adding.addsupp_text.text()
tables = {"1": "Stainless", "2": "Steel", "3": "Aluminium", "4":
"Plastic", "5": "misc", "6": "duplex", "8": "consumables", "9":
"freeIssue" }
table = tables[pref[0]]
try:
c.execute('INSERT INTO ' + table + '
(prefix,grade,desc1,supplier)VALUES(?,?,?,?)',(pref,grade,desc,supp))
conn.commit()
print('DONE')
getattr(self, 'Load_' + table + 'Db')()
except Exception as error:
print(error)
这也是buttonWidget connect语句...
self.ui = MainApp()
self.ui.pushButton_3.clicked.connect(self.Add_New)
self.show
这就是它在Visual Studio中对我的显示方式...
答案 0 :(得分:0)
如果只是确定表的第一位数字,则可以使用字典将数字映射到表名称。
调用Load_XXXDb()
方法时,可以使用getattr()
动态获取属性。
def Add_New(self):
pref = self.adding.addcode_text.text()
grade = self.adding.addgrade_text.text()
desc = self.adding.adddesc_text.text()
supp = self.adding.addsupp_text.text()
tables = {"1": "Stainless", "2": "Steel", "3": "Aluminum", ...}
table = tables[pref[0]]
try:
c.execute('INSERT INTO ' + table + ' (prefix,grade,desc1,supplier)VALUES(?,?,?,?)',(pref,grade,desc,supp))
conn.commit()
print('DONE')
getattr(self, 'Load_' + table + 'Db')()
except Exception as error:
print(error)