如何在现有Pycharm之后保存Sqlite(python)数据



我正在学习Python、GUI和Sqlite,所以我写了这段代码。

它一直工作到程序运行,保存数据并将其从sqlite等中删除…

但当我停止程序并再次运行时,sqilte为空。

什么是问题?

import tkinter.messagebox
from tkinter import *
import sqlite3
top = tkinter.Tk()
db = sqlite3.connect('DBTest.db')
db.execute('DROP TABLE IF EXISTS text')
#db.execute('CREATE TABLE test3(clmn1 text )')
tkinter.Label(top,bg = 'darkgray')
label1  = tkinter.Label(top, text = 'Write your numbers here :')
text1   = tkinter.Entry(top)
text1.pack(side = tkinter.RIGHT)
label1.pack(side = tkinter.LEFT)
ans = ""
def callBackButton():
ans = (str)(text1.get())
db.execute('INSERT INTO test3 (clmn1) VALUES (?)', [ans])
j = []
def callBackButton2():
j.clear()
cursor = db.execute('SELECT * FROM test3 ORDER BY clmn1')
for row in cursor: j.append(row)
tkinter.messagebox._show('Your answer is :', j)
def callBackButton3():
ans = text1.get()
db.execute('DELETE FROM test3 WHERE clmn1 = ?',(ans,))
button1 = tkinter.Button(top, text = 'Calculate', command = callBackButton, bg = 'orange')
button3 = tkinter.Button(top, text = 'Delete', command = callBackButton3, bg = 'lightblue')
button2 = tkinter.Button(top, text='Show', command=callBackButton2)
button1.pack(side=tkinter.BOTTOM)
button2.pack(side=tkinter.RIGHT)
button3.pack(side=tkinter.RIGHT)
db.commit()
top.mainloop()

因为您删除了此行中的表:

db.execute('DROP TABLE IF EXISTS text')

最新更新