我可以成功地使用Python创建数据库并运行execute()方法来创建2个新表并指定列名。但是,我无法将数据插入数据库。这是我想使用的数据插入到数据库的代码:无法使用Python将数据插入到sqlite3数据库中
#! /usr/bin/env python
import sqlite3
companies = ('GOOG', 'AAPL', 'MSFT')
db = sqlite3.connect('data.db')
c = db.cursor()
for company in companies:
c.execute('INSERT INTO companies VALUES (?)', (company,))
下面是我用成功创建数据库的代码:
#! /usr/bin/env python
import sqlite3
db = sqlite3.connect('data.db')
db.execute('CREATE TABLE companies ' \
'('\
'company varchar(255) '\
')')
db.execute('CREATE TABLE data ' \
'('\
'timestamp int, '\
'company int, '\
'shares_held_by_all_insider int, '\
'shares_held_by_institutional int, '\
'float_held_by_institutional int, '\
'num_institutions int '\
')')
它为我工作得很好。你有什么错误吗? – 2009-07-29 16:44:23
没有,除了记录没有被添加到表格这一事实之外,一切似乎都运行良好。 – 2009-07-29 17:19:05