简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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