-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinit_db.py
46 lines (40 loc) · 1.43 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
#
# twitterbot2
#
# edoardottt
# edoardoottavianelli.it
# https://github.com/edoardottt/twitterbot2
#
# This repository is under GPL-3 License.
#
# This file, when executed, creates a SQlite3 database
# called 'database.db' and creates a table called 'statistics' that
# stores the data collected during the twitterbot2 usage.
# (The database is created only if it does not already exist)
#
import os
import sqlite3
import logging
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
# create table with the sql code
def create_table(conn, create_table_sql):
try:
c = conn.cursor()
c.execute(create_table_sql)
except Exception as e:
logger = logging.getLogger("__main__")
logger.error(e)
sql_create_statistics_table = """CREATE TABLE IF NOT EXISTS statistics (
username text NOT NULL,
date date NOT NULL,
tweets integer NOT NULL,
likes integer NOT NULL,
retweets integer NOT NULL,
followers integer NOT NULL,
PRIMARY KEY (username,date)
);"""
if conn is not None:
create_table(conn, sql_create_statistics_table)
conn.close()