Reputation: 442
I am trying to use request.form to get data from the user and save that to data base by INSERT INTO ... VALUES
my code was
statement = "INSERT INTO Course(cId, cName) VALUES (%s, %s)" , (cId,cName)
result = conn.execute(statement);
conn.commit()
conn.close()
The code compiles but when it is running on web, it gives me a value error. Error message is
ValueError: operation parameter must be str or unicode
I am pretty sure there is something wrong with my format in the first line. but I cannot figure it out.
Upvotes: 0
Views: 199
Reputation: 1123400
You are passing in a tuple with two elements, not a single string. You need to pass in the parameters as a separate argument:
statement = "INSERT INTO Course(cId, cName) VALUES (%s, %s)"
params = (cId,cName)
result = conn.execute(statement, params)
Just because a call uses commas, does not mean you can transfer that comma to a tuple and have it mean the same thing.
You can unpack the tuple using conn.execute(*statement)
syntax, but that's overkill here as you don't have a variable number of arguments to begin with.
Upvotes: 1