-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite3_funcs.py
More file actions
executable file
·79 lines (68 loc) · 2.73 KB
/
sqlite3_funcs.py
File metadata and controls
executable file
·79 lines (68 loc) · 2.73 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
from typing import List, Any
try:
import sqlite3
except ImportError as e:
raise e
class Connection:
"""
Connection is a wrapper class for sqlite3.Connection. It provides the following methods:
->get_table(table) - returns all records from the specified table
->execute(sql) - runs sqlite3.Connection.cursor().execute(sql)
->get_table_names() - returns a list containing the names of tables in the connected database
->get_number_of_rows(table) - returns number of records in the specified table
->create_table(name, fields) - creates a table with the name specified and with the columns and their respective
"""
def __init__(self, db_path: str) -> None:
try:
self.connection = sqlite3.connect(db_path)
except Exception as e:
raise e
self.cursor = self.connection.cursor()
def get_table(self, table: str) -> List[Any]:
"""Returns all records from the specified table."""
try:
return self.cursor.execute("SELECT * FROM " + table).fetchall()
except Exception as e:
raise e
def execute(self, sql: str, **kwargs) -> sqlite3.Cursor:
"""Executes a query."""
try:
return self.cursor.execute(sql, kwargs)
except Exception as e:
raise e
def get_table_names(self) -> List[Any]:
"""Returns a list of all tables in a database"""
return [x[0] for x in self.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall()]
def get_number_of_rows(self, table: str) -> int:
"""Returns the number of records in the specified table"""
return int(self.execute("SELECT Count(*) FROM " + table).fetchall()[0][0])
def create_table(self, name: str, fields: dict) -> None:
"""Creates a table"""
try:
command = f"CREATE TABLE IF NOT EXISTS {name} ("
i = 0
for key, value in fields.items():
command += f"{str(key)} {str(value)}"
if i == len(fields) - 1:
pass
else:
command += ","
i += 1
command += ")"
self.execute(command)
except sqlite3.OperationalError as e:
raise e
except Exception as e:
raise e
def get_columns(self, table: str) -> List[Any]:
"""Returns the columns or fields in the specified table"""
try:
data = self.cursor.execute(f"SELECT * FROM {table}")
columns = []
for x in data.description:
columns.append(x[0])
return columns
except Exception as e:
raise e
def close(self) -> None:
self.connection.close()