-> send to SQL editor -> select all statement -> 아래처럼 수정
# 주소록 GUI 프로그램 - MySQL 연동
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import pymysql
class qtApp(QMainWindow):
conn = None
def __init__(self):
super().__init__()
uic.loadUi('./studyPyQt/AddressBook.ui',self)
self.setWindowIcon(QIcon('./studyPyQt/Addressicon.png'))
self.setWindowTitle('주소록')
self.initDB() # DB초기화
def initDB(self):
self.conn = pymysql.connect(host='localhost',user='root',password='815301',
db='miniproject',charset='utf8')
cur = self.conn.cursor()
query = '''SELECT idx
, FullName
, PhoneNum
, Email
, Address
FROM addressbook'''
cur.execute(query)
rows = cur.fetchall()
# print(rows) # 콘솔창에 주소 출력
self.makeTable(rows)
self.conn.close() # 프로그램 종료할 때
def makeTable(self,rows):
self.tblAddress.setColumnCount(5) # 0, 열 갯수
self.tblAddress.setRowCount(len(rows)) # 0, 행 갯수
self.tblAddress.setSelectionMode(QAbstractItemView.SingleSelection) # 1. 단일선택
self.tblAddress.setHorizontalHeaderLabels(['번호','이름','핸드폰','이메일','주소']) # 1. 열제목
self.tblAddress.setColumnWidth(0,0) # 번호(idx)는 숨김
self.tblAddress.setColumnWidth(1,70)
self.tblAddress.setColumnWidth(2,100)
self.tblAddress.setColumnWidth(3,200)
self.tblAddress.setColumnWidth(4,194)
self.tblAddress.setEditTriggers(QAbstractItemView.NoEditTriggers) # 1. 컬럼 수정 금지
for i , row in enumerate(rows):
# row[0]~row[4] 까지
idx = row[0]
FullName = row[1]
PhoneNum = row[2]
Email = row[3]
Address = row[4]
self.tblAddress.setItem(i,0,QTableWidgetItem(str(idx)))
self.tblAddress.setItem(i,1,QTableWidgetItem(FullName))
self.tblAddress.setItem(i,2,QTableWidgetItem(PhoneNum))
self.tblAddress.setItem(i,3,QTableWidgetItem(Email))
self.tblAddress.setItem(i,4,QTableWidgetItem(Address))
self.stbCurrent.showMessage(f'전체 주소록 :{len(rows)}개')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = qtApp() # MyApp(9일차 참고)
ex.show()
sys.exit(app.exec_())
신규버튼 누르면 값을 넣을 수 있는 기능 / 저장 버튼
# 주소록 GUI 프로그램 - MySQL 연동
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import pymysql
class qtApp(QMainWindow):
conn = None
def __init__(self):
super().__init__()
uic.loadUi('./studyPyQt/AddressBook.ui',self)
self.setWindowIcon(QIcon('./studyPyQt/Addressicon.png'))
self.setWindowTitle('주소록')
self.initDB() # DB초기화
# 버튼 시그널/슬롯함수 지정 -- 1
self.btnNew.clicked.connect(self.btnNewClicked)
self.btnSave.clicked.connect(self.btnSaveClicked)
def btnNewClicked(self): # 신규버튼 누르면 -- 1
# 라인 에디트 내용 삭제 후 이름에 포커스 맞춤
self.txtName.setText('')
self.txtPhone.setText('')
self.txtEmail.setText('')
self.txtAddress.setText('')
self.txtName.setFocus()
def btnSaveClicked(self): # 저장
FullName = self.txtName.text()
PhoneNum = self.txtPhone.text()
Email = self.txtEmail.text()
Address = self.txtAddress.text()
print(FullName,PhoneNum,Email,Address)
def initDB(self):
self.conn = pymysql.connect(host='localhost',user='root',password='815301',
db='miniproject',charset='utf8')
cur = self.conn.cursor()
query = '''SELECT idx
, FullName
, PhoneNum
, Email
, Address
FROM addressbook'''
cur.execute(query)
rows = cur.fetchall()
# print(rows) # 콘솔창에 주소 출력
self.makeTable(rows)
self.conn.close() # 프로그램 종료할 때
def makeTable(self,rows):
self.tblAddress.setColumnCount(5) # 0, 열 갯수
self.tblAddress.setRowCount(len(rows)) # 0, 행 갯수
self.tblAddress.setSelectionMode(QAbstractItemView.SingleSelection) # 1. 단일선택
self.tblAddress.setHorizontalHeaderLabels(['번호','이름','핸드폰','이메일','주소']) # 1. 열제목
self.tblAddress.setColumnWidth(0,0) # 번호(idx)는 숨김
self.tblAddress.setColumnWidth(1,70)
self.tblAddress.setColumnWidth(2,100)
self.tblAddress.setColumnWidth(3,200)
self.tblAddress.setColumnWidth(4,194)
self.tblAddress.setEditTriggers(QAbstractItemView.NoEditTriggers) # 1. 컬럼 수정 금지
for i , row in enumerate(rows):
# row[0]~row[4] 까지
idx = row[0]
FullName = row[1]
PhoneNum = row[2]
Email = row[3]
Address = row[4]
self.tblAddress.setItem(i,0,QTableWidgetItem(str(idx)))
self.tblAddress.setItem(i,1,QTableWidgetItem(FullName))
self.tblAddress.setItem(i,2,QTableWidgetItem(PhoneNum))
self.tblAddress.setItem(i,3,QTableWidgetItem(Email))
self.tblAddress.setItem(i,4,QTableWidgetItem(Address))
self.stbCurrent.showMessage(f'전체 주소록 :{len(rows)}개')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = qtApp() # MyApp(9일차 참고)
ex.show()
sys.exit(app.exec_())
추가된 내용