| 10 | |
| 11 | |
| 12 | class SQLite(DataBase): |
| 13 | |
| 14 | @property |
| 15 | def principal_database(self)->str: |
| 16 | return "" |
| 17 | |
| 18 | def close(self): |
| 19 | if self._conn: |
| 20 | self._conn.close() |
| 21 | |
| 22 | def _get_db_file(self): |
| 23 | manager = SaveManager() |
| 24 | path = os.path.join(ResultSetting().output,DatabaseSetting().database_name, "db") |
| 25 | manager._create_directory(path) |
| 26 | return path |
| 27 | |
| 28 | def _get_cursor(self, database:str=None): |
| 29 | path_db = self._get_db_file() |
| 30 | db_file = os.path.join(path_db, database if database is not None else self._database) |
| 31 | conn = sqlite3.connect(db_file+".db") |
| 32 | self._conn = conn |
| 33 | return conn.cursor() |
| 34 | |
| 35 | def _execute(self, sentence, values=None): |
| 36 | cursor = self._get_cursor() |
| 37 | if values is not None: |
| 38 | cursor.execute(sentence, values) |
| 39 | else: |
| 40 | cursor.execute(sentence) |
| 41 | |
| 42 | self._conn.commit() |
| 43 | self._conn.close() |
| 44 | |
| 45 | def _select(self, sentence: str, values=None, showColumns = False): |
| 46 | cursor = self._get_cursor() |
| 47 | rows=[] |
| 48 | if values is not None: |
| 49 | cursor.execute(sentence, values) |
| 50 | else: |
| 51 | cursor.execute(sentence) |
| 52 | results = cursor.fetchall() |
| 53 | |
| 54 | if showColumns: |
| 55 | column_names = [desc[0] for desc in cursor.description] |
| 56 | for row in results: |
| 57 | row_with_column_names = {} |
| 58 | for idx, value in enumerate(row): |
| 59 | row_with_column_names[column_names[idx]] = value |
| 60 | rows.append(row_with_column_names) |
| 61 | else: |
| 62 | for row in results: |
| 63 | rows.append(row) |
| 64 | |
| 65 | self._conn.close() |
| 66 | return rows |
| 67 | |
| 68 | def _get_columns(self, tablename:str): |
| 69 | return self._get_columns_from_table(tablename) |