I'm trying to insert some database fields using python to an SQLite DB. I keep getting the following error:
ValueError: operation parameter must be str
Below is my code.
import sqlite3
conn = sqlite3.connect("pass.db")
c = conn.cursor()
#Create table password
table_create_statement = "create table if not exists password(id integer primary key, site text, pass text);"
c.execute(table_create_statement)
#Promt user to input entry
sites = str(raw_input("Enter name of the sites: "))
password = str(raw_input("Enter password: "))
#SQL statement to insert the new entry
sql_statement = "insert into password values(?, ?, ?);",(None, sites, password)
c.execute((sql_statement))
connection.commit()
print("Password entered successfully")
Output:
Enter name of the sites: www.google.com
Enter password: abcd
Traceback (most recent call last):
File "pw.py", line 39, in <module>
c.execute((sql_statement))
ValueError: operation parameter must be str or unicode
You need to pass in the sql statement and parameters as separate arguments to execute(). You are currently passing in a tuple.
Do this instead:
sql_statement = "insert into password values(?, ?, ?);"
c.execute(sql_statement, (None, sites, password))
Now sql_statement is a single string, and the parameters are passed in as a second, separate argument.
Note that raw_input() already returns a str object, no need to convert that to str again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With