-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdb.py
More file actions
96 lines (83 loc) · 3.4 KB
/
sdb.py
File metadata and controls
96 lines (83 loc) · 3.4 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import sqlite3
class PrimaryKey:
pass
class Connection:
def __init__(self, file_path, log=False):
self.connection = sqlite3.connect(file_path)
self.cursor = self.connection.cursor()
if log:
print(f"Connected to database: {file_path}")
def close(self, log=False):
self.cursor.close()
self.connection.close()
if log:
print("Connection closed.")
class Table:
def __init__(self, connection, table_name):
self.connection = connection
self.cursor = self.connection.cursor()
self.table_name = table_name
def create(self, columns, log=False):
try:
query = f"CREATE TABLE IF NOT EXISTS {self.table_name} ("
for column_name, data_type in columns.items():
if data_type == str:
query += f"{column_name} TEXT, "
elif data_type == int:
query += f"{column_name} INTEGER, "
elif data_type == PrimaryKey:
query += f"{column_name} INTEGER PRIMARY KEY AUTOINCREMENT, "
else:
print(f"Invalid data type for column '{column_name}'.")
return
query = query.rstrip(", ") + ")"
self.cursor.execute(query)
if log:
print(f"Table '{self.table_name}' created successfully.")
except Exception as e:
print(f"Error creating table: {e}")
return self # Return the Table object itself
def drop(self, log=False):
try:
query = f"DROP TABLE IF EXISTS {self.table_name}"
self.cursor.execute(query)
if log:
print(f"Table '{self.table_name}' dropped successfully.")
except Exception as e:
print(f"Error dropping table: {e}")
def insert(self, values, log=False):
try:
query = f"INSERT INTO {self.table_name} VALUES ("
for value in values:
if isinstance(value, str):
query += f"'{value}', "
else:
query += f"{value}, "
query = query.rstrip(", ") + ")"
self.cursor.execute(query)
self.connection.commit()
if log:
print(f"Row inserted into table '{self.table_name}' successfully.")
except Exception as e:
print(f"Error inserting row: {e}")
def select(self, columns=None):
try:
if columns:
query = f"SELECT {', '.join(columns)} FROM {self.table_name}"
else:
query = f"SELECT * FROM {self.table_name}"
self.cursor.execute(query)
rows = self.cursor.fetchall()
return rows
except Exception as e:
print(f"Error selecting rows: {e}")
def close(self, log=False):
self.cursor.close()
self.connection.close()
if log:
print("Connection closed.")
def connect(file_path, log=False):
return Connection(file_path, log=log)
def table(self, table_name):
return self.Table(self.connection, table_name)
setattr(Connection, "table", table)