2017-09-04 97 views
-1

這是我的代碼。當我試圖運行此代碼。我面臨以下錯誤。任何人都可以建議我如何糾正該錯誤,並且此腳本可以成功運行。Python上的SQLite3錯誤

from tkinter import * 
import sqlite3 

global a, b 

def sair_mestre(): 
    global mestre 
    mestre.quit() 

def criar_tabela(): 
    c.execute("CREATE TABLE IF NOT EXISTS Registro (Nome text, Senha text)") 

def receberl(): 
    global Nome, Senha 
    Nome = entradalogin.get() 
    Senha = entradasenha.get() 
    ler_dados(Senha) 
    if x: 
     sucesso = Label(mestre, text="Login Realizado com sucesso!") 
     sucesso.grid(row=1,column=1) 
    else: 
     inexistente = Label(mestre, text="Login errado") 
     inexistente.grid(row=1, column=1) 

def receber2(): 
    logincadastro2 = entradalogin2.get() 
    senhacadastro2 = entradasenha2.get() 
    c.execute("INSERT INTO Registro VALUES(?, ?)", (logincadastro2, senhacadastro2)) # Utilização de Variáveis 
    conexao.commit() 
    cadastro.destroy() 

    realizado = Label(mestre, text="Cadastrado com sucesso") 
    realizado.grid(row=1, column=1) 

    botaorealizado = Button(mestre, text="Ok", command=sair_mestre) 
    botaorealizado.grid(row=2, column=1) 

def registro(): 
    programa.destroy() 

    global cadastro 
    cadastro = Frame(mestre) 
    cadastro.grid() 

    realizando_cadastro = Label(cadastro, text="Realizando Cadastro...") 
    realizando_cadastro.grid(row=0, column=1) 

    labellogin = Label(cadastro, text="Login") 
    labellogin.grid(row=1, column=0) 

    labelsenha = Label(cadastro, text="Senha") 
    labelsenha.grid(row=2, column=0) 

    global entradalogin2 
    entradalogin2 = Entry(cadastro) 
    entradalogin2.grid(row=1, column=1) 

    global entradasenha2 
    entradasenha2 = Entry(cadastro, show="•") 
    entradasenha2.grid(row=2, column=1) 

    botaocadastro = Button(cadastro, text="Ok", command=receber2) 
    botaocadastro.grid(row=3, column=1) 

def ler_dados (valorbusca): 
    global row 
    global x 
    buscar_dados = "SELECT * FROM Registro WHERE Nome = ? AND Senha = ?" 
    for row in c.execute(buscar_dados, (valorbusca,)): 
     if Nome and Senha in row: 
      x = True 


conexao = sqlite3.connect("Registro_Cadastro.db") 
c = conexao.cursor() 

criar_tabela() 

mestre = Tk() 

programa = Frame(mestre) 
programa.grid() 

login = Label(programa, text="Login") 
login.grid(row=0, column=0) 

global entradalogin 
entradalogin = Entry(programa) 
entradalogin.grid(row=0, column=1) 

senha = Label(programa, text="Senha") 
senha.grid(row=1, column=0) 

global entradasenha 
entradasenha = Entry(programa, show="•") 
entradasenha.grid(row=1, column=1) 

botaook = Button(programa, text="Entrar",command= receberl) 
botaook.grid(row=2, column=1) 

botaoregistro = Button(programa, text="Cadastro",command= registro) 
botaoregistro.grid(row=3,column=1) 

mestre.mainloop() 

這是我的程序拋出的錯誤。任何人都可以幫助我解決這個錯誤? 我認爲錯誤在receberl函數。

File "-------", line 1558, in __call__ 
    return self.func(*args) 

File "------", line 17, in receberl 
    ler_dados(Senha) 

File "------", line 69, in ler_dados 
    for row in c.execute(buscar_dados, (valorbusca,)): 
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 2, and there are 1 supplied. 

回答

2

你需要爲這個查詢提供了2個參數:

buscar_dados = "SELECT * FROM Registro WHERE Nome = ? AND Senha = ?" 

當你在函數調用receberller_dados(Senha),貌似Nome丟失。

你應該使用類似:ler_dados(Nome, Senha)第一

,然後更改方法ler_dados到:

def ler_dados (nome, senha): 
    global row 
    global x 
    buscar_dados = "SELECT * FROM Registro WHERE Nome = ? AND Senha = ?" 
    for row in c.execute(buscar_dados, (nome, senha)): 
     if Nome and Senha in row: 
      x = True 
+0

**我做了兩個建議的更改並且現在給出這個錯誤:** Tkinter回調中的異常 Traceback(最近調用最後一次): 文件「-----」,第1558行,在__call__中 return self.func(* args) File「 - ---「,line 16,in receberl Nome = entradalogin.get() 文件「-----」,行2530,在得到 返回self.tk.call(self._w,'get') _tkinter.TclError:無效的命令名稱「.5288976.5207312」 –

+0

你會得到什麼錯誤? – PRMoureu

3

你所得到的錯誤信息是非常明確的:你SELECT查詢結合兩個參數,但你的代碼只能指定其中的一個。要解決,首先改變你對ler_dados()呼叫的用戶名和密碼都經過:

def receberl(): 
    global Nome, Senha 
    Nome = entradalogin.get() 
    Senha = entradasenha.get() 
    ler_dados(Nome, Senha) 
    # ... etc. 

,然後在查詢綁定兩者的用戶名和密碼:

def ler_dados(username, valorbusca): 
    global row 
    global x 
    buscar_dados = "SELECT * FROM Registro WHERE Nome = ? AND Senha = ?" 
    for row in c.execute(buscar_dados, (username, valorbusca,)): 
     if Nome and Senha in row: 
      x = True 
+0

我做了兩個建議的更改,現在給這個錯誤:在Tkinter回調Traceback(最近的最後一次調用最後)異常:文件「-----」,第1558行,在調用返回self.func(* args)文件「 - ---「,第16行,在receberl中Nome = entradalogin.get()文件」-----「,第2530行,返回self.tk.call(self._w,'get')_tkinter.TclError:無效的命令名稱「.5288976.5207312」 –

+0

您的代碼充滿了錯誤,您應該使用調試器。 –

+0

你能舉個例子嗎?請問 –