I am writing to you because I cannot solve this:
I want to insert into a database with 2 fields: ID AND DATE.
and in python it throws me an error: Incorrect number of bindings supplied. The current statement uses 1.
thankss a lottt
My code:
def run_query(self, query, parameters = ()):
with sqlite3.connect(self.db_name) as conn:
cursor = conn.cursor()
result = cursor.execute(query, parameters)
conn.commit()
return result
def add_fecha(self):
query = 'INSERT INTO base VALUES(NULL, ?)'
parameters = "19-2-2022"
self.run_query(query, parameters)
self.get_fechas()
CodePudding user response:
The substitution parameters value must be a tuple (or some kind of iterable). You're giving it a string, which IS an iterable, but it's going to try to use it one character at a time.
Simple fix:
self.run_query(query, (parameters,))
CodePudding user response:
The parameters in execute()
are used as iterables. When you pass your date string directly, its is also taken as iterable, therefore passing 9 parameters rather than a single date string.
You need to put your string in a tuple or a list to pass it all at once.
See the following example:
import sqlite3
with sqlite3.connect(":memory:") as conn:
conn.execute("CREATE TABLE t (id INT, date TEXT);")
conn.execute("INSERT INTO t VALUES(1, ?)", ["2022-06-20"])
print(conn.execute("SELECT * FROM t").fetchall())