Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #_*_ coding: utf-8 _*_
- import sys,os
- #Check Python Version
- try:
- assert sys.version_info >= (3,0)
- except AssertionError:
- print(sys.version_info)
- print('App>이프로그램은 Python3이상 또는 동급버전에서 실행하야합니다.')
- sys.exit(1)
- #Import PyMySQL. If not install PyPySQL, installed automatically
- try:
- import pymysql
- except ImportError:
- print("App>프로그램구동에 필요한 PyMySQL 모듈을 찾을수 없습니다.")
- print("App>PyMySQL모듈을 자동으로 설치합니다.")
- os.system("pip3 install pymysql")
- try:
- import pymysql
- print("App>MyMySQL묘듈 설치완료")
- except ImportError:
- print("App>MyMySQL묘듈 설치실패")
- sys.exit(1)
- #Global Variable
- serverip = None
- username = None
- password = None
- database = None
- conn = None
- cur = None
- #MariaDB Connect
- def connect():
- global serverip, username, password, conn, cur
- serverip = input("ServerIP>") #Enter MariaDB Server IP Address
- username = input("username>") #Enter MariaDB Username
- password = input("password>") #Enter MariaDB Password
- try:
- conn = pymysql.connect(host=serverip,user=username, password=password, db='pytest', charset = 'utf8mb4',autocommit=True)
- cur = conn.cursor()
- except pymysql.Error as err: #If MySQL Connect Error
- errno,errinfo = err.args
- if errno==1049: #If Just no DB, Why not connected.
- print("App>DB에는 접속되었지만 pytest라는 데이터베이스가 없습니다.")
- print("App>데이터베이스를 자동으로 생성합니다.")
- try:
- conn = pymysql.connect(host=serverip,user=username, password=password,charset = 'utf8mb4',autocommit=True)
- cur = conn.cursor()
- cur.execute("create database pytest");
- cur.execute("use pytest")
- print("App>테이블 pytable을 생성하고 있습니다.")
- cur.execute("create table pytable(id int(5) not null auto_increment,name varchar(30) not null, info varchar(100) not null, primary key (id));")
- cur.execute("alter table pytable convert to character set utf8mb4")
- print("App>데이터베이스 생성을 성공했습니다.")
- except pymysql.Error as err:
- print(err)
- print("App>데이터베이스 생성을 실패했습니다.")
- sys.exit(1)
- print("App>데이터베이스 생성이 완료되었습니다.")
- else:
- print(err)
- print("App>DB서버연결에 실패했습니다.")
- sys.exit(1)
- def input_value(): # Input Value
- global cur
- _id = "" #input("idvl>")
- name = input("name>")
- info = input("info>")
- query = "insert into pytable (id,name,info) value('NULL','" +name+ "','" +info+ "');"
- cur.execute(query)
- def print_row(): # Print Tables
- global cur
- cur.execute("select * from pytable");
- row = cur.fetchall()
- print(row)
- connect()
- print("App>",serverip ,"에 연결 성공")
- select = None
- while True:
- print("\n\nMenu>1)Input 2>Print 3>Exit")
- select = input("select>")
- if select == '' : print("App>Invalid Value")
- elif select == '1':
- input_value()
- elif select == '2':
- print_row()
- elif select == '3':
- break
- else : print("App>Invalid Value")
- conn.close()
- cur.close()
- print("App>byebye")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement