2010-04-22 65 views
8

我知道這很簡單,但我無法得到它的工作!我沒有插入,更新或選擇命令probs,讓我說我有一個字典,我想填充一個表中的字典中的列名與我的一行添加一列的錯誤是什麼?如何添加列到sqlite3 python?

##create 
con = sqlite3.connect('linksauthor.db') 
c = con.cursor() 
c.execute('''create table linksauthor (links text)''') 
con.commit() 
c.close() 
##populate author columns 
allauthors={'joe':1,'bla':2,'mo':3} 
con = sqlite3.connect('linksauthor.db') 
c = con.cursor() 
for author in allauthors: 
    print author 
    print type(author) 
    c.execute("alter table linksauthor add column '%s' 'float'")%author ##what is wrong here? 
    con.commit() 
c.close() 

回答

14

Your paren is misplaced。您可能認爲是這樣的:

c.execute("alter table linksauthor add column '%s' 'float'" % author) 
+0

omg,謝謝lmao,我盯着代碼顯然太長 – user291071 2010-04-22 18:52:15

3

您還在使用字符串作爲列名和類型名稱。 Sqlite是非常寬容的,但你真的應該使用雙引號作爲標識符的引用字符。