我似乎找到了很多关于如何使用两个表的教程,但我似乎无法弄清楚如何创建两个表。我可能错过了很简单的事情。
我想为my_data_1和my_data_2创建一个表格。这是我的代码:
import sqlite3
my_data_1 = [('a',1,'BUY'),('b',2,'SELL'),('c',3,'HOLD')]
my_data_2 = [('a',1,5),('d',6,6),('e',2,7)]
#I am using :memory: because I want to experiment
#with the database a lot
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('''CREATE TABLE MY_TABLE_1
(stock TEXT, price REAL, recommendation TEXT )''' )
### Something is probably wrong with the following line
c.execute('''CREATE TABLE MY_TABLE_2
(stock TEXT, price REAL, volume REAL )''' )
for ele in my_data_1:
c.execute('''INSERT INTO MY_TABLE_1 VALUES(?,?,?)''',ele)
conn.commit()
c.execute('SELECT* FROM MY_TABLE_1')
for entry in c:
print entry
c.execute('SELECT* FROM MY_TABLE_2')
for entry in c:
print entry
我的输出是:
(u'a', 1.0, u'BUY')
(u'b', 2.0, u'SELL')
(u'c', 3.0, u'HOLD')
所以我没有创建MY_TABLE_2。我该怎么做?
先谢谢你。
答案 0 :(得分:2)
您没有在表2中插入任何内容
插入表1后尝试使用此代码。
for ele in my_data_2:
c.execute('''INSERT INTO MY_TABLE_2 VALUES(?,?,?)''',ele)