|
| 1 | +import sys |
| 2 | +import sqlite3 |
| 3 | +from PyQt5 import QtWidgets, QtSql, QtGui, QtCore |
| 4 | +from PyQt5.QtWidgets import QMainWindow, QFileDialog, QApplication, QMessageBox, QInputDialog |
| 5 | +from ui_main import Ui_MainWindow |
| 6 | + |
| 7 | +class MainWindow(QMainWindow, Ui_MainWindow): |
| 8 | + def __init__(self): |
| 9 | + super().__init__() |
| 10 | + self.setupUi(self) |
| 11 | + self.actionOpen.triggered.connect(self.open_database) |
| 12 | + self.searchButton.clicked.connect(self.search_data) |
| 13 | + self.addButton.clicked.connect(self.add_data) |
| 14 | + self.editButton.clicked.connect(self.edit_data) |
| 15 | + self.deleteButton.clicked.connect(self.delete_data) |
| 16 | + self.tablesListWidget.itemClicked.connect(self.display_table) |
| 17 | + self.db = None |
| 18 | + |
| 19 | + self.set_logo("ishakpasa.jpeg") |
| 20 | + |
| 21 | + def set_logo(self, logo_path): |
| 22 | + pixmap = QtGui.QPixmap(logo_path) |
| 23 | + self.logoLabel.setPixmap(pixmap.scaled(self.logoLabel.size(), QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation)) |
| 24 | + self.logoLabel.setScaledContents(True) |
| 25 | + |
| 26 | + def open_database(self): |
| 27 | + options = QFileDialog.Options() |
| 28 | + fileName, _ = QFileDialog.getOpenFileName(self, "Open SQLite Database", "", "SQLite Files (*.sqlite);;All Files (*)", options=options) |
| 29 | + if fileName: |
| 30 | + self.connect_to_database(fileName) |
| 31 | + |
| 32 | + def connect_to_database(self, db_file): |
| 33 | + if self.db: |
| 34 | + self.db.close() |
| 35 | + |
| 36 | + self.db = QtSql.QSqlDatabase.addDatabase('QSQLITE') |
| 37 | + self.db.setDatabaseName(db_file) |
| 38 | + |
| 39 | + if not self.db.open(): |
| 40 | + QMessageBox.critical(None, "Database Error", self.db.lastError().text()) |
| 41 | + return |
| 42 | + |
| 43 | + self.update_table_list() |
| 44 | + |
| 45 | + def update_table_list(self): |
| 46 | + query = QtSql.QSqlQuery("SELECT name FROM sqlite_master WHERE type='table';", self.db) |
| 47 | + self.tablesListWidget.clear() |
| 48 | + while query.next(): |
| 49 | + self.tablesListWidget.addItem(query.value(0)) |
| 50 | + |
| 51 | + def display_table(self, item): |
| 52 | + table_name = item.text() |
| 53 | + model = QtSql.QSqlTableModel(self, self.db) |
| 54 | + model.setTable(table_name) |
| 55 | + model.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange) |
| 56 | + model.select() |
| 57 | + self.tableView.setModel(model) |
| 58 | + |
| 59 | + def search_data(self): |
| 60 | + search_text = self.searchLineEdit.text() |
| 61 | + current_table = self.tablesListWidget.currentItem().text() if self.tablesListWidget.currentItem() else None |
| 62 | + if current_table: |
| 63 | + query = QtSql.QSqlQuery(f"SELECT * FROM {current_table} WHERE name LIKE '%{search_text}%'", self.db) |
| 64 | + model = QtSql.QSqlQueryModel() |
| 65 | + model.setQuery(query) |
| 66 | + self.tableView.setModel(model) |
| 67 | + |
| 68 | + def add_data(self): |
| 69 | + current_table = self.tablesListWidget.currentItem().text() if self.tablesListWidget.currentItem() else None |
| 70 | + if current_table: |
| 71 | + columns_query = QtSql.QSqlQuery(f"PRAGMA table_info({current_table})", self.db) |
| 72 | + columns = [] |
| 73 | + while columns_query.next(): |
| 74 | + columns.append(columns_query.value(1)) |
| 75 | + data = [] |
| 76 | + for col in columns: |
| 77 | + value, ok = QInputDialog.getText(self, "Add Data", f"Enter value for {col}:") |
| 78 | + if ok: |
| 79 | + data.append(value) |
| 80 | + else: |
| 81 | + return |
| 82 | + query = QtSql.QSqlQuery(self.db) |
| 83 | + query.prepare(f"INSERT INTO {current_table} ({', '.join(columns)}) VALUES ({', '.join(['?' for _ in columns])})") |
| 84 | + for i, val in enumerate(data): |
| 85 | + query.bindValue(i, val) |
| 86 | + if not query.exec_(): |
| 87 | + QMessageBox.critical(self, "Insert Error", query.lastError().text()) |
| 88 | + else: |
| 89 | + self.display_table(self.tablesListWidget.currentItem()) |
| 90 | + |
| 91 | + def edit_data(self): |
| 92 | + index = self.tableView.currentIndex() |
| 93 | + if not index.isValid(): |
| 94 | + QMessageBox.warning(self, "Edit Error", "Please select a cell to edit.") |
| 95 | + return |
| 96 | + new_value, ok = QInputDialog.getText(self, "Edit Data", "Enter new value:") |
| 97 | + if ok: |
| 98 | + model = self.tableView.model() |
| 99 | + model.setData(index, new_value) |
| 100 | + model.submitAll() |
| 101 | + |
| 102 | + def delete_data(self): |
| 103 | + index = self.tableView.currentIndex() |
| 104 | + if not index.isValid(): |
| 105 | + QMessageBox.warning(self, "Delete Error", "Please select a row to delete.") |
| 106 | + return |
| 107 | + model = self.tableView.model() |
| 108 | + model.removeRow(index.row()) |
| 109 | + model.submitAll() |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + app = QApplication(sys.argv) |
| 113 | + window = MainWindow() |
| 114 | + window.show() |
| 115 | + sys.exit(app.exec_()) |
0 commit comments