tags:

views:

44

answers:

1

Hi,

I want to create a table with multiple columns, say about 100 columns, in an sqlite database. Is there a better solution than naming each column individually? I am trying the following:

conn = sqlite3.connect('trialDB')
cur = conn.cursor()

listOfVars = ("added0",)
for i in range(1,100):
    newVar = ("added" + str(i),)
    listOfVars = listOfVars + newVar
print listOfVars

for i in listOfVars:
    cur.execute('''ALTER TABLE testTable ADD COLUMN ? TEXT''',(i,))

conn.commit()    
cur.close()
conn.close()

But I get the following error:

OperationalError: near "?": syntax error

Can someone please suggest how I can do this? Thanks!

+2  A: 

I guess you could do it through string formatting, like this :

for i in listOfVars:
    cur.execute('''ALTER TABLE testTable ADD COLUMN %s TEXT''' % i)

But having 100 columns in a sqlite db is certainly not common, are you sure of having a proper db design ?

mripard
I am running some simulations, each of which outputs a data matrix with about 1500 rows and 200 columns. If I output the data to a .csv file it is about 3mb. I was wondering if using an sqlite database will make the whole thing efficient. Thanks for your answer.
Curious2learn