-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.py
More file actions
35 lines (31 loc) · 1.03 KB
/
database.py
File metadata and controls
35 lines (31 loc) · 1.03 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
import sqlite3
class Database:
def __init__(self):
self.conn = sqlite3.connect("scraped_data.db")
self.create_table()
def create_table(self):
try:
cursor = self.conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS scraped_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
url TEXT,
data TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
self.conn.commit()
except sqlite3.Error as e:
print("Error creating table:", str(e))
def insert_data(self, url, data):
try:
cursor = self.conn.cursor()
cursor.execute("""
INSERT INTO scraped_data (url, data)
VALUES (?, ?)
""", (url, data))
self.conn.commit()
except sqlite3.Error as e:
print("Error inserting data:", str(e))
def close_connection(self):
self.conn.close()