Stack Overflow Asked by user13888307 on January 27, 2021
Because I want to insert tables with varying sizes I need to be able to use a string variable in my insert statement for example like so:
numvals = '?,?'
vals = 'delays_values[0], delays_values[1]'
converted_valz = 'ID int ,Description text'
c.execute("CREATE TABLE DELAYS ("+converted_valz+")")
c.execute("INSERT INTO DELAYS VALUES ("+numvals+" )", (vals))
However this gives me the aforementioned error which says exactly: sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 19, and there are 349 supplied.
For some reason, if I replace vals with the hardcoded values it actually contains like so it actually works but then I am forced to hardcode values which defeats the purpose of all of this. Please help!:
numvals = '?,?'
vals = 'delays_values[0], delays_values[1]'
converted_valz = 'ID int ,Description text'
c.execute("CREATE TABLE DELAYS ("+converted_valz+")")
c.execute("INSERT INTO DELAYS VALUES ("+numvals+" )", (delays_values[0], delays_values[1]))
This might be an oversimplification that may cause problems later on, but an easy fix is to evaluate your vals
string with eval(vals)
in the end:
import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
delays_values = (1,2)
numvals = '?,?'
vals = 'delays_values[0], delays_values[1]'
converted_valz = 'ID int ,Description text'
c.execute("CREATE TABLE DELAYS ("+converted_valz+")")
c.execute("INSERT INTO DELAYS VALUES ("+numvals+" )", eval(vals))
Don't know why vals
is a string to begin with though to be honest...
Correct answer by SuperCiocia on January 27, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP