컴퓨터/mysql

pyqt5 18데이터베이스-테이블생성

풍경소리^^ 2020. 2. 16. 19:54
import sys, pymysql
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton

class sql(QWidget):
    def __init__(self):
        super().__init__()                  # 상위객체생성
                self.sqlConnect()
        self.initUI()                       # 클래스 안의 메소드 호출
                self.run()

    def sqlConnect(self):
        try:
            self.conn = pymysql.connect(
                host="192.168.0.6",
                user="유저아이디",
                password="패스워드",
                db="디비아이디",
                port=3306,
                charset="utf8"
            )
        except:
            print("문제가 있네요!")
            exit(1)
        print("연결 성공!")
        self.cur = self.conn.cursor()

    def initUI(self):
        self.setGeometry(300,300,500,520)
        self.setWindowTitle("데이터 베이스 활용 예제")
        self.show()


def run(self):
    # self.cmd = "show tables"
    # self.cmd = "create table test(no int, name char)"
    # self.cmd = "create table test2(no int, name varchar(20))"
    # self.cmd = "drop table test2"
    # self.cmd = "create table test2(no int, name varchar(20), addr varchar(20))"
    # self.cmd = "show tables"
    self.cmd = "desc test2"  # 테이블의 자료형 확인 (desc 테이블명)
    self.cur.execute(self.cmd)
    self.conn.commit()
    print(self.cur.fetchall())
    # pass



    def closeEvent(self, QCloseEvent):
        print("close!")
        self.conn.close()

app = QApplication(sys.argv)                # Application 오브젝트 생성-쉘스크립트에서 실행할 때 명령줄로 인수를 받을 수 있는데
w = sql()                                   # 객체생성
sys.exit(app.exec_())                       # 프로그램 종료-이벤트처리를 위한 메인루프 실행-창을 닫을 때

 

import sys, pymysql
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton

class sql(QWidget):
def __init__(self):
super().__init__() # 상위객체생성
self.sqlConnect()
self.initUI() # 클래스 안의 메소드 호출
self.run()

def sqlConnect(self):
try:
self.conn = pymysql.connect(
host="192.168.0.6",
user="유저아이디",
password="패스워드",
db="디비아이디",
port=3306,
charset="utf8"
)
except:
print("문제가 있네요!")
exit(1)
print("연결 성공!")
self.cur = self.conn.cursor()

def initUI(self):
self.setGeometry(300,300,500,520)
self.setWindowTitle("데이터 베이스 활용 예제")
self.show()

def run(self):
# self.cmd = "show tables"
# self.cmd = "create table test(no int, name char)"
# self.cmd = "create table test2(no int, name varchar(20))"
# self.cmd = "drop table test2"
# self.cmd = "create table test2(no int, name varchar(20), addr varchar(20))"
# self.cmd = "show tables"
self.cmd = "desc test2" # 테이블의 자료형 확인 (desc 테이블명)
self.cur.execute(self.cmd)
self.conn.commit()
print(self.cur.fetchall())
# pass



def closeEvent(self, QCloseEvent):
print("close!")
self.conn.close()

app = QApplication(sys.argv) # Application 오브젝트 생성-쉘스크립트에서 실행할 때 명령줄로 인수를 받을 수 있는데
w = sql() # 객체생성
sys.exit(app.exec_()) # 프로그램 종료-이벤트처리를 위한 메인루프 실행-창을 닫을 때