I am trying to create automatically a rather large sqlite database tables which all have at least 50 columns. The column names are already available in different lists. Using the .format I almost did this. The only open issue is the predetermination of the number of placeholders for "{}" from the length of name's list. Please see the code example below.
import sqlite3
db_path = "//Some path/"
sqlite_file = 'index_db.sqlite'
conn = sqlite3.connect(db_path + sqlite_file)
c = conn.cursor()
db_columns=['c1','c2','c3','c4','c5']
#This code is working
c.execute("create table my_table1 ({}, {}, {}, {}, {})" .format(*db_columns))
#This code doesn't work
c.execute("create table my_table2 (" + ("{}, " * 5)[:-2] + ")" .format(*db_columns))
#Following error appears
OperationalError: unrecognized token: "{"
#--> This even that the curly brackets are the same as in my_table1
print("create table my_table2 (" + ("{}, " * 5)[:-2] + ")")
#Output: create table my_table2 ({}, {}, {}, {}, {})
c.execute("INSERT INTO my_table1 VALUES (?,?,?,?,?)", (11, 111, 111, 1111, 11111))
conn.commit()
c.close
conn.close()
Is there a way to resolve that issue for my_table2? Or is there a better way to create the column names dynamically from a list?
P.s. This is an internal database so I don't have any concerns regarding security issues due to using variables as names dynamically.
Thanks in advance! Timur