6575
6575

Reputation: 67

"No such column error" in sqlite3 in python

I am getting the error while trying to insert values.

This is for storing userid and password of users in sqlite3 database.

import sqlite3, hashlib

conn = sqlite3.connect('user.db')

a = input("Enter id:")
b = input("Enter password:")
b = str.encode(b)
b = hashlib.sha224(b).hexdigest()

conn.execute("insert into user(id,pass) values(%s, %s);"%(a,b))
print("Done")

Create statement was:

CREATE TABLE user(id varchar[50], pass varchar[50])

When I try to enter values A(id) and a (password), I get the following error:

conn.execute("insert into user(id,pass) values(%s, %s);"%(a,b))
sqlite3.OperationalError: no such column: A

Upvotes: 0

Views: 86

Answers (1)

EchoMike444
EchoMike444

Reputation: 1692

You must use placeholders for the parameters.

So your code will become:

 conn.execute("insert into user(id,pass) values(? , ?);" , (a,b))

Another solution that is bad practice, because of the high risk of SQL injection is:

 conn.execute("insert into user(id,pass) values('%s', '%s');"%(a,b))

SQLite3 Python module documentation:

https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.execute

Upvotes: 1

Related Questions