Adding columns into sqlite3 table dynamically using list

266 Views Asked by At

I have been trying to insert column list into table but unable to do so here is what i want to do

Let say i have a list of column names column=["Email","Name","Salary"] and i want to insert these columns into a table without hardcoding them , using column list only what is the best and easy way of doing it , i have read multiple answers but unable to understand , as i am a beiginer please explain it in basic manner , in python i have tried it using

column=["Email","Name","Salary"] 
'''CREATE TABLE IF NOT EXISTS EMPLOYEE(? TEXT NOT NULL,? TEXT NOT NULL,? TEXT NOT NULL)''',(column[0],column[1],column[2])

but i am getting the syntax error Please help me i am beginner

1

There are 1 best solutions below

1
On

One approach would be to use an f-string in python to create your sqlite instructions. So, you could do:

table_string = f"CREATE TABLE employee({column[0]} TEXT, {column[1]} TEXT, {column[2]} TEXT)"
cur.execute(table_string)