Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将python变量值插入SQL表_Python_Sqlite_Tkinter - Fatal编程技术网

将python变量值插入SQL表

将python变量值插入SQL表,python,sqlite,tkinter,Python,Sqlite,Tkinter,我有一个密码系统,它将python程序的密码存储在SQL表中。我希望用户能够在tkinter窗口中更改密码,但我不确定如何使用python变量的值作为SQL表的值。下面是一个示例代码: import tkinter from tkinter import * import sqlite3 conn = sqlite3.connect('testDataBase') c = conn.cursor() c.execute("INSERT INTO info Values('test')") c

我有一个密码系统,它将python程序的密码存储在SQL表中。我希望用户能够在tkinter窗口中更改密码,但我不确定如何使用python变量的值作为SQL表的值。下面是一个示例代码:

import tkinter
from tkinter import *
import sqlite3

conn = sqlite3.connect('testDataBase')
c = conn.cursor()

c.execute("INSERT INTO info Values('test')")
c.execute("SELECT password FROM info")
password = (c.fetchone()[0])

#Window setup
admin = tkinter.Tk()
admin.minsize(width=800, height = 600)
admin.maxsize(width=800, height = 600)
#GUI
passwordChangeLabel = Label(admin, text="It is recommended to change your password after first login!", font=("Arial", 14))
passwordChangeLabel.pack()

passwordChangeCurrentPasswordLabel = Label(admin, text="Current Password: ", font=("Arial", 11))
passwordChangeCurrentPasswordLabel.place(x=275, y=30)
passwordChangeCurrentPasswordEntry = Entry(admin)
passwordChangeCurrentPasswordEntry.place(x=405, y=32.5)

passwordChangeNewPasswordLabel = Label(admin, text="New Password: ", font=("Arial", 11))
passwordChangeNewPasswordLabel.place(x=295, y=50)
passwordChangeNewPasswordEntry = Entry(admin)
passwordChangeNewPasswordEntry.place(x=405, y=52.5)

passwordChangeButton = Button(admin, text="Submit", width=20)
passwordChangeButton.place(x=350, y=80)

def changePasswordFunction(event):
    newPassword = passwordChangeNewPasswordEntry.get()
    enteredPassword = passwordChangeCurrentPasswordEntry.get()
    if enteredPassword == password:
        c.execute("REPLACE INTO info(password) VALUES(newPassword)")
    else:
        wrong = tkinter.Toplevel()
        wrong.minsize(width=200, height = 100)
        wrong.maxsize(width=200, height = 100)
        Label(wrong, text="Sorry that password is incorrect!", font=("Arial", 24), anchor=W, wraplength=180, fg="red").pack()

admin.bind('<Return>', changePasswordFunction)
passwordChangeButton.bind('<Button-1>', changePasswordFunction)

如何用新输入的密码正确替换密码列中以前的值?

有两种有效的方法使用
值()
:使用标签或字符串文字。字符串文字是单引号或双引号中的字符串

由于没有将
newPassword
放在引号中,Sqlite假定
newPassword
是一个标签,即列名。它在当前记录中查找
newPassword
字段的值,并抛出一个错误,因为该字段不存在

您要做的是,获取
newPassword
python变量的值,并将其置于引号中。下面是一个已更正的查询字符串(未测试):


正如有人在评论中提到的,直接使用用户输入更新数据库是非常不安全的。数据库接口通常提供了清理所有输入的简单方法,您应该注意遵循这些实践。这是一个很好的资源,其中包含许多流行语言的代码示例。(还请注意,如果在SO的答案中没有包含有关SQL安全性的信息,可能会导致投票被否决。)

这是可行的,第二个问题(很抱歉,我是数据库新手)为什么每次运行程序时都要将值插入表中?有没有办法使值保留在表中,直到它被删除或替换?数据没有理由不存在,除非您删除了它。很可能,你在查询的方式上犯了某种错误。您的代码不显示
info
表中的字段。在表中设置一个自动递增索引字段是一个好主意,这样无论发生什么情况(即,即使两个用户拥有相同的密码),每一行都可以被唯一标识。这就增加了sql注入攻击的可能性。sqlite支持。你读过关于参数化查询的文档吗?
sqlite3.OperationalError: no such column: newPassword
"REPLACE INTO info(password) VALUES('" + newPassword + "')"