我正在开发一个带有登录名和信息表的python接口,但是我发现了一个阻碍我移动的障碍
我已尝试使用户能够修改(或更新)数据表。起初,我认为我只需要为用户提供行id和所选列就可以更新数据,但不幸的是,由于某种原因,我尝试的命令都不起作用
以下是我最后一次失败的尝试:
def changeconfirmed():
escolha = entry.get()
entrada = entry2.get()
Id = entry3.get()
if escolha == "Nome":
funcao = entrada, Id
banco = ("""
UPDATE Navio SET Nome = %s WHERE Id = %s
""")
Banco.cursor.execute(banco, funcao)
Banco.conn.commit()
print("update successfully")
有些单词是葡萄牙语的,因为我是巴西人,所以更容易将它们用作指示语。
我尝试使用几种形式的SQL UPDATE命令,但都不起作用,尤其是使用“%s”的,解释器说它无法识别“%”。不幸的是,由于数据输入和删除工作正常(WHERE命令也无法识别),这就是我无法完成接口的全部原因
如何使用用户输入的数据更新表
完整代码:
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
import tkinter as tk
import Banco
import sqlite3
import random
def update():
l.config(text=str(random.random()))
jan.after(1000, update)
jan = Tk()
jan.title("RESTRITO")
jan.geometry("500x405")
jan.configure(background="#585858", highlightcolor="#cfc91c", highlightbackground="#cfc91c", highlightthickness=10)
jan.resizable(width=False, height=False)
# mapa
LeftFrame = Frame(jan, width=310, height=120, bg="#cfc91c")
LeftFrame.place(x=85, y=50)
label1Frame = Frame(jan, width=310, height=50, bg="#cfc91c")
label1Frame.place(x=85, y=180)
label2Frame = Frame(jan, width=310, height=50, bg="#cfc91c")
label2Frame.place(x=85, y=220)
buttonFrame = Frame(jan, width=200, height=50, bg="#cfc91c")
buttonFrame.place(x=140, y=280)
# termina aqui mapa
# titulo e texto
Acessolabel = Label(LeftFrame, text="Acesso Restrito", font=("Arial", 20, 'bold'), bg="#cfc91c", fg="#585858")
Acessolabel.place(x=3, y=10)
texto1label = Label(LeftFrame, text="Este programa é de uso restrito ", font=("Arial", 14), bg="#cfc91c", fg="#585858")
texto1label.place(x=3, y=50)
texto2label = Label(LeftFrame, text="para somente pessoas autorizadas.", font=("Arial", 14), bg="#cfc91c", fg="#585858")
texto2label.place(x=3, y=80)
# temina aqui titulo e texto
# usuario e senha entrada
userLabel1 = Label(label1Frame, text="Usuário:", font=("Arial", 16, 'bold'), bg="#cfc91c", fg="#585858")
userLabel1.place(x=17, y=10)
userLabel2 = Label(label2Frame, text="Senha:", font=("Arial", 16, 'bold'), bg="#cfc91c", fg="#585858")
userLabel2.place(x=18, y=8)
userUsuario = ttk.Entry(label1Frame, width=30)
userUsuario.place(x=105, y=13)
userSenha = ttk.Entry(label2Frame, width=30, show="*")
userSenha.place(x=105, y=11)
# termina aqui usuario e senha entrada
# login comando
def login():
user = userUsuario.get()
password = userSenha.get()
Banco.cursor.execute("""
SELECT * FROM Users
WHERE User = ? and Password = ?
""", (user, password))
print("Clicou!")
verifylogin = Banco.cursor.fetchone()
try:
if user in verifylogin and password in verifylogin:
print("Parabéns!")
conn = sqlite3.connect("UserData.db")
cursor = conn.cursor()
# SQL
sql2 = "SELECT TOX, Quantidade, QDM FROM TOXICO"
cursor.execute(sql2)
rows2 = cursor.fetchall()
total1 = cursor.rowcount
print("number entry's table:" + str(total1))
sql = "SELECT Id, Nome, Idade, Situação FROM Navio"
cursor.execute(sql)
rows = cursor.fetchall()
total2 = cursor.rowcount
print("number entry's:"+str(total2))
# movendo tela de login
LeftFrame.place(x=501)
label1Frame.place(x=501)
label2Frame.place(x=501)
buttonFrame.place(x=501)
# termina aqui movendo tela de login
# mapa
topFrame = Frame(jan, width=440, height=100, bg="#cfc91c", relief="raise")
topFrame.place(x=20, y=20)
topFrame2 = Frame(jan, width=440, height=50, bg="#cfc91c", relief="raise")
topFrame2.place(x=20, y=90)
centerframe = Frame(jan, width=150, height=227, bg="#cfc91c", relief="raise")
centerframe.place(x=20, y=140)
centerframe2 = Frame(jan, width=290, height=227, bg="#cfc91c", relief="raise")
centerframe2.place(x=170, y=140)
# termina aqui mapa
# labels
titulolabel = Label(topFrame, text="Sistema de Informações", font=("Arial", 20, 'bold'), bg="#cfc91c",
fg="#585858")
titulolabel.place(x=55, y=25)
# termina aqui labels
# "#585858" "#cfc91c"
# funções
def Return():
# removendo tela de informações
topFrame.place(x=501)
topFrame2.place(x=501)
centerframe.place(x=501)
centerframe2.place(x=501)
fecharb.place(x=501)
# voltando a tela de login
LeftFrame.place(x=85)
label1Frame.place(x=85)
label2Frame.place(x=85)
buttonFrame.place(x=140)
def trip():
# botões
botao3.place(x=15, y=15)
botao4.place(x=15, y=55)
botao5.place(x=15, y=95)
# termina aqui botões
# puxar tabela TRIPULAÇÃO
tree1.place(x=0)
# termina aqui puxar tabela TRIPULAÇÃO
# remover tabela TOXICO
tree2.place(x=500)
# termina aqui remover tabela TOXICO
# esconder
entry.place(x=401)
entry1.place(x=401)
entry2.place(x=401)
entry3.place(x=401)
entry4.place(x=441)
botao6.place(x=401)
botao7.place(x=401)
botao8.place(x=401)
botao61.place(x=401)
botao71.place(x=401)
botao81.place(x=401)
insert.place(x=401)
change.place(x=401)
excluirtrash.place(x=401)
labelid.place(x=401)
labelid2.place(x=401)
labelid3.place(x=441)
labelid5.place(x=401)
labelid11.place(x=401)
labelid21.place(x=401)
labelid41.place(x=401)
MT.place(x=401)
QT.place(x=401)
QDM.place(x=441)
botao61.place(x=401)
botao71.place(x=401)
botao81.place(x=401)
def tox():
# botões
insert.place(x=15, y=15)
change.place(x=15, y=55)
excluirtrash.place(x=15, y=95)
# termina aqui botões
# puxar tabela TOXICO
tree2.place(x=0)
# termina aqui puxar tabela TOXICO
# remover tabela TRIPULAÇÃO
tree1.place(x=500)
# termina aqui remover tabela TRIPULAÇÃO
# esconder
entry.place(x=401)
entry1.place(x=401)
entry2.place(x=401)
entry3.place(x=401)
entry4.place(x=441)
botao6.place(x=401)
botao7.place(x=401)
botao8.place(x=401)
botao61.place(x=401)
botao71.place(x=401)
botao81.place(x=401)
labelid.place(x=401)
labelid2.place(x=401)
labelid3.place(x=441)
labelid5.place(x=401)
labelid11.place(x=401)
labelid21.place(x=401)
labelid41.place(x=401)
MT.place(x=401)
QT.place(x=401)
QDM.place(x=441)
botao61.place(x=401)
botao71.place(x=401)
botao81.place(x=401)
def insert():
botao6.place(x=80, y=15)
entry.place(x=15, y=150)
entry2.place(x=15, y=195)
entry4.place(x=285, y=12)
labelid.place(x=15, y=125)
labelid2.place(x=15, y=170)
labelid3.place(x=220, y=12)
# esconder
botao7.place(x=401)
botao8.place(x=401)
entry1.place(x=401)
entry3.place(x=401)
labelid5.place(x=401)
labelid11.place(x=401)
labelid21.place(x=401)
labelid41.place(x=401)
botao61.place(x=401)
botao71.place(x=401)
botao81.place(x=401)
MT.place(x=401)
QT.place(x=401)
QDM.place(x=441)
def insertconfirmed():
entrada2 = entry2.get()
entrada = entry.get()
entrada3 = entry4.get()
banco = """INSERT INTO Navio(Nome, idade, Situação) VALUES(?, ?, ?)"""
entrada1 = entrada, entrada2, entrada3
cursor.execute(banco, entrada1)
conn.commit()
print("deu certo nome!")
def change():
botao7.place(x=80, y=55)
entry.place(x=15, y=150)
entry2.place(x=15, y=195)
entry3.place(x=110, y=95)
labelid5.place(x=80, y=95)
labelid11.place(x=15, y=125)
labelid21.place(x=15, y=170)
# esconder
botao6.place(x=401)
botao8.place(x=401)
botao61.place(x=401)
botao71.place(x=401)
botao81.place(x=401)
labelid41.place(x=401)
labelid.place(x=401)
labelid2.place(x=401)
labelid3.place(x=441)
MT.place(x=401)
QT.place(x=401)
QDM.place(x=441)
entry1.place(x=401)
entry4.place(x=441)
def changeconfirmed():
escolha = entry.get()
entrada = entry2.get()
Id = entry3.get()
if escolha == "Nome":
funcao = entrada, Id
banco = ("""
UPDATE Navio SET Nome = %s WHERE Id = %s
""", funcao)
Banco.cursor.execute(banco, funcao)
Banco.conn.commit()
print("update successfully")
elif escolha == "Idade":
banco = "Update Navio set Idade = %s where Id = %s"
funcao = entrada, Id
cursor.execute(banco, funcao)
Banco.conn.commit()
print("deu certo idade!")
elif escolha == "Situação":
banco = "Update Navio set Situação = %s where Id = %s"
funcao = entrada, Id
cursor.execute(banco, funcao)
Banco.conn.commit()
print("deu certo situação!")
def trash():
botao8.place(x=80, y=95)
entry1.place(x=15, y=150)
labelid41.place(x=15, y=125)
# esconder
botao7.place(x=401)
botao6.place(x=401)
botao61.place(x=401)
botao71.place(x=401)
botao81.place(x=401)
entry.place(x=401)
entry2.place(x=401)
entry3.place(x=401)
entry4.place(x=441)
labelid.place(x=401)
labelid2.place(x=401)
labelid3.place(x=441)
labelid5.place(x=401)
labelid11.place(x=401)
labelid21.place(x=401)
MT.place(x=401)
QT.place(x=401)
QDM.place(x=441)
def trashconfirmed():
entrada = entry.get()
if entrada == "1":
banco = """DELETE FROM Navio WHERE id = 1"""
cursor.execute(banco)
conn.commit()
print("deu certo 1!")
elif entrada == "2":
banco = """DELETE FROM Navio WHERE id = 2"""
cursor.execute(banco)
conn.commit()
print("deu certo 2!")
elif entrada == "3":
banco = """DELETE FROM Navio WHERE id = 3"""
cursor.execute(banco)
conn.commit()
print("deu certo 3!")
elif entrada == "4":
banco = """DELETE FROM Navio WHERE id = 4"""
cursor.execute(banco)
conn.commit()
print("deu certo 4!")
elif entrada == "5":
banco = """DELETE FROM Navio WHERE id = 5"""
cursor.execute(banco)
conn.commit()
print("deu certo 5!")
elif entrada == "6":
banco = """DELETE FROM Navio WHERE id = 6"""
cursor.execute(banco)
conn.commit()
print("deu certo 6!")
elif entrada == "7":
banco = """DELETE FROM Navio WHERE id = 7"""
cursor.execute(banco)
conn.commit()
print("deu certo 7!")
elif entrada == "8":
banco = """DELETE FROM Navio WHERE id = 8"""
cursor.execute(banco)
conn.commit()
print("deu certo 8!")
elif entrada == "9":
banco = """DELETE FROM Navio WHERE id = 9"""
cursor.execute(banco)
conn.commit()
print("deu certo 9!")
elif entrada == "10":
banco = """DELETE FROM Navio WHERE id = 10"""
cursor.execute(banco)
conn.commit()
print("deu certo 10!")
elif entrada == "11":
banco = """DELETE FROM Navio WHERE id = 11"""
cursor.execute(banco)
conn.commit()
print("deu certo 11!")
elif entrada == "12":
banco = """DELETE FROM Navio WHERE id = 12"""
cursor.execute(banco)
conn.commit()
print("deu certo 12!")
elif entrada == "13":
banco = """DELETE FROM Navio WHERE id = 13"""
cursor.execute(banco)
conn.commit()
print("deu certo 13!")
elif entrada == "14":
banco = """DELETE FROM Navio WHERE id = 14"""
cursor.execute(banco)
conn.commit()
print("deu certo 14!")
elif entrada == "15":
banco = """DELETE FROM Navio WHERE id = 15"""
cursor.execute(banco)
conn.commit()
print("deu certo 15!")
def insert2():
botao61.place(x=80, y=15)
entry.place(x=15, y=150)
entry2.place(x=15, y=195)
entry4.place(x=280, y=12)
MT.place(x=15, y=125)
QT.place(x=15, y=170)
QDM.place(x=220, y=12)
botao71.place(x=441)
botao81.place(x=441)
labelid5.place(x=401)
entry1.place(x=401)
entry3.place(x=401)
def insertconfirmed2():
entrada2 = entry2.get()
entrada = entry.get()
entrada3 = entry4.get()
banco = """INSERT INTO TOXICO(TOX, Quantidade, QDM) VALUES(?, ?, ?)"""
entrada1 = entrada, entrada2, entrada3
cursor.execute(banco, entrada1)
conn.commit()
print("deu certo nome!")
def change2():
botao71.place(x=80, y=55)
entry.place(x=15, y=150)
entry2.place(x=15, y=195)
entry3.place(x=110, y=95)
entry4.place(x=280, y=12)
labelid5.place(x=80, y=95)
MT.place(x=15, y=125)
QT.place(x=15, y=170)
QDM.place(x=220, y=12)
# esconder
botao6.place(x=401)
labelid41.place(x=401)
botao8.place(x=401)
botao61.place(x=401)
botao81.place(x=441)
entry1.place(x=401)
def changeconfirmed2():
escolha = entry.get()
entrada = entry2.get()
Id = entry3.get()
if escolha == "Material Tóxico":
banco = ("""
UPDATE INTO TOXICO(TOX) VALUES(TEXT)
""")
cursor.execute(banco)
Banco.conn.commit()
print("deu certo nome!")
elif escolha == "Quantidade":
Banco.cursor.execute("""
UPDATE INTO TOXICO(Quantidade) VALUES(TEXT)
""", Id, entrada)
Banco.conn.commit()
print("deu certo idade!")
elif escolha == "Q. Despejada no Mar":
Banco.cursor.execute("""
UPDATE INTO TOXICO(QMD) VALUES(TEXT)
""", Id, entrada)
Banco.conn.commit()
print("deu certo situação!")
def trash2():
botao81.place(x=80, y=95)
entry1.place(x=15, y=150)
labelid41.place(x=15, y=125)
# esconder
botao7.place(x=401)
botao6.place(x=401)
botao61.place(x=401)
botao71.place(x=401)
entry.place(x=401)
entry2.place(x=401)
entry3.place(x=401)
entry4.place(x=441)
labelid.place(x=401)
labelid2.place(x=401)
labelid3.place(x=441)
labelid5.place(x=401)
MT.place(x=401)
QT.place(x=401)
QDM.place(x=441)
def trashconfirmed2():
entrada = entry1.get()
if entrada == "1":
banco = """DELETE FROM TOXICO WHERE id = 1"""
cursor.execute(banco)
conn.commit()
print("deu certo 1!")
elif entrada == "2":
banco = """DELETE FROM TOXICO WHERE id = 2"""
cursor.execute(banco)
conn.commit()
print("deu certo 2!")
# termina aqui funções
# botões MENU E TABELAS
fecharb = Button(jan, text="←", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=Return)
fecharb.place(x=20, y=20)
botao1 = Button(topFrame2, text="Tripulação", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=trip)
botao1.place(x=20, y=10)
botao2 = Button(topFrame2, text="Sub. Tóxicas", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=tox)
botao2.place(x=112, y=10)
botao3 = Button(centerframe, text="Inserir:", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=insert)
botao3.place(x=441)
insert = Button(centerframe, text="Inserir:", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=insert2)
insert.place(x=441)
botao4 = Button(centerframe, text="Alterar:", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=change)
botao4.place(x=441)
change = Button(centerframe, text="Alterar:", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=change2)
change.place(x=441)
botao5 = Button(centerframe, text="Excluir:", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=trash)
botao5.place(x=441)
excluirtrash = Button(centerframe, text="Excluir:", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=trash2)
excluirtrash.place(x=441)
# botoes para confirmar
botao6 = Button(centerframe, text="Inserir!", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=insertconfirmed)
botao6.place(x=441)
botao61 = Button(centerframe, text="Inserir!", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=insertconfirmed2)
botao61.place(x=441)
botao7 = Button(centerframe, text="Alterar!", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=changeconfirmed)
botao7.place(x=441)
botao71 = Button(centerframe, text="Alterar!", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=changeconfirmed2)
botao71.place(x=441)
botao8 = Button(centerframe, text="Excluir!", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=trashconfirmed)
botao8.place(x=441)
botao81 = Button(centerframe, text="Excluir!", font=("Arial", 10, 'bold'), bg="#585858", fg="#cfc91c",
command=trashconfirmed2)
botao81.place(x=441)
# termina aqui botoes para confirmar
# termina aqui botões
# labels da entrada TABELAS
labelid = Label(centerframe, text="Nome:", font=("Arial", 10, 'bold'), bg="#cfc91c", fg="#585858")
labelid.place(x=401)
labelid2 = Label(centerframe, text="Idade:", font=("Arial", 10, 'bold'), bg="#cfc91c", fg="#585858")
labelid2.place(x=401)
labelid11 = Label(centerframe, text="Nome da Coluna:", font=("Arial", 10, 'bold'), bg="#cfc91c", fg="#585858")
labelid11.place(x=401)
labelid21 = Label(centerframe, text="Entrada:", font=("Arial", 10, 'bold'), bg="#cfc91c", fg="#585858")
labelid21.place(x=401)
labelid3 = Label(topFrame2, text="Situação:", font=("Arial", 10, 'bold'), bg="#cfc91c", fg="#585858")
labelid3.place(x=441)
labelid4 = Label(centerframe, text="Excluir:", font=("Arial", 10, 'bold'), bg="#cfc91c", fg="#585858")
labelid4.place(x=401)
labelid41 = Label(centerframe, text="Excluir Linha Id:", font=("Arial", 10, 'bold'), bg="#cfc91c", fg="#585858")
labelid41.place(x=401)
labelid5 = Label(centerframe, text="Id:", font=("Arial", 10, 'bold'), bg="#cfc91c", fg="#585858")
labelid5.place(x=401)
MT = Label(centerframe, text="Material Tóxico:", font=("Arial", 10, 'bold'), bg="#cfc91c", fg="#585858")
MT.place(x=401)
QT = Label(centerframe, text="Quantidade:", font=("Arial", 10, 'bold'), bg="#cfc91c", fg="#585858")
QT.place(x=401)
QDM = Label(topFrame2, text="Q.D.M.:", font=("Arial", 10, 'bold'), bg="#cfc91c", fg="#585858")
QDM.place(x=441)
# termina aqui labels da entrada TABELAS
# entrada TABELAS
entry = ttk.Entry(centerframe, width=19)
entry.place(y=500)
entry1 = ttk.Entry(centerframe, width=3)
entry1.place(y=500)
entry2 = ttk.Entry(centerframe, width=19)
entry2.place(y=500)
entry3 = ttk.Entry(centerframe, width=3)
entry3.place(y=500)
entry4 = ttk.Entry(topFrame2, width=19)
entry4.place(x=500)
# termina aqui entrada
# tabela TRIPULAÇÃO
tree1 = ttk.Treeview(centerframe2, selectmode="extended", columns=("x", "z", "A", "B"), height=10)
tree1.pack(expand=YES, fill=BOTH)
tree1.place(x=500)
tree1.heading("#0", text="")
tree1.column("#0", minwidth=0, width=-1)
tree1.heading("x", text="Id")
tree1.column("x", minwidth=0, width=20)
tree1.heading("A", text="Idade")
tree1.column("A", minwidth=0, width=40)
tree1.heading("B", text="Situação")
tree1.column("B", minwidth=0, width=96)
tree1.heading("z", text="Nome")
tree1.column("z", minwidth=0, width=152)
# termina aqui tabela
# tabela TOXICO
tree2 = ttk.Treeview(centerframe2, selectmode="extended", columns=("A", "B", "C"), height=10)
tree2.pack(expand=YES, fill=BOTH)
tree2.place(x=500)
tree2.heading("#0")
tree2.column("#0", minwidth=0, width=-1, stretch=NO)
tree2.heading("A", text="Material Tóxico")
tree2.column("A", minwidth=0, width=92)
tree2.heading("B", text="Quantidade")
tree2.column("B", minwidth=0, width=72)
tree2.heading("C", text="Q. Despejada no Mar")
tree2.column("C", minwidth=0, width=125)
# termina aqui tabela
# função sql
for i in rows:
tree1.insert('', 'end', values=i)
for i in rows2:
tree2.insert('', 'end', values=i)
except:
messagebox.showinfo(title="", message="ACESSO NEGADO. POR FAVOR MANTENHA DISTÂNCIA DO NAVIO.")
# termina aqui login comando
# botao
EntrarBotao = Button(buttonFrame, text="Entrar", font=("Arial", 10, 'bold'), width=15, command=login, bg="#585858", fg="#cfc91c")
EntrarBotao.place(x=35, y=12)
# termina aqui botao
l = tk.Label()
l.pack()
l.place(x=501)
jan.after(1000, update)
jan.update()
jan.mainloop()
参考文献:
Inserir=插入
Alterar=更改
Excluir=删除
Entrada=输入
埃斯科拉=选择
数据库代码:
将文件另存为“Banco.py”
import sqlite3
conn = sqlite3.connect("UserData.db")
cursor = conn.cursor()
print("Successfully connected")
表信息:
有四张桌子
CREATE TABLE "Navio" (
"Id" INTEGER NOT NULL,
"Nome" TEXT,
"Idade" INTEGER,
"Situação" TEXT,
PRIMARY KEY("Id" AUTOINCREMENT)
);
CREATE TABLE "TOXICO" (
"Id" INTEGER,
"TOX" TEXT NOT NULL,
"Quantidade" TEXT,
"QDM" TEXT,
PRIMARY KEY("Id")
);
CREATE TABLE "Users" (
"Id" INTEGER NOT NULL,
"User" TEXT NOT NULL,
"Password" TEXT NOT NULL,
PRIMARY KEY("Id" AUTOINCREMENT)
);
我感谢那些试图帮助我的人!幸运的是,我现在设法解决了这个问题,我将把解决方案留在这里,以便其他人也可以使用它:
工作完成了!我尝试了很多方法,这是最简单、最快的,最终解决了我的问题
相关问题 更多 >
编程相关推荐