· 9 years ago · Jan 24, 2017, 07:08 PM
1import sqlite3
2from yahoo_finance import Share
3
4conn = sqlite3.connect('C:/Temp/stock.sqlite')
5c = conn.cursor()
6
7def create_table():
8 create_db = input('Would you like to create database? Yes or No:')
9 if create_db == 'yes':
10 print("Creating Database...")
11 c.execute('CREATE TABLE IF NOT EXISTS stock (name TEXT, symbol TEXT)')
12 conn.commit()
13 elif create_db == 'no':
14 print("Skipping Database creation\n")
15
16def sql_close():
17 c.close()
18 conn.close()
19
20def add_node():
21 commit = False
22
23 while commit == False:
24 print("Adding a new entry")
25 stock_name = input('Stock name: ')
26 stock_symbol = input('Stock symbol: ')
27
28 user_prompt = input("Would you like to add %s, - %s? (Y)es or (N)o: " % (stock_name, stock_symbol))
29 if user_prompt == 'y':
30 c.execute("INSERT INTO stock (name, symbol) VALUES (?, ?)",
31 (stock_name, stock_symbol))
32 conn.commit()
33
34 add_another = input("Woud you like to add another entry?")
35 if add_another == 'y':
36 commit = False
37 elif add_another == 'n':
38 commit = True
39
40 elif user_prompt == 'no':
41 commit = True
42 # print('BREAK OUT')
43 # break
44
45def read_from_db():
46 print("Printing current entries")
47 c.execute('SELECT * FROM stock')
48 for row in c.fetchall():
49 print('Stock %s, - Symbol %s' % (row[0], row[1]))
50 print('\n')
51
52def prompt_user():
53 question = input('Would you like to (A)dd Symbols, (V)iew Symbols, D(elete) Symbols, (G)et Quotes?')
54 if question == 'a':
55 add_node()
56 prompt_user()
57 elif question == 'v':
58 read_from_db()
59 prompt_user()
60 elif question == 'g':
61 get_quotes()
62 prompt_user()
63 elif question == 'd':
64 print("no delete yet")
65
66def get_quotes():
67 # print("Printing current entries")
68 c.execute('SELECT * FROM stock')
69 for stockprice in c.fetchall():
70 shareprice = Share(stockprice[1])
71 print('Share price for', stockprice[0], 'is:', shareprice.get_price())
72
73# create_table()
74# read_from_db()
75# add_node()
76
77try:
78 c.execute('CREATE TABLE IF NOT EXISTS stock (name TEXT, symbol TEXT)')
79 conn.commit()
80except:
81 print('database already exists')
82
83prompt_user()
84sql_close()