From f9cde5a1482f2546ab397948bef150b71a81e3b8 Mon Sep 17 00:00:00 2001 From: FatihMalakci Date: Mon, 1 Jul 2019 17:20:56 +0300 Subject: [PATCH] database view added --- .idea/workspace.xml | 110 +++++++++++++++++++++++++++++++++----------- database.py | 43 +++++++++++++---- interface.py | 75 ++++++++++++++++++++++++------ interface.ui | 106 +++++++++++++++++++++++++++++++++++++----- users.db | Bin 0 -> 12288 bytes 5 files changed, 271 insertions(+), 63 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d2cc22c..1a30885 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,13 @@ - + + + + + + + - + - - + + + + + + - + - - + + + + + @@ -105,6 +120,12 @@ + + + pencere + load + + - @@ -158,8 +179,8 @@ - - + + - + @@ -238,18 +259,40 @@ - + - + - + @@ -292,19 +335,30 @@ + + + - + - - + + + + + - + - - + + + + + + diff --git a/database.py b/database.py index 228925c..79295fa 100644 --- a/database.py +++ b/database.py @@ -1,18 +1,41 @@ import sqlite3 -conn = sqlite3.connect('users.db') -c = conn.cursor() +def connectdatabase(database_name): + conn = sqlite3.connect(f'{database_name}') + print(f"{database_name} database connected.") + c = conn.cursor() + return c #Creating our table. -c.execute('''CREATE TABLE users - (name,sirname,age,phone number)''') +def createtable(table): + conn = sqlite3.connect('users.db') + c = conn.cursor() + c.execute(f'''CREATE TABLE {table} + (name,sirname,age)''') -# Inserting an example user. -c.execute("INSERT INTO users VALUES('Fatih','Malakçı','24','5419768923')") +def insertuser(name,sirname,age): + conn = sqlite3.connect('users.db') + c = conn.cursor() + c.execute("INSERT INTO users VALUES(?,?,?)",(name,sirname,age)) + print(f'User created Name : {name} Sirname : {sirname} Age : {age}') + conn.commit() + conn.close() -#Saving -conn.commit() +def deleteuser(name,sirname): + conn = sqlite3.connect('users.db') + c = conn.cursor() + c.execute(f"DELETE FROM users WHERE name='{name}' AND sirname= '{sirname}'") + print(f"User {name} {sirname} Deleted.") + conn.commit() + conn.close() -#Closing -conn.close() \ No newline at end of file + +def databasename(): + print('Now using the Users.db database') + + +if __name__ == '__main__': + gir = input('Enter a database name :') + # createtable(gir) + connectdatabase(gir) diff --git a/interface.py b/interface.py index 4dc762a..ca9b840 100644 --- a/interface.py +++ b/interface.py @@ -7,21 +7,41 @@ # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets +import database +from database import connectdatabase +import sqlite3 class Ui_MainWindow(object): + + def loaddata(self): + # connection = sqlite3.connect('users.db') + query = "SELECT * FROM users" + # connection.execute(query) + result = connectdatabase('users.db').execute(query) + self.pencere.setRowCount(0) + for row_number, row_data in enumerate(result): + self.pencere.insertRow(row_number) + for column_number, data in enumerate(row_data): + self.pencere.setItem(row_number,column_number,QtWidgets.QTableWidgetItem(str(data))) + + connectdatabase('users.db').close() + print("Database closed") + def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(844, 624) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") - self.adduser = QtWidgets.QPushButton(self.centralwidget) - self.adduser.setGeometry(QtCore.QRect(550, 320, 111, 41)) + self.adduserbtn = QtWidgets.QPushButton(self.centralwidget) + self.adduserbtn.setGeometry(QtCore.QRect(550, 370, 111, 41)) font = QtGui.QFont() font.setPointSize(12) - self.adduser.setFont(font) - self.adduser.setObjectName("adduser") - self.pencere = QtWidgets.QListView(self.centralwidget) - self.pencere.setGeometry(QtCore.QRect(10, 100, 441, 371)) + self.adduserbtn.setFont(font) + self.adduserbtn.setObjectName("adduserbtn") + self.pencere = QtWidgets.QTableWidget(self.centralwidget) + self.pencere.setGeometry(QtCore.QRect(10, 100, 441, 361)) + self.pencere.setRowCount(11) + self.pencere.setColumnCount(4) self.pencere.setObjectName("pencere") self.users = QtWidgets.QLabel(self.centralwidget) self.users.setGeometry(QtCore.QRect(170, 40, 161, 41)) @@ -33,7 +53,7 @@ def setupUi(self, MainWindow): self.users.setFont(font) self.users.setObjectName("users") self.namebox = QtWidgets.QLineEdit(self.centralwidget) - self.namebox.setGeometry(QtCore.QRect(480, 130, 101, 31)) + self.namebox.setGeometry(QtCore.QRect(480, 180, 101, 31)) self.namebox.setObjectName("namebox") self.usercount = QtWidgets.QLCDNumber(self.centralwidget) self.usercount.setGeometry(QtCore.QRect(230, 490, 91, 41)) @@ -48,7 +68,7 @@ def setupUi(self, MainWindow): self.label_2.setFont(font) self.label_2.setObjectName("label_2") self.label = QtWidgets.QLabel(self.centralwidget) - self.label.setGeometry(QtCore.QRect(500, 550, 341, 31)) + self.label.setGeometry(QtCore.QRect(500, 530, 341, 31)) font = QtGui.QFont() font.setPointSize(9) font.setBold(True) @@ -72,7 +92,7 @@ def setupUi(self, MainWindow): self.label_4.setFont(font) self.label_4.setObjectName("label_4") self.label_5 = QtWidgets.QLabel(self.centralwidget) - self.label_5.setGeometry(QtCore.QRect(480, 90, 221, 21)) + self.label_5.setGeometry(QtCore.QRect(480, 140, 221, 21)) font = QtGui.QFont() font.setPointSize(11) font.setBold(True) @@ -80,10 +100,10 @@ def setupUi(self, MainWindow): self.label_5.setFont(font) self.label_5.setObjectName("label_5") self.sirnamebox = QtWidgets.QLineEdit(self.centralwidget) - self.sirnamebox.setGeometry(QtCore.QRect(600, 130, 101, 31)) + self.sirnamebox.setGeometry(QtCore.QRect(610, 180, 101, 31)) self.sirnamebox.setObjectName("sirnamebox") self.label_6 = QtWidgets.QLabel(self.centralwidget) - self.label_6.setGeometry(QtCore.QRect(480, 210, 171, 21)) + self.label_6.setGeometry(QtCore.QRect(480, 250, 131, 21)) font = QtGui.QFont() font.setPointSize(11) font.setBold(True) @@ -91,8 +111,34 @@ def setupUi(self, MainWindow): self.label_6.setFont(font) self.label_6.setObjectName("label_6") self.agebox = QtWidgets.QLineEdit(self.centralwidget) - self.agebox.setGeometry(QtCore.QRect(480, 250, 101, 31)) + self.agebox.setGeometry(QtCore.QRect(480, 300, 101, 31)) self.agebox.setObjectName("agebox") + self.label_7 = QtWidgets.QLabel(self.centralwidget) + self.label_7.setGeometry(QtCore.QRect(630, 250, 151, 21)) + font = QtGui.QFont() + font.setPointSize(11) + font.setBold(True) + font.setWeight(75) + self.label_7.setFont(font) + self.label_7.setObjectName("label_7") + self.phonebox = QtWidgets.QLineEdit(self.centralwidget) + self.phonebox.setGeometry(QtCore.QRect(630, 300, 101, 31)) + self.phonebox.setObjectName("phonebox") + self.loadusersbtn = QtWidgets.QPushButton(self.centralwidget) + self.loadusersbtn.setGeometry(QtCore.QRect(660, 60, 91, 31)) + self.loadusersbtn.setObjectName("loadusersbtn") + + self.loadusersbtn.clicked.connect(self.loaddata) + + self.Databasenamelabel = QtWidgets.QLabel(self.centralwidget) + self.Databasenamelabel.setGeometry(QtCore.QRect(690, 560, 141, 21)) + font = QtGui.QFont() + font.setPointSize(10) + font.setBold(True) + font.setWeight(75) + self.Databasenamelabel.setFont(font) + self.Databasenamelabel.setText("") + self.Databasenamelabel.setObjectName("Databasenamelabel") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 844, 21)) @@ -113,7 +159,7 @@ def setupUi(self, MainWindow): def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) - self.adduser.setText(_translate("MainWindow", "Add User")) + self.adduserbtn.setText(_translate("MainWindow", "Add User")) self.users.setText(_translate("MainWindow", "User List")) self.label_2.setText(_translate("MainWindow", "Number of Users")) self.label.setText(_translate("MainWindow", "The User Information is Provided by SQLite3 Database")) @@ -121,10 +167,11 @@ def retranslateUi(self, MainWindow): self.label_4.setText(_translate("MainWindow", "github.com/fatihmalakci")) self.label_5.setText(_translate("MainWindow", "Enter User\'s Name/Sirname:")) self.label_6.setText(_translate("MainWindow", "Enter User\'s Age:")) + self.label_7.setText(_translate("MainWindow", "Enter User\'s Phone:")) + self.loadusersbtn.setText(_translate("MainWindow", "Load Users")) self.menuUsers.setTitle(_translate("MainWindow", "Main Window")) self.actionMain_Window.setText(_translate("MainWindow", "Main Window")) - if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) diff --git a/interface.ui b/interface.ui index 23ce839..24c8e1c 100644 --- a/interface.ui +++ b/interface.ui @@ -14,11 +14,11 @@ MainWindow - + 550 - 320 + 370 111 41 @@ -32,15 +32,36 @@ Add User - + 10 100 441 - 371 + 361 + + 11 + + + 4 + + + + + + + + + + + + + + + + @@ -67,7 +88,7 @@ 480 - 130 + 180 101 31 @@ -108,7 +129,7 @@ 500 - 550 + 530 341 31 @@ -168,7 +189,7 @@ 480 - 90 + 140 221 21 @@ -187,8 +208,8 @@ - 600 - 130 + 610 + 180 101 31 @@ -198,8 +219,8 @@ 480 - 210 - 171 + 250 + 131 21 @@ -218,12 +239,75 @@ 480 + 300 + 101 + 31 + + + + + + + 630 250 + 151 + 21 + + + + + 11 + 75 + true + + + + Enter User's Phone: + + + + + + 630 + 300 101 31 + + + + 660 + 60 + 91 + 31 + + + + Load Users + + + + + + 690 + 560 + 141 + 21 + + + + + 10 + 75 + true + + + + + + diff --git a/users.db b/users.db index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0e19f3a49d913ddc3781ce3c73b6ad04a232dd39 100644 GIT binary patch literal 12288 zcmeI#!Aiq09LMowRbhg}g3yDP94Cl~dJ#N0r697ybbEowq7+-jw1U2YkK<`i;5&I; z7CKQV^x|QRZ%XoqU(-M7=a$jUAgipte|Ckb^s(3$Qi^k}g%GiyQ9Wf{*L+=!dDb>! zad3FL<#$On6>6&TwhwrZ00IagfB*srAb$iF~QdU2flvGml}3E^pKR zIMw6+#URxo+SI>C@5vN)zsy{y=E3e=$&KWv(`#v68J5nUP9bXRG{2ksOTSRx>SOV& zfHS{<00IagfB*srAb