4
4
5
5
6
6
class Table :
7
- def __init__ (self , name : str , cursor : sqlite3 .Cursor , connector : sqlite3 .Connection ):
8
- self .__name = name
7
+ def __init__ (self , tablename : str , cursor : sqlite3 .Cursor , connector : sqlite3 .Connection ):
8
+ self .tablename = tablename
9
9
self .__cursor = cursor
10
10
self .__connector = connector
11
11
self .__primary_key = None
@@ -21,27 +21,27 @@ def column_names(self) -> List[str]:
21
21
This property returns a list of column names
22
22
"""
23
23
if not self .__is_loaded :
24
- raise Exception (f"DataBase \' { self .__name } \' is not exist!. Try using \' Table.create_table\' ." )
24
+ raise Exception (f"DataBase \' { self .tablename } \' is not exist!. Try using \' Table.create_table\' ." )
25
25
return list (self .__table_labels .keys ())
26
26
27
- def create_table (self , labels : Dict [str , DBType ], primary_key : str = None ) -> None :
27
+ def create_table (self , columns : Dict [str , DBType ], primary_key : str = None ) -> None :
28
28
"""
29
29
This method creates a table with the specified columns. If "primary_key" is not specified, then the first
30
30
column will be considered the identifier, otherwise - the one selected by the user.
31
- :param labels : A dictionary where the key is the column name and the value is the data type
31
+ :param columns : A dictionary where the key is the column name and the value is the data type
32
32
:param primary_key: The column that will be considered the key
33
33
"""
34
34
if self .__is_loaded :
35
35
raise Exception ("DataBase is already exist!" )
36
36
if primary_key is not None :
37
- if primary_key not in labels :
37
+ if primary_key not in columns :
38
38
raise Exception (f"Column \' { primary_key } \' not exist in table labels!" )
39
39
self .__primary_key = primary_key
40
40
else :
41
- self .__primary_key = list (labels .keys ())[0 ]
42
- self .__cursor .execute (f"CREATE TABLE IF NOT EXISTS { self .__name } ({ Table .__prep_labels (labels )} )" )
41
+ self .__primary_key = list (columns .keys ())[0 ]
42
+ self .__cursor .execute (f"CREATE TABLE IF NOT EXISTS { self .tablename } ({ Table .__prep_labels (columns )} )" )
43
43
self .__connector .commit ()
44
- self .__table_labels = labels
44
+ self .__table_labels = columns
45
45
self .__is_loaded = True
46
46
47
47
def get_from_cell (self , key : str , column_name : str ) -> Any :
@@ -52,10 +52,10 @@ def get_from_cell(self, key: str, column_name: str) -> Any:
52
52
:param column_name: The name of the column from which the value should be returned
53
53
"""
54
54
if not self .__is_loaded :
55
- raise Exception (f"DataBase \' { self .__name } \' is not exist!. Try using \' Table.create_table\' ." )
55
+ raise Exception (f"DataBase \' { self .tablename } \' is not exist!. Try using \' Table.create_table\' ." )
56
56
if column_name not in self .__table_labels :
57
57
raise Exception (f"Column \' { column_name } \' not exist in table labels!" )
58
- self .__cursor .execute (f"SELECT { column_name } FROM { self .__name } WHERE { self .__primary_key } = '{ key } '" )
58
+ self .__cursor .execute (f"SELECT { column_name } FROM { self .tablename } WHERE { self .__primary_key } = '{ key } '" )
59
59
return self .__cursor .fetchone ()[0 ]
60
60
61
61
def set_to_cell (self , key : str , column_name : str , new_value : Any , commit : bool = True ) -> None :
@@ -67,10 +67,10 @@ def set_to_cell(self, key: str, column_name: str, new_value: Any, commit: bool =
67
67
:param commit: Is it worth committing (often a commit is needed 1 time in 10-100 operations)
68
68
"""
69
69
if not self .__is_loaded :
70
- raise Exception (f"DataBase \' { self .__name } \' is not exist!. Try using \' Table.create_table\' ." )
70
+ raise Exception (f"DataBase \' { self .tablename } \' is not exist!. Try using \' Table.create_table\' ." )
71
71
if column_name not in self .__table_labels :
72
72
raise Exception (f"Column \' { column_name } \' not exist in table labels!" )
73
- self .__cursor .execute (f"UPDATE { self .__name } SET { column_name } = '{ new_value } ' WHERE { self .__primary_key } = '{ key } '" )
73
+ self .__cursor .execute (f"UPDATE { self .tablename } SET { column_name } = '{ new_value } ' WHERE { self .__primary_key } = '{ key } '" )
74
74
if commit :
75
75
self .__connector .commit ()
76
76
@@ -81,12 +81,12 @@ def add_row(self, row: list, commit: bool = True) -> None:
81
81
:param commit: Is it worth committing (often a commit is needed 1 time in 10-100 operations)
82
82
"""
83
83
if not self .__is_loaded :
84
- raise Exception (f"DataBase \' { self .__name } \' is not exist!. Try using \' Table.create_table\' ." )
84
+ raise Exception (f"DataBase \' { self .tablename } \' is not exist!. Try using \' Table.create_table\' ." )
85
85
if len (row ) != len (self .__table_labels ):
86
86
raise Exception (f"There are only { len (self .__table_labels )} columns in the database "
87
- f"\' { self .__name } \' , and you are trying to write { len (row )} " )
87
+ f"\' { self .tablename } \' , and you are trying to write { len (row )} " )
88
88
values = ", " .join (["'" + str (i ) + "'" for i in row ])
89
- self .__cursor .execute (f"INSERT INTO { self .__name } VALUES ({ values } )" )
89
+ self .__cursor .execute (f"INSERT INTO { self .tablename } VALUES ({ values } )" )
90
90
if commit :
91
91
self .__connector .commit ()
92
92
@@ -96,8 +96,8 @@ def get_row(self, key: str) -> Dict[str, Any]:
96
96
:param key: Unique identifier
97
97
"""
98
98
if not self .__is_loaded :
99
- raise Exception (f"DataBase \' { self .__name } \' is not exist!. Try using \' Table.create_table\' ." )
100
- self .__cursor .execute (f"SELECT * FROM { self .__name } WHERE { self .__primary_key } = '{ key } '" )
99
+ raise Exception (f"DataBase \' { self .tablename } \' is not exist!. Try using \' Table.create_table\' ." )
100
+ self .__cursor .execute (f"SELECT * FROM { self .tablename } WHERE { self .__primary_key } = '{ key } '" )
101
101
request = self .__cursor .fetchall ()
102
102
if len (request ) == 0 :
103
103
raise Exception ("There are no values for this query!" )
@@ -112,8 +112,8 @@ def delete_row(self, key: str) -> None:
112
112
:param key: Unique identifier
113
113
"""
114
114
if not self .__is_loaded :
115
- raise Exception (f"DataBase \' { self .__name } \' is not exist!. Try using \' Table.create_table\' ." )
116
- self .__cursor .execute (f"DELETE FROM { self .__name } WHERE { self .__primary_key } = '{ key } '" )
115
+ raise Exception (f"DataBase \' { self .tablename } \' is not exist!. Try using \' Table.create_table\' ." )
116
+ self .__cursor .execute (f"DELETE FROM { self .tablename } WHERE { self .__primary_key } = '{ key } '" )
117
117
self .__connector .commit ()
118
118
119
119
def get_column (self , column_name : str ) -> List [Any ]:
@@ -122,19 +122,19 @@ def get_column(self, column_name: str) -> List[Any]:
122
122
:param column_name: Column name
123
123
"""
124
124
if not self .__is_loaded :
125
- raise Exception (f"DataBase \' { self .__name } \' is not exist!. Try using \' Table.create_table\' ." )
125
+ raise Exception (f"DataBase \' { self .tablename } \' is not exist!. Try using \' Table.create_table\' ." )
126
126
if column_name not in self .__table_labels :
127
127
raise Exception (f"Column \' { column_name } \' not exist in table labels!" )
128
- self .__cursor .execute (f"SELECT { column_name } FROM { self .__name } " )
128
+ self .__cursor .execute (f"SELECT { column_name } FROM { self .tablename } " )
129
129
return [sfa [0 ] for sfa in self .__cursor .fetchall ()]
130
130
131
131
def get_all_keys (self ) -> List [Any ]:
132
132
"""
133
133
This method returns all values of all identifiers (a column whose values are unique for each row)
134
134
"""
135
135
if not self .__is_loaded :
136
- raise Exception (f"DataBase \' { self .__name } \' is not exist!. Try using \' Table.create_table\' ." )
137
- self .__cursor .execute (f"SELECT { self .__primary_key } FROM { self .__name } " )
136
+ raise Exception (f"DataBase \' { self .tablename } \' is not exist!. Try using \' Table.create_table\' ." )
137
+ self .__cursor .execute (f"SELECT { self .__primary_key } FROM { self .tablename } " )
138
138
return [sfa [0 ] for sfa in self .__cursor .fetchall ()]
139
139
140
140
def commit (self ) -> None :
0 commit comments