-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpqp.py
193 lines (157 loc) · 6.54 KB
/
pqp.py
1
2
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import psycopg2
import sys
import main_window_design
import child_window_design
from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtWidgets import (QApplication, QWidget, QTableWidgetItem,
QMainWindow, QMessageBox, QAbstractItemView)
connection_data = "host='localhost' \n\
dbname='lab_db' user='postgres' password='123'"
class child_window(QWidget, child_window_design.Ui_Dialog):
def create_window(self):
super().__init__()
self.setupUi(self)
self.setWindowModality(Qt.ApplicationModal)
self.show()
self.buttonBox.rejected.connect(self.quit)
self.buttonBox.accepted.connect(self.insert_record)
def insert_stud(self):
pass
@pyqtSlot()
def quit(self):
self.close()
def insert_record(self):
name = self.tableWidget.item(0, 0)
name = str(name.text())
second_name = self.tableWidget.item(0, 1)
second_name = str(second_name.text())
middle_name = self.tableWidget.item(0, 2)
middle_name = str(middle_name.text())
group_id = self.tableWidget.item(0, 3)
group_id = str(group_id.text())
address = self.tableWidget.item(0, 4)
address = str(address.text())
course = self.tableWidget.item(0, 5)
course = str(course.text())
try:
con = psycopg2.connect(connection_data)
cur = con.cursor()
cur.execute("INSERT INTO student(first_name, second_name, \n\
middle_name, id_group, address, cource) \n\
VALUES (%s, %s, %s, %s, %s, %s);",
(name, second_name, middle_name,
group_id, address, course,))
con.commit()
QMessageBox.information(self, 'Sucses', "Record sucsesfuly insert",
QMessageBox.Ok, QMessageBox.Ok)
self.quit()
except psycopg2.DatabaseError as er:
err = er.diag.message_primary
QMessageBox.information(self, 'Error', err, QMessageBox.Ok,
QMessageBox.NoButton)
finally:
if con:
con.rollback()
con.close()
class main_window(QMainWindow, main_window_design.Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.btn_add.clicked.connect(self.on_btn_add_click)
self.btn_remove.clicked.connect(self.on_btn_remove_click)
self.table1.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.read_table()
self.show()
def read_table(self, table_name='student'):
try:
con = psycopg2.connect(connection_data)
cur = con.cursor()
# set table size
# cur.execute("SELECT count(1) from %s", (table_name, ))
cur.execute("SELECT count(1) from " + table_name)
amountRows = cur.fetchone()
# cur.execute("select count(1) from (select column_name from\n\
# information_schema.columns where table_schema = 'public' \n\
# and table_name = '%s') as numcol;", (table_name,))
cur.execute("select count(1) from (select column_name from\n\
information_schema.columns where table_schema = 'public' \n\
and table_name = '" + table_name + "') as numcol;")
amountColums = cur.fetchone()
amountRows = int(amountRows[0])
amountColums = int(amountColums[0])
self.table1.setRowCount(amountRows)
self.table1.setColumnCount(amountColums)
hor_label = ["ID", "Name", "Second Name", "Middle Name",
"Group ID", "Address", "Course"]
self.table1.setHorizontalHeaderLabels(hor_label)
cur.execute("SELECT * FROM " + table_name)
i = 0
for i in range(amountRows):
row = cur.fetchone()
if row is None:
break
for j in range(amountColums):
self.table1.setItem(i, j, QTableWidgetItem(str(row[j])))
except psycopg2.DatabaseError as er:
err = er.diag.message_primary
QMessageBox.information(self, 'Error', err, QMessageBox.Ok,
QMessageBox.NoButton)
if con:
con.rollback()
finally:
if con:
con.close()
def create_child_window(self):
self.child_window = child_window()
self.child_window.create_window()
self.child_window.show()
self.read_table()
@pyqtSlot()
def on_menuExit_click(self):
answer = QMessageBox.question(self, 'Exit', "You are shure?",
QMessageBox.Yes | QMessageBox.No,
QMessageBox.Yes)
if answer == QMessageBox.Yes:
pass
def on_btn_add_click(self):
self.create_child_window()
self.read_table()
def on_btn_remove_click(self):
removedItemName = "current item"
try:
con = psycopg2.connect(connection_data)
cur = con.cursor()
idToDelete = self.table1.item(self.table1.currentRow(), 0)
tmpStr = str(idToDelete.text())
cur.execute("SELECT * FROM student where id = %s;", (tmpStr,))
row = cur.fetchone()
removedItemName = str(row[1]) + " " + str(row[2])
con.commit()
except psycopg2.DatabaseError:
if con:
con.rollback()
con.close()
answer = QMessageBox.question(self, 'Remove', "Remove " +
removedItemName + "?",
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No)
if answer == QMessageBox.Yes:
try:
cur.execute("delete from student where id = %s", (tmpStr,))
con.commit()
except psycopg2.Error as er: # todo
err = er.diag.message_primary
QMessageBox.information(self, 'Error', err, QMessageBox.Ok,
QMessageBox.NoButton)
if con:
con.rollback()
finally:
if con:
con.close()
self.read_table()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = main_window()
sys.exit(app.exec_())