· 6 years ago · Jul 16, 2019, 03:14 AM
1import sqlite3
2
3# connect function opens a connection to the SQLite database file,
4conn = sqlite3.connect('session.db')
5print(conn)
6# Output: <sqlite3.Connection object at 0x0000015A87671730>
7
8# Drop a table name Crypto if it exists already
9try:
10 conn.execute('DROP TABLE IF EXISTS `Crypto` ')
11except Exception as e:
12 raise(e)
13finally:
14 print('Table dropped')
15
16
17# Create a new Table named as Crypto
18try:
19 conn.execute('''
20 CREATE TABLE Crypto
21 (ID INTEGER PRIMARY KEY,
22 NAME TEXT NOT NULL,
23 Date datetime,
24 Open Float DEFAULT 0,
25 High Float DEFAULT 0,
26 Low Float DEFAULT 0,
27 Close Float DEFAULT 0);''')
28 print ("Table created successfully");
29except Exception as e:
30 print(str(e))
31 print('Table Creation Failed!!!!!')
32finally:
33 conn.close() # this closes the database connection