[英]Create two tables in sqlite3 database in Python
I seem to find many tutorials on how to work with two table, but I can't seem to figure out how to create two tables. 我似乎找到了很多关于如何使用两个表的教程,但我似乎无法弄清楚如何创建两个表。 I am probably missing something very simple. 我可能错过了很简单的事情。
I want to create a table for my_data_1 and my_data_2. 我想为my_data_1和my_data_2创建一个表。 Here is my code: 这是我的代码:
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
My output is: 我的输出是:
(u'a', 1.0, u'BUY')
(u'b', 2.0, u'SELL')
(u'c', 3.0, u'HOLD')
So I have not created MY_TABLE_2. 所以我还没有创建MY_TABLE_2。 How should I do this? 我该怎么做?
Thank You in Advance. 先感谢您。
You aren't inserting anything into Table 2 您没有在表2中插入任何内容
Try this code after you insert into Table 1. 插入表1后尝试使用此代码。
for ele in my_data_2:
c.execute('''INSERT INTO MY_TABLE_2 VALUES(?,?,?)''',ele)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.