For this project, I have a database file with a table RESULTS which looks like this:
conn.execute('''CREATE TABLE RESULTS(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age TEXT NOT NULL,
gender TEXT NOT NULL);''')
And this is the file I'm using to create a prototype front end so that users can access the data in the database:
#setting up the user input variables
srcIn = input("what field do you want to search: id, name, age, or gender? ")
srcQuery = input("what result do you want to find from the "+ srcIn + " search? ")
if(srcIn == "age") or (srcIn == "id"):
#by default input returns a string but it needs to be an int for age and id parameters
srcQuery = int(srcQuery)
print("converted to int")
srcOut = input("what field do you want to return: id, name, age, or gender? ")
print("making cursor")
cursor = conn.execute("SELECT %s FROM RESULTS WHERE %s = %s" % (srcOut, srcIn, srcQuery))
for row in cursor:
print(srcOut + ": " + row[0])
conn.close()
print("closed database " + db)
If I were to run this and pass it age or id, the number 45, and name it would return the names, genders, and other stats from my database perfectly. However, when I pass it gender, and female or male, and returning the names or ages or ids, an error would pop up saying this:
cursor = conn.execute("SELECT %s FROM RESULTS WHERE %s = %s" % (srcOut, srcIn, srcQuery))
sqlite3.OperationalError: no such column: male
For this specific example:
srcOut = name
srcIn = gender
srcQuery = male
I've tried replacing the %s with ? arguments (I know it won't work on the parameters that I'm passing to the SELECT statement), so I'm stumped.
I think that it has something to do with the fact that in certain instances I'm passing it an integer in the srcQuery field and in others I'm passing it a string.
Any help or suggestions would be appreciated.