簡體   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