简体   繁体   English

使用变量的 Python SQL 查询?

[英]Python SQL Query using Variables?

Apologies if this seems too simple to ask, but just started coding and cant seem to figure out what the problem is.如果这看起来太简单了,但刚刚开始编码并且似乎无法弄清楚问题是什么,请道歉。 I've replaced the hard coded values with user inputted variables and the data is saving into the database but I can't seem to display it back out.我已经用用户输入的变量替换了硬编码值,并且数据正在保存到数据库中,但我似乎无法将其显示出来。 Is my cur.execute select line incorrect?我的 cur.execute 选择行不正确吗?

import sqlite3
conn = sqlite3.connect("OS_Employee.db")

with conn:
cur = conn.cursor()
try:
    ID = input("Please enter your Employee ID: ")
    FirstName = input("Please enter your First Name: ")
    LastName= input("Please enter your Last Name: ")
    Email = input("Please enter your Employee Email: ")
    Password = input("Please enter your Desired Password: ")

    cur.execute ("""INSERT INTO Employee VALUES (?, ?, ?, ?, ?)""",
                 (ID, FirstName, LastName, Email, Password))

    cur.execute("SELECT EmployeeID FROM Employee WHERE (EmployeeID = ?", (ID, ))

    results = cur.fetchall()
    print(results)
except:
        print("Connection Failed")enter code here

If the indentation looks odd it's only on stackoverflow since idk how to get it to paste correctly, there are no indentation errors in the actual code.如果缩进看起来很奇怪,它只是在 stackoverflow 上,因为 idk 如何让它正确粘贴,实际代码中没有缩进错误。 Thanks!谢谢!

Try to replace your line: 尝试替换您的行:

cur.execute("SELECT EmployeeID FROM Employee WHERE (EmployeeID = ?", (ID, ))

with: 与:

cur.execute("SELECT EmployeeID FROM Employee WHERE EmployeeID = ?", (ID, ))

It looks like you have mismatched parentheses. 括号似乎不匹配。

尝试这个:

    cur.execute ("INSERT INTO Employee VALUES ({}, {}, {}, {}, {})".format(ID, FirstName, LastName, Email, Password))

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

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