-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
43 lines (37 loc) · 1.22 KB
/
database.py
File metadata and controls
43 lines (37 loc) · 1.22 KB
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
# database.py
import sqlite3
from datetime import datetime
def initialize_db():
conn = sqlite3.connect('scraped_data.db')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS table_data (
year TEXT,
goverment TEXT,
country TEXT,
report TEXT,
language TEXT,
state TEXT,
timestamp TEXT,
UNIQUE(year,goverment,country,report,language,state)
)
''')
conn.commit()
conn.close()
def insert_new_data(year,goverment,country,report,language,state):
"""Insert new data into the SQLite database if it doesn't already exist."""
conn = sqlite3.connect('scraped_data.db')
c = conn.cursor()
# Get the current timestamp
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
try:
# Insert data with a UNIQUE constraint on column_1, column_2, and column_3
c.execute('''
INSERT OR IGNORE INTO table_data (year,goverment,country,report,language,state,timestamp)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (year,goverment,country,report,language,state,timestamp))
conn.commit()
except sqlite3.Error as e:
print(f"Error inserting data: {e}")
finally:
conn.close()