Iam new to python sqlite, and I have a problem with create table query.
I need to create a table, but I have the column names of the table as a list.
columnslist = ["column1", "column2", "column3"]
Now, I have to create a table MyTable with the above columns. But the problem is, I won't know before hand how may columns are there in columnslist
Is it possible to create a table with the number of and name of columns given in columnslist and its syntax?
1 Answer 1
You can first convert your list to tuple and use str.format:
import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute('''CREATE TABLE table_name {}'''.format(tuple(column_list)))
answered Jul 1, 2015 at 7:37
Kasravnd
108k19 gold badges167 silver badges195 bronze badges
default
len(columnslist)gives you the length.len(columnslist)is a number of names in list.columnslist[0]...columnslist[x]...columnslist[len(columnslist)-1]are names of particular list element