python sql server tkinter_如何在python中使用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.

I think my problem in

def savedata ():

I changed what inside brackets

I tried change this also

c.execute('INSERT INTO data (fname, sname) VALUES (?,?)',

(firstname_entry, secondnamename_entry))

conn.commit()

.

thank for helping

.

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

解决方案

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

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.

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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值