如何在python中使用tkinter和sqlite [英] how to use tkinter with sqlite in python

查看:558
本文介绍了如何在python中使用tkinter和sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将tkinter条目传递给sqlite
时遇到了问题,我的目标是建立用户界面来收集数据,并删除,显示,更新
,我将继续学习。

I had issues with delivering tkinter entry to sqlite My goal is build user interface to collect data and delete, show, update, I will keep learning.

我认为我的问题

def savedata ():

我更改了括号内的内容

我也尝试更改了此内容

c.execute('INSERT INTO data (fname, sname) VALUES (?,?)', 
(firstname_entry, secondnamename_entry))
conn.commit()

感谢帮助

import random 
import tkinter as tk 
from tkinter import * 
from tkinter import messagebox 
import sqlite3 

def conacona(): 
    conn = sqlite3.connect('student.db') 
    c = conn.cursor() 
    c.execute("CREATE TABLE IF NOT EXISTS stud (firstname TEXT, secondname TEXT)") 
    conn.commit() 
    conn.close() 

#oooooooo 

main_menu = tk.Tk() 

firstname_label = Label(main_menu, text="First name") 
firstname_label.pack() 
secondname_label = Label(main_menu, text="Second name") 
secondname_label.pack() 

# First name get 
firstname_entry = tk.StringVar() 
firstname_entry_entry = Entry(main_menu, textvariable = fn_ent_ent) 
firstname_entry_entry.pack() 

# Second name get 
secondname_entry = tk.StringVar() 
secondname_entry_entry = Entry(main_menu, textvariable = sn_ent_ent) 
secondname_entry_entry.pack() 

def savedata (): 
    conn = sqlite3.connect('stud.db') 
    c = conn.cursor() 
    c.execute('INSERT INTO data (fname, sname) VALUES (?,?)', (firstname_entry, secondnamename_entry)) 
    conn.commit() 
    print("OK") 

u_ent_btn = Button(text="Enter",command=savedata()) 
u_ent_btn.pack() 

main_menu.mainloop() 


推荐答案

fn_ent_ent 替换为 firstent_entry sn_ent_ent secondname_entry 来引用您创建的字符串变量。

Replace fn_ent_ent with firstname_entry and sn_ent_ent with secondname_entry to refer to the string variables that you created.

execute()语句中有一个错字:应该是 secondname_entry ,而不是 secondnamename_entry 。另外,您还需要在字符串变量上调用 .get()来检索要在查询中使用的值。

There is a typo in the execute() statement: it should be secondname_entry, not secondnamename_entry. Also you need to call .get() on the string variables to retrieve the value to be used in the query.

SQL语句必须引用创建表时使用的正确表名和列名,即 stud 而不是 data 名字第二名而不是 fname 名称

The SQL statement must reference the correct table and column names that were used when the table was created namely stud instead of data, andfirstname and secondname instead of fname and sname.

c.execute('INSERT INTO stud (firstname, secondname) VALUES (?,?)', (firstname_entry.get(), secondname_entry.get())) 

将其作为按钮命令的函数传递时,请勿调用 savedata()

Do not call savedata() when passing it as the function for the button command:

u_ent_btn = Button(text="Enter",command=savedata)

最后,在进入 mainloop之前,需要调用 conacona()创建SQLite数据库。 ()。并且必须为数据库使用相同的文件名,因此将其设为 stud.db student.db 之一

Finally, you need to call conacona() to create the SQLite database before entering the mainloop(). And you must use the same file name for the database, so make it one of stud.db or student.db but not both.

将所有内容放在一起会得到以下代码:

Putting all of that together results in this code:

import random
import tkinter as tk
from tkinter import *
from tkinter import messagebox
import sqlite3

def conacona():
    conn = sqlite3.connect('student.db')
    c = conn.cursor()
    c.execute("CREATE TABLE IF NOT EXISTS stud (firstname TEXT, secondname TEXT)")
    conn.commit()
    conn.close()

#oooooooo 

main_menu = tk.Tk()

firstname_label = Label(main_menu, text="First name")
firstname_label.pack()
secondname_label = Label(main_menu, text="Second name")
secondname_label.pack()

# First name get 
firstname_entry = tk.StringVar()
firstname_entry_entry = Entry(main_menu, textvariable=firstname_entry)
firstname_entry_entry.pack()

# Second name get 
secondname_entry = tk.StringVar()
secondname_entry_entry = Entry(main_menu, textvariable=secondname_entry)
secondname_entry_entry.pack()

def savedata ():
    print(dir(firstname_entry))
    conn = sqlite3.connect('student.db')
    c = conn.cursor()
    c.execute('INSERT INTO stud (firstname, secondname) VALUES (?,?)', (firstname_entry.get(), secondname_entry.get()))
    conn.commit()
    print("OK")

u_ent_btn = Button(text="Enter",command=savedata)
u_ent_btn.pack()

conacona()
main_menu.mainloop()

这篇关于如何在python中使用tkinter和sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆