Reputation: 11
Using python 2.7.5, I've written the following code down to compile based off of an online course I'm taking that shows how sqlite3 works with python
import sqlite3 as sql
database1 = sql.connect('test1.db')
db1_cursor = database1.cursor()
cmd = 'CREATE TABLE IF NOT EXISTS users(username TEXT,password TEXT)'
cmd2 = 'INSERT INTO users(username,password) VALUES("testuser,testpassword")'
cmd3 = 'SELECT username,password FROM users'
db1_cursor.execute(cmd)
db1_cursor.execute(cmd2)
db1_cursor.execute(cmd3)
database1.commit()
for x in db1_cursor:
print(x)
Now, upon running this code it gives me the following operation error:
Traceback (most recent call last):
File "C:\Users\Ryan\My Code Projects\Learning\udemycourse.py", line 11, in <module>
db1_cursor.execute(cmd2)
OperationalError: 1 values for 2 columns
Why does it give this error for db1_cursor.execute(cmd2) but not for db1_cursor.execute(cmd1) and how can I fix this?
Upvotes: 0
Views: 69
Reputation: 1
A better way of doing the insert would be cmd2 = 'INSERT INTO users(username,password) VALUES(?, ?)'
Creating placeholders
db1_cursor.execute(cmd2, ('testuser','testpassword'))
and passing the values as a tuple
Upvotes: 0