使用变量的Python SQL查询?

时间:2018-10-16 19:28:47

标签: python sql

道歉,如果这看起来太简单了,但是刚开始编码并且似乎无法找出问题所在。我已经用用户输入的变量替换了硬编码的值,并且数据已保存到数据库中,但是我似乎无法将其显示出来。我的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

如果缩进看起来很奇怪,则仅在stackoverflow上,因为idk如何正确粘贴它,所以实际代码中没有缩进错误。谢谢!

2 个答案:

答案 0 :(得分:0)

尝试替换您的行:

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

具有:

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

似乎括号不匹配。

答案 1 :(得分:-1)

尝试一下:

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