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

问题描述 投票:0回答:1

我正在学习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()
python sqlite pycharm
1个回答
0
投票

因为您删除此行中的表:

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

© www.soinside.com 2019 - 2024. All rights reserved.