我是python的新手,正在使用pyqt5和sqlite数据库创建gui应用程序。我发现我的程序可以连接数据库,但无法创建表并插入记录,当我按下按钮时显示'RESTART SHELL'< / p>
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
class DBHelper():
def __init__(self):
self.book=sqlite3.connect("Books.db")
self.c=self.book.cursor()
self.c.execute('''CREATE TABLE lib1(
book_id INTEGER PRIMARY KEY AUTO INCREMENT,
title TEXT,
author TEXT,
price INTEGER);''')
self.c.execute("INSERT INTO lib1(book_id,title,author,price)VALUES(1,'python','allan downey',400);")
self.c.execute("INSERT INTO lib1(book_id,title,author,price)VALUES(1,'c complete reference','Herbert Schieldt',400);")
self.c.execute("INSERT INTO lib1(book_id,title,author,price)VALUES(2,'Harry Potter','J.K Rowling',800);")
self.c.execute("INSERT INTO lib1(book_id,title,author,price)VALUES(3,'Half Girlfriend','chetan bhagath',400);")
self.c.execute("INSERT INTO lib1(book_id,title,author,price)VALUES(4,'ponniyin selvan','kalki',400);")
def searchBook(self,title):
self.c.execute("SELECT price FROM lib1 WHERE title="+str(title))
self.data=self.c.fetchone()
if not self.data:
QMessageBox.warning(QMessageBox(),'ERROR! COULD NOT FIND ANY BOOKK WITH THIS TITLE'+str(title1))
self.c.close()
self.book.close()
return self.data
def TotalCost(self,quantity):
self.Total=self.data*self.quantity
self.c.close()
self.book.close()
return self.total
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(Form)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.t1 = QtWidgets.QLineEdit(Form)
self.t1.setObjectName("t1")
self.horizontalLayout.addWidget(self.t1)
self.b1 = QtWidgets.QPushButton(Form)
self.b1.setObjectName("b1")
self.b1.clicked.connect(self.buttonclicked1)
self.horizontalLayout.addWidget(self.b1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setObjectName("label_3")
self.horizontalLayout_3.addWidget(self.label_3)
self.t2 = QtWidgets.QLineEdit(Form)
self.t2.setObjectName("t2")
self.horizontalLayout_3.addWidget(self.t2)
self.b2 = QtWidgets.QPushButton(Form)
self.b2.setObjectName("b2")
self.b2.clicked.connect(self.buttonclicked2)
self.horizontalLayout_3.addWidget(self.b2)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setObjectName("label_4")
self.horizontalLayout_4.addWidget(self.label_4)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "BOOK TITLE:"))
self.b1.setText(_translate("Form", "FIND PRICE"))
self.label_2.setText(_translate("Form", "PRICE:"))
self.label_3.setText(_translate("Form", "QUANTITY:"))
self.b2.setText(_translate("Form", "TOTAL"))
self.label_4.setText(_translate("Form", "TOTAL:"))
def buttonclicked1(self):
self.p1=DBHelper()
self.price=self.t1.text()
self.p1.searchBook(self.price)
def buttonclicked2(self):
self.p2=DBHelper()
self.total=self.t2.text()
self.p2.TotalCost(self.total)
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_())
运行代码后,出现错误消息“ SHOWING RECORDS,RESTART SHELL”。有人可以帮我吗?谢谢