微信号:weixin888
I'm looping and with each loop I manipulate data and then save it to different CSV file. Now I'm trying to do the same with SQLite. I have many tables but for sake of simplicity, lets say i have 3 tables: first_table, second_table, third_table
So say I properly connect to database file, create cursor, generate data i want to save, I generate proper table names for each loop, to save data into each table, see simplified code bellow:
tables=['first', 'second', 'third'] for each in tables: table_name=each + '_table' # generated proper table name c.execute("INSERT OR IGNORE INTO table_name VALUES (?,?,?)", data) c.execute("INSERT OR IGNORE INTO" table_name "VALUES (?,?,?)", data)
I can't figure out, how to use variable: "table_name" as placeholder for previously generated table name. Above I'm showing two examples I tried, but of course both returns error.
Can anyone help me with this, please?
Edit: Not sure I managed to explain properly. Say I have 50 tables with names from: first_table to fiftieth_table. I will loop over 50 times, to insert into each table some data. (i need to loop 50 times since that's how i'm getting data). After every loop, I'm inserting into next table. How do I manage to change table name in c.execute("INSERT... statement for every loop?