SQLite Forum

Placeholder for table name when inserting data?
I couldn't find example, how to use placeholder for table name when inserting data. Can someone help me fix SyntaxError in my simplified example bellow?

Why do I need table name placeholder? Say I have 100 tables like so: first_table, second_table... My (Python) script loop over 100 times, manipulate & extract data, that needs to be written into these tables. At first loop it writes into first table, at second loop it writes into second table... and so on. First row is UNIQUE (that's why I have "INSERT OR IGNORE" in insert statement) For now I'm looping and inserting into CSV files but I would like to save data into SQLite3 database file.

I was told, there is no table name placeholder in SQLite3, but that I could use python's f-string. I can't get code to work. Bellow is simple code I would like to get it to work, if anyone has any suggestion:

> import sqlite3

> conn = sqlite3.connect('./test.db')   
> c = conn.cursor()

> c.execute('''CREATE TABLE IF NOT EXISTS first_table
                 (value1 INTEGER, value2 INTEGER, value3 INTEGER)''')

> table_name = 'first_table'

> numbers = [111, 222, 333]

> c.execute(f"INSERT OR IGNORE {table_name} INTO VALUES (?,?,?)", numbers)

> conn.commit()