| 12
 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
 
 | from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from Design import Ui_MainWindow
import os
import sqlite3
import pandas as pd
import sys
 
 
def resource_path(relative_path):
    try:           
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)
 
db_path = resource_path("test.db")
 
 
class MyWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
 
        self.btn_insert.clicked.connect(self.ajouter)
        self.btn_show.clicked.connect(self.afficher)
        self.btn_delete.clicked.connect(self.supprimer)
    def ajouter(self):
            try:
                connection = sqlite3.connect(db_path)
                cursor = connection.cursor()
                a=self.txb_name.text()
                b=self.txb_pass.text()
                cursor.execute("insert into user (name, password) values (?, ?)",(a, b))
                connection.commit()
                self.txb_info.setText("Record inserted successfully into SqliteDb_developers table ")
                cursor.close()
            except sqlite3.Error as error:
                print("Error while executing sqlite script", error)
 
    def afficher(self):
        connection = sqlite3.connect(db_path)
        self.table.clearContents()
        df = pd.read_sql_query('SELECT * FROM user ',connection)
        print(df)
        for i in range(len(df.index)):
            for j in range(len(df.columns)):
                item = QTableWidgetItem()
                text = "{}".format(str(df.iat[i, j]))
                item.setText(text)
                item.setTextAlignment(Qt.AlignCenter)
                self.table.setItem(i, j, item)
    def supprimer(self):
        connection = sqlite3.connect(db_path)
        cursor = connection.cursor()
        a = self.txb_name.text()
        cursor.execute("""DELETE from user where name = ?""", (a,))
        connection.commit()
        self.txb_info.setText("Record deleted successfully ")
        cursor.close()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_()) | 
Partager