0

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. 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. How should I do this?

Thank You in Advance.

1 Answer 1

3

You aren't inserting anything into Table 2

Try this code after you insert into Table 1.

for ele in my_data_2:
    c.execute('''INSERT INTO MY_TABLE_2 VALUES(?,?,?)''',ele)
Sign up to request clarification or add additional context in comments.

1 Comment

I knew I was missing something simple :). Thank You.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.