我试图在表中插入数据,但数据未存储在表中[重复]

Posted

技术标签:

【中文标题】我试图在表中插入数据,但数据未存储在表中[重复]【英文标题】:i tried to insert data in a table but the data is not storing in the table [duplicate] 【发布时间】:2019-09-30 12:55:12 【问题描述】:
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1039, 584)
        self.image_label = QtWidgets.QLabel(Form)
        self.image_label.setGeometry(QtCore.QRect(10, 0, 801, 511))
        self.image_label.setObjectName("image_label")
        self.control_bt = QtWidgets.QPushButton(Form)
        self.control_bt.setGeometry(QtCore.QRect(890, 340, 93, 28))
        self.control_bt.setObjectName("control_bt")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(860, 240, 158, 51))
        self.label_2.setObjectName("label_2")
        self.lineEdit_2 = QtWidgets.QLineEdit(Form)
        self.lineEdit_2.setGeometry(QtCore.QRect(860, 280, 158, 24))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(890, 310, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(880, 230, 113, 22))
        self.lineEdit.setObjectName("lineEdit")
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(910, 210, 55, 16))
        self.label.setObjectName("label")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Face detection"))
        self.image_label.setText(_translate("Form", "                                                 FACE DETECTION"))
        self.control_bt.setText(_translate("Form", "Start"))
        self.label_2.setText(_translate("Form", "    Enter name of person"))
        self.pushButton.setText(_translate("Form", "click to save"))
        self.label.setText(_translate("Form", "enter id"))
        self.pushButton.clicked.connect(self.InsertData)``
    def InsertData(self):
        import sqlite3
        id=self.lineEdit.text()
        name=str(self.lineEdit_2.text())
        conn=sqlite3.connect('B.db')
        curs=conn.cursor()
        curs.execute("INSERT INTO register (id,name) VALUES ('"+id+"','"+name+"');")
        print(id,name)
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

【问题讨论】:

尝试将curs.execute("INSERT INTO register (id,name) VALUES ('"+id+"','"+name+"');")改为curs.execute("INSERT INTO register (id, name) VALUES (?,?)",(id, name))conn.commit() @S.Nick 我试过它不工作 【参考方案1】:

您不需要再次在函数中导入 sqlite3,然后在查询后提交您的连接

conn.commit()

【讨论】:

我试过了,但是没用

以上是关于我试图在表中插入数据,但数据未存储在表中[重复]的主要内容,如果未能解决你的问题,请参考以下文章

在表中存储唯一的图像

在表中插入行的副本并检查重复项

需要在删除和插入模式下将最近 5 个月的数据存储在表中

为mysql中的每个查询结果在表中插入行[重复]

SQL 命令 INSERT 正在工作,但数据未出现在表中

SQL 命令 INSERT 正在工作,但数据未出现在表中