4
4
from .utils import DATA_TYPES
5
5
6
6
class ReallySimpleDB :
7
- """ ReallySimpleDB class
7
+ """
8
+ ReallySimpleDB class.
8
9
9
10
ReallySimpleDB objects are the ones responsible of creating DBs, connecting
10
11
with them, creating tables, adding records, geting records, among tasks. In
11
12
more cases these should be one per database.
12
13
"""
13
14
14
15
def __init__ (self ) -> None :
15
- """
16
- create a object
17
- """
16
+ """Create a object."""
18
17
self ._add_columns_cmd = ""
19
18
self .connection = ""
20
19
21
20
def clean (self ):
22
21
"""
23
- cleans add_columns data
22
+ Clean add_columns data.
24
23
25
- why ? _add_columns_cmd variable is for define SQL command. when using add_column,
24
+ Why ? _add_columns_cmd variable is for define SQL command. when using add_column,
26
25
it sets up a string here. but when it is finished this is not clean and the data
27
26
continues to exist. when use add_column again and again, it will be processed
28
27
along with the existing data. this should be used to prevent it.
29
28
"""
30
29
self ._add_columns_cmd = ""
31
30
32
31
def create_connection (self , database ):
33
- """opens a connection to the SQLite database file"""
32
+ """Open a connection to the SQLite database file. """
34
33
self .connection = sqlite3 .connect (database )
35
34
return True
36
35
37
36
def create_db (self , dbpath :str = "" , replace :bool = False ):
38
- """creates a new database in a given path"""
37
+ """Create a new database in a given path. """
39
38
if self .connection == "" and not dbpath :
40
39
raise TypeError ("create_db() missing 1 required positional argument: 'dbpath'" )
41
40
@@ -61,14 +60,13 @@ def add_columns(self,
61
60
database :str = "" ,
62
61
table :str = "" ):
63
62
"""
64
- add columns to an existing table / define columns before creating a table
63
+ Add columns to an existing table / define columns before creating a table.
65
64
66
- if use for create new table: sqlite cannot create table without columns.
65
+ If use for create new table: sqlite cannot create table without columns.
67
66
so user must first define the columns and create a table.
68
67
important: user have to close connection here. if not, code returns error.
69
68
because it tries to add column to existing table.
70
69
"""
71
-
72
70
# checks if the user is trying to add unsupported data type
73
71
if datatype .upper () not in DATA_TYPES :
74
72
raise TypeError ("datatype not supported, '{}'" .format (datatype ))
@@ -81,7 +79,7 @@ def add_columns(self,
81
79
# column to an existing table.
82
80
self .create_connection (database = database )
83
81
cursor = self .connection .cursor ()
84
- sql_cmd = "ALTER TABLE {} ADD COLUMN {} {}" . format ( table , column_name , datatype )
82
+ sql_cmd = "ALTER TABLE " + table + " ADD COLUMN " + column_name + " " + datatype
85
83
if not_null :
86
84
sql_cmd += " NOT NULL"
87
85
if primary_key :
@@ -92,7 +90,7 @@ def add_columns(self,
92
90
# if table is not defines, it means that the user is trying to add / define
93
91
# a column to a new table. so the following code add SQL syntax globally for
94
92
# use when creating new table
95
- self ._add_columns_cmd += ( ",{} {}" . format ( column_name , datatype ))
93
+ self ._add_columns_cmd += "," + column_name + " " + datatype
96
94
97
95
if primary_key :
98
96
self ._add_columns_cmd += " PRIMARY KEY"
@@ -103,7 +101,7 @@ def add_columns(self,
103
101
return True
104
102
105
103
def create_table (self , table_name :str , database :str = "" ):
106
- """creates new table in database"""
104
+ """Create new table in database. """
107
105
if self .connection == "" and not database :
108
106
raise TypeError ("create_table() missing 1 required positional argument: 'database'" )
109
107
@@ -116,13 +114,13 @@ def create_table(self, table_name:str, database:str=""):
116
114
if self ._add_columns_cmd == "" :
117
115
raise NotImplementedError ("call 'add_columns' function before create table" )
118
116
119
- sql_cmd = "CREATE TABLE {} ({})" . format ( table_name , self ._add_columns_cmd [1 :])
117
+ sql_cmd = "CREATE TABLE " + table_name + " (" + self ._add_columns_cmd [1 :] + ")"
120
118
121
119
self .connection .execute (sql_cmd )
122
120
return True
123
121
124
122
def all_tables (self , database :str = "" ):
125
- """get a list of all the tables in the database"""
123
+ """Get a list of all the tables in the database. """
126
124
if self .connection == "" and not database :
127
125
raise TypeError ("all_tables() missing 1 required positional argument: 'database'" )
128
126
@@ -134,7 +132,7 @@ def all_tables(self, database:str=""):
134
132
return [tables [0 ] for tables in cursor .execute (sql_cmd )]
135
133
136
134
def is_table (self , table_name :str , database :str = "" ):
137
- """checks if the given table is exists in the database"""
135
+ """Check if the given table is exists in the database. """
138
136
if self .connection == "" and not database :
139
137
raise TypeError ("is_table() missing 1 required positional argument: 'database'" )
140
138
@@ -146,7 +144,7 @@ def is_table(self, table_name:str, database:str=""):
146
144
return False
147
145
148
146
def delete_table (self , table :str , database :str = "" ):
149
- """delete a table from the database"""
147
+ """Delete a table from the database. """
150
148
if self .connection == "" and not database :
151
149
raise TypeError ("delete_table() missing 1 required positional argument: 'database'" )
152
150
@@ -155,7 +153,7 @@ def delete_table(self, table:str, database:str=""):
155
153
156
154
if self .is_table (table_name = table ):
157
155
cursor = self .connection .cursor ()
158
- sql_cmd = "DROP TABLE {};" . format ( table )
156
+ sql_cmd = "DROP TABLE " + table + ";"
159
157
cursor .execute (sql_cmd )
160
158
161
159
return True
@@ -164,7 +162,7 @@ def delete_table(self, table:str, database:str=""):
164
162
raise sqlite3 .OperationalError ("no such table: {}" .format (table ))
165
163
166
164
def get_all_column_types (self , table :str , database :str = "" ):
167
- """get all the column names with the data types in a table"""
165
+ """Get all the column names with the data types in a table. """
168
166
if self .connection == "" and not database :
169
167
raise TypeError (
170
168
"get_all_column_types() missing 1 required positional argument: 'database'" )
@@ -175,7 +173,7 @@ def get_all_column_types(self, table:str, database:str=""):
175
173
if self .is_table (table_name = table , database = database ):
176
174
cursor = self .connection .cursor ()
177
175
178
- sql_cmd = "PRAGMA TABLE_INFO({});" . format ( table )
176
+ sql_cmd = "PRAGMA TABLE_INFO(" + table + ");"
179
177
fetch = cursor .execute (sql_cmd )
180
178
181
179
data_dict = {}
@@ -188,7 +186,7 @@ def get_all_column_types(self, table:str, database:str=""):
188
186
raise sqlite3 .OperationalError ("no such table: {}" .format (table ))
189
187
190
188
def get_column_type (self , table :str , column :str , database :str = "" ):
191
- """get data type of a column in a table"""
189
+ """Get data type of a column in a table. """
192
190
all_data = self .get_all_column_types (table = table , database = database )
193
191
194
192
# if columns exists in the table and given column in the table
@@ -198,7 +196,7 @@ def get_column_type(self, table:str, column:str, database:str=""):
198
196
raise sqlite3 .OperationalError ("no such column: {}" .format (column ))
199
197
200
198
def get_columns (self , table :str , database :str = "" ):
201
- """get all the column names list in a table"""
199
+ """Get all the column names list in a table. """
202
200
if self .connection == "" and not database :
203
201
raise TypeError ("get_columns() missing 1 required positional argument: 'database'" )
204
202
@@ -215,7 +213,7 @@ def get_columns(self, table:str, database:str=""):
215
213
return columns
216
214
217
215
def get_primary_key (self , table :str , database :str = "" ):
218
- """find and get primary key of a table"""
216
+ """Find and get primary key of a table. """
219
217
if self .connection == "" and not database :
220
218
raise TypeError ("get_primary_key() missing 1 required positional argument: 'database'" )
221
219
@@ -225,15 +223,15 @@ def get_primary_key(self, table:str, database:str=""):
225
223
if self .is_table (table_name = table , database = database ):
226
224
cursor = self .connection .cursor ()
227
225
228
- sql_cmd = "SELECT * FROM pragma_table_info('{}' ) WHERE pk;" . format ( table )
229
- fetch = cursor .execute (sql_cmd )
226
+ sql_cmd = "SELECT * FROM pragma_table_info(? ) WHERE pk;"
227
+ fetch = cursor .execute (sql_cmd , ( table ,) )
230
228
return fetch .fetchall ()[0 ][1 ]
231
229
232
230
# raise OperationalError if the given table not exists
233
231
raise sqlite3 .OperationalError ("no such table: {}" .format (table ))
234
232
235
233
def add_record (self , table :str , record , database :str = "" ):
236
- """add a new record to a table"""
234
+ """Add a new record to a table. """
237
235
if self .connection == "" and not database :
238
236
raise TypeError ("add_record() missing 1 required positional argument: 'database'" )
239
237
@@ -252,7 +250,7 @@ def add_record(self, table:str, record, database:str=""):
252
250
all_columns [column ] = ""
253
251
254
252
fields = []
255
- sql_cmd = "INSERT INTO {} VALUES(" . format ( table )
253
+ sql_cmd = "INSERT INTO " + table + " VALUES("
256
254
257
255
# if record is dict type,..
258
256
if isinstance (record , dict ):
@@ -288,7 +286,7 @@ def add_record(self, table:str, record, database:str=""):
288
286
raise sqlite3 .OperationalError ("no such table: {}" .format (table ))
289
287
290
288
def get_record (self , table :str , primary_key , database :str = "" ):
291
- """get row data / record from a table using the primary key"""
289
+ """Get row data / record from a table using the primary key. """
292
290
if self .connection == "" and not database :
293
291
raise TypeError ("get_record() missing 1 required positional argument: 'database'" )
294
292
@@ -298,8 +296,7 @@ def get_record(self, table:str, primary_key, database:str=""):
298
296
if self .is_table (table_name = table , database = database ):
299
297
cursor = self .connection .cursor ()
300
298
301
- sql_cmd = "SELECT * FROM {} WHERE {}=?;" .format (
302
- table , self .get_primary_key (table = table , database = database ))
299
+ sql_cmd = "SELECT * FROM " + table + " WHERE " + self .get_primary_key (table = table , database = database ) + "=?;"
303
300
fetch = cursor .execute (sql_cmd , (primary_key ,))
304
301
305
302
# get columns list using get_columns
@@ -321,7 +318,7 @@ def get_record(self, table:str, primary_key, database:str=""):
321
318
raise sqlite3 .OperationalError ("no such table: {}" .format (table ))
322
319
323
320
def get_all_records (self , table :str , database :str = "" ):
324
- """get all data / records of a table"""
321
+ """Get all data / records of a table. """
325
322
if self .connection == "" and not database :
326
323
raise TypeError ("get_all_records() missing 1 required positional argument: 'database'" )
327
324
@@ -331,7 +328,7 @@ def get_all_records(self, table:str, database:str=""):
331
328
if self .is_table (table_name = table , database = database ):
332
329
cursor = self .connection .cursor ()
333
330
334
- sql_cmd = "SELECT * FROM {}" . format ( table )
331
+ sql_cmd = "SELECT * FROM " + table
335
332
cursor .execute (sql_cmd )
336
333
rows = cursor .fetchall ()
337
334
@@ -353,7 +350,7 @@ def get_all_records(self, table:str, database:str=""):
353
350
raise sqlite3 .OperationalError ("no such table: {}" .format (table ))
354
351
355
352
def delete_record (self , table :str , primary_key , database :str = "" ):
356
- """delete record from a table"""
353
+ """Delete record from a table. """
357
354
if self .connection == "" and not database :
358
355
raise TypeError ("delete_record() missing 1 required positional argument: 'database'" )
359
356
@@ -362,8 +359,7 @@ def delete_record(self, table:str, primary_key, database:str=""):
362
359
363
360
if self .is_table (table_name = table , database = database ):
364
361
cursor = self .connection .cursor ()
365
- sql = "DELETE FROM {} WHERE {}=?" .format (
366
- table , self .get_primary_key (table = table , database = database ))
362
+ sql = "DELETE FROM " + table + " WHERE " + self .get_primary_key (table = table , database = database ) + "=?"
367
363
cursor .execute (sql , (primary_key ,))
368
364
self .connection .commit ()
369
365
@@ -374,9 +370,9 @@ def delete_record(self, table:str, primary_key, database:str=""):
374
370
375
371
def filter_records (self , table :str , values :dict , database :str = "" ):
376
372
"""
377
- get filtered record list from a table
373
+ Get filtered record list from a table.
378
374
379
- this will return one or more records by checking the values.
375
+ This will return one or more records by checking the values.
380
376
"""
381
377
if self .connection == "" and not database :
382
378
raise TypeError ("filter_records() missing 1 required positional argument: 'database'" )
@@ -389,7 +385,7 @@ def filter_records(self, table:str, values:dict, database:str=""):
389
385
390
386
operators = [">" , "<" , "!" , "=" ]
391
387
392
- sql = "SELECT * FROM {} WHERE " . format ( table )
388
+ sql = "SELECT * FROM " + table + " WHERE "
393
389
394
390
for value in values :
395
391
try :
@@ -427,6 +423,6 @@ def filter_records(self, table:str, values:dict, database:str=""):
427
423
raise sqlite3 .OperationalError ("no such table: {}" .format (table ))
428
424
429
425
def close_connection (self ):
430
- """close the connection with the SQLite database file"""
426
+ """Close the connection with the SQLite database file. """
431
427
self .connection .close ()
432
428
return True
0 commit comments