Skip to content Skip to sidebar Skip to footer

Dynamically Creating A Placeholder To Insert Many Column Values For A Row In Sqlite Table

I know that it's possible to insert many column values in a SQLite database using a variable with a tuple of values ('2006-03-28', 'BUY', 'IBM', 1000, 45.00) and a corresponding pl

Solution 1:

Build a string based on the number of items in your values, eg:

def place_holder(values):
    return'({})'.format(', '.join('?' * len(values)))

values = ['a', 'b', 'c']
ph = place_holder(values)
# (?, ?, ?)

Then something like:

your_cursor.execute('insert into your_table values {}'.format(ph), values)

If it doesn't meet your schema, you'll have issues, but that's another problem...

Post a Comment for "Dynamically Creating A Placeholder To Insert Many Column Values For A Row In Sqlite Table"