我的代码有问题。我正在制作一个窗口应用程序。现在,我需要将数据插入数据库中的表,然后将其显示给我(使用SELECT)。我有两个字段来输入用户的名称和密码(ID是根据表中的条目数计算的)和一个触发输入的按钮。然后我有一个字段,我希望在插入后显示确认信息,以及从表格中触发“SELECT”的按钮。应用程序确实启动,但据我所知,它不会将其添加到表中(并且不会显示任何内容作为确认)。这是我的代码:
from tkinter import *
import sqlite3 as lite
import sys
from Crypto.Cipher import AES
con = None
con = lite.connect('test.db')
cur = con.cursor()
class Application(Frame):
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.inst_lbl = Label(self, text = "Dodaj uzytkownika")
self.inst_lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W)
self.pwa = Label(self, text = "Login")
self.pwa.grid(row = 1, column = 0, sticky = W)
self.pwb = Label(self, text = "Haslo")
self.pwb.grid(row = 2, column = 0, sticky = W)
self.a = Entry(self)
self.a.grid(row = 1, column = 1, sticky = W)
self.b = Entry(self)
self.b.grid(row = 2, column = 1, sticky = W)
self.submit_bttn = Button(self, text = "Dodaj uzytkownika", command = self.dodaj)
self.submit_bttn.grid(row = 3, column = 0, sticky = W)
self.secret_txt = Text(self, width = 35, height = 5, wrap = WORD)
self.secret_txt.grid(row = 4, column = 0, columnspan = 2, sticky = W)
self.view_users = Button(self, text = "Wyswietl uzytkownikow", command = self.wyswietl)
self.view_users.grid(row = 10, column = 0, sticky = W)
self.users_window = Text(self, width = 35, height = 5, wrap = WORD)
self.users_window.grid(row = 11, column = 0, columnspan = 2, sticky = W)
def dodaj(self):
a = self.a.get()
b = self.b.get()
index = cur.execute("SELECT COUNT(Id) FROM users")
cur.execute("INSERT INTO users (Id, Nazwa, Haslo) VALUES(?,?,?)", (int(index),str(a),str(b)))
#obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
#ciphertext = obj.encrypt(test)
self.secret_txt.delete(0.0, END)
self.secret_txt.insert(0.0, (index, a, b))
def wyswietl(self):
viewall = cur.execute("SELECT * FROM users")
self.users_window.delete(0.0, END)
self.users_window.insert(0.0, viewall)
root = Tk()
root.title("ShopQIP")
root.geometry("1024x700")
#root.mainloop()
app = Application(root)
root.mainloop()
当我尝试查看表格的内容时,我在该字段中收到错误:
sqlite3.Cursor对象位于0x02A6E220
此外,代码运行,但运行时出现此错误:
* Tkinter回调中的例外情况
追溯(最近的呼叫最后):
文件“C:\ Users \ kjubus \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ tkinter__init __。py”,第1549行,调用
return self.func( args)
文件“C:\ Users \ kjubus \ Dysk Google!Szkoła\ MGR \ Projekt inzynierski \ inzynierka v0.1.py”,第52行,在dodaj中
cur.execute(“INSERT INTO users(Id,Nazwa,Haslo)VALUES(?,?,?)”,(int(index),str(a),str(b)))
TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'sqlite3.Cursor'
并且只是为了澄清 - 在调用之前和之后的这部分“1549行”中,有2个下划线。
答案 0 :(得分:0)
__call__
是用于调用对象和函数的内部方法。你可以忽略它。
游标的execute
方法通常返回一些无关的值,如受影响的行数,并通过单独调用其fetch*
方法之一返回结果。所以你可能需要像
cur.execute("SELECT COUNT(Id) FROM users")
index = cur.fetchone()[0] # First (and only) element in the single row
cur.execute("INSERT INTO users (Id, Nazwa, Haslo) VALUES(?,?,?)", (int(index),str(a),str(b)))