0

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

Nbbbb
  • 1
  • 1
  • You should remove coma (`,`) between `TEXT` and `NOT NULL` in your query. – Olvin Roght Oct 24 '20 at 08:19
  • I added comma by mistake in this ques , but on my machine i didnt use comma it was giving syntax error , now i edited the ques and has removed comma Thanks!! – Nbbbb Oct 24 '20 at 08:22

1 Answers1

0

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)
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
jmaloney13
  • 225
  • 1
  • 8
  • You can let `sqlite3` module to add parameters, just pass `column` as second parameter of [`execute()`](https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.execute). – Olvin Roght Oct 24 '20 at 09:55