发现python数据未插入sqlite数据库错误

Posted

技术标签:

【中文标题】发现python数据未插入sqlite数据库错误【英文标题】:python data not insert into sqlite database errors found 【发布时间】:2020-04-10 06:42:26 【问题描述】:

嗨,我想将数据插入到 sqlite 中,数据不插入。 错误提示

代码中的我的函数

def addstudent(self):

        b2 = self.a2.text()
        b3 = self.a3.text()
        b4 = self.a4.text()
        b5 = self.a5.text()
        b6 = self.a6.text()
        b7 = self.a7.text()
        b8 = self.a8.text()
        b9 = self.a9.itemText(self.a9.currentIndex())
        b10 = self.a10.itemText(self.a10.currentIndex())
        b11 = self.a11.itemText(self.a11.currentIndex())
        b12 = self.a12.text()
        b13 = self.a13.itemText(self.a13.currentIndex())
        b14 = self.a14.itemText(self.a14.currentIndex())
        b15 = self.a15.text()
        b16 = self.a16.itemText(self.a16.currentIndex())
        try:
            conn = sqlite3.connect("collage_App.db")
            dbs = conn.cursor()
            dbs.execute("""INSERT INTO student_registration VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, , ?, ?) """ , (b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16, arrow.now().format('YYYY-MM-DD')) )
            conn.commit()
            conn.close()

            QMessageBox.information(QMessageBox(),'Successful','Student is added successfully into database.')
            self.close()
        except Exception:
            QMessageBox.warning(QMessageBox(), 'Error', 'Could not add student to the database.')

我的按钮事件代码

#Submit Button Clicked
        self.add.clicked.connect(self.addstudent)

【问题讨论】:

欢迎来到 SO!您发布的错误消息是您在自己的代码中编写的错误消息。您将真正的错误隐藏在自己的错误处理后面。删除 try/except 块,您将获得完整的堆栈跟踪,其中包含实际错误的详细信息。 【参考方案1】:

您需要删除 try-except 子句并发布实际错误。

另一方面,您最好使用 Django 来处理数据库。在https://www.djangoproject.com/start/查看他们的教程。它更不容易出错,并为处理模型提供了一个很好的界面。

【讨论】:

我已删除 try catch now 控制台错误 ** 异常“未处理的 sqlite3.IntegrityError”数据类型不匹配文件:C:\Users\ultronhouse11\Documents\Eric6\collageApp\ui\Ui_registerStudent.py,行:第729章 这基本上意味着您提供的数据类型之一与数据库架构要求的不匹配,即数据库要求“City”为字符串类型,而您已为其提供整数.您应该以不同于“a”或“b”的方式命名这些字段,因为以后很难在代码中找到错误。

以上是关于发现python数据未插入sqlite数据库错误的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Sqlite3 Python Django 中的数据库中插入特定数据

SQLITE3插入大量数据,效率翻百倍

SQLite 插入问题 - 错误:没有这样的列

数据未插入 sqlite 数据库

URL 未插入 SQLite3 数据库

python 中向sqlite 插入数据时,使用占位符出现问题