繁体   English   中英

如何将列添加到sqlite3 python?

[英]How to add columns to sqlite3 python?

我知道这很简单,但我不能让它工作! 我没有插入,更新或选择命令的probs,让我说我有一个字典,我想用字典中的列名填充一个表,我添加一行的一行有什么问题?

##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()

你的paren是错位的。 你可能意味着这个:

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

您还使用字符串作为列名和类型名称。 Sqlite非常宽容,但你真的应该使用双引号作为标识符的引号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM