简体   繁体   English

Python上的SQLite3错误

[英]Error with SQLite3 on Python

This is my code. 这是我的代码。 When I am trying to run this code. 当我尝试运行此代码时。 I face following error. 我面临以下错误。 Anyone can suggest me how to correct that error and this script can run successfully. 任何人都可以建议我如何纠正该错误,此脚本可以成功运行。

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

This is the error thrown by my program. 这是我的程序抛出的错误。 Can anyone help me to resolve this error? 谁能帮助我解决此错误? I think the error is in receberl function. 我认为错误在于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.

The error message you are getting is pretty clear: your SELECT query binds two parameters, but your code only specifies one of them. 您收到的错误消息非常清楚:您的SELECT查询绑定了两个参数,但是您的代码仅指定了其中一个。 To fix, first change your call to ler_dados() to pass in both the username and password: 要解决此问题,请首先将您对ler_dados()调用更改为同时传递用户名和密码:

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

And then in your query bind both the username and password: 然后在查询中绑定用户名和密码:

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

You need to provide 2 arguments for this query : 您需要为此查询提供2个参数:

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

When you call ler_dados(Senha) in the function receberl , looks like Nome is missing. 当您在函数receberl调用ler_dados(Senha)时,看起来Nome丢失了。

You should use something like : ler_dados(Nome, Senha) first 您应该先使用: ler_dados(Nome, Senha)

and then change the method ler_dados to : 然后将ler_dados方法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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM