繁体   English   中英

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

[英]How to save Sqlite (python) data after existing Pycharm?

我正在学习 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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM