I was building a todo app and used the sql-lite python database for it. I added the clear command and all was going well until I found out when I cleared the database and then tried to view it, my pyqt application just stopped responding. The database code:
def db(self):
title = self.textEdit.toPlainText()
desc = self.textEdit_2.toPlainText()
conn = sqlite3.connect('mydb.db')
cur = conn.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS task
(title text, desc text)
""")
cur.execute("INSERT INTO task VALUES(?,?)", (title, desc))
conn.commit()
cur.close()
conn.close()
the code to view the database [within qt application]
def showdb(self):
conn = sqlite3.connect('mydb.db')
cur = conn.cursor()
str = ''
str2 = ''
self.tableWidget.setRowCount(50)
tableindex = 0
amount = len(cur.execute('SELECT * FROM task').fetchall())
if amount > 0:
for x in cur.execute('SELECT * FROM task'):
str = ''.join(x[0])
str2 = ''.join(x[1])
self.tableWidget.setItem(tableindex, 0, QtWidgets.QTableWidgetItem(str))
self.tableWidget.setItem(tableindex, 1, QtWidgets.QTableWidgetItem(str2))
tableindex = 1
else:
pass
the code to clear the database [it stops to respond after i click it]
def delete(self):
conn = sqlite3.connect('mydb.db')
cur = conn.cursor()
cur.execute('DROP TABLE task')
conn.commit()
CodePudding user response:
The problem is in your delete function, after dropping the database you are not creating it again. This line of code should fix the error.
def delete(self):
conn = sqlite3.connect('mydb.db')
cur = conn.cursor()
cur.execute('DROP TABLE task')
cur.execute("""CREATE TABLE IF NOT EXISTS task
(title text, desc text);
""")
conn.commit()