简体   繁体   中英

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

I'm learning Python and GUI and Sqlite so i wrote this code.

It works till program is running and saves data and deletes them from sqlite, etc...

But when I stop the program and run it again, sqilte is empty.

What is problem?

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()

Because you delete the table in this line:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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