-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathinit_db.py
executable file
·52 lines (37 loc) · 1.5 KB
/
init_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 8 13:41:22 2019
@author: edoardottt
Initialization of database.
This file is under MIT License.
"""
import os
import sqlite3
db_filename = "database.db"
db_is_new = not os.path.exists(db_filename)
conn = sqlite3.connect(db_filename) # connect to the database or create it
# ctreate table with the sql code input
def create_table(conn, create_table_sql):
try:
c = conn.cursor()
c.execute(create_table_sql)
except Exception as e:
print(e)
sql_create_users_table = """ CREATE TABLE IF NOT EXISTS users (
username text PRIMARY KEY,
password text NOT NULL
); """
sql_create_analytics_table = """CREATE TABLE IF NOT EXISTS analytics (
username text NOT NULL,
date date NOT NULL,
likes integer NOT NULL,
retweets integer NOT NULL,
followers integer NOT NULL,
PRIMARY KEY (username,date)
FOREIGN KEY (username) REFERENCES users (username)
);"""
if conn is not None:
create_table(conn, sql_create_users_table)
create_table(conn, sql_create_analytics_table)
conn.close()