๋ฌธ์ œ1) goods ํ…Œ์ด๋ธ”์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜์‹œ์˜ค.  
 <์กฐ๊ฑด1> HDTV ์ˆ˜๋Ÿ‰ ์ˆ˜์ • 
 <์กฐ๊ฑด2> ์ „์ž๋ ˆ์ธ์ง€ ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€ 
 
[ goods ํ…Œ์ด๋ธ” ํ˜„ํ™ฉ ]
1 ๋ƒ‰์žฅ๊ณ  2 850000
2 ์„ธํƒ๊ธฐ 3 550000
3 HDTV 3 1500000  <- ์ˆ˜๋Ÿ‰ ์ˆ˜์ •
4 ์ „์ž๋ ˆ์ธ์ง€ 5  400000 <- ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€
์ „์ฒด ๋ ˆ์ฝ”๋“œ ์ˆ˜ : 4

[ ์ƒํ’ˆ๋ณ„ ์ด๊ธˆ์•ก ]
๋ƒ‰์žฅ๊ณ  ์ƒํ’ˆ์˜ ์ด๊ธˆ์•ก์€ 1,700,000
์„ธํƒ๊ธฐ ์ƒํ’ˆ์˜ ์ด๊ธˆ์•ก์€ 1,650,000
HDTV ์ƒํ’ˆ์˜ ์ด๊ธˆ์•ก์€ 4,500,000
์ „์ž๋ ˆ์ธ์ง€ ์ƒํ’ˆ์˜ ์ด๊ธˆ์•ก์€ 2,000,000

 

import pymysql

config = {
    'host' : '127.0.0.1',
    'user' : 'scott',
    'password' : 'tiger',
    'database' : 'work',
    'port' : 3306,
    'charset':'utf8',
    'use_unicode' : True}

try :
    conn = pymysql.connect(**config)
    cursor = conn.cursor()
    '''
    #1. update
    query ="update goods set su = 3 where name = 'HDTV'"
    cursor.execute(query) # ๋ ˆ์ฝ”๋“œ ์ˆ˜์ • 
    conn.commit() 
    
    #2. insert 
    query ="insert into goods values(4,'์ „์ž๋ ˆ์ธ์ง€',5,400000)"
    cursor.execute(query) # ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€ 
    conn.commit() 
    '''
    #3. ์ด๊ธˆ์•ก ๊ณ„์‚ฐ 
    cursor.execute("select * from goods")
    dataset = cursor.fetchall()
    print("\t[ goods ํ…Œ์ด๋ธ” ํ˜„ํ™ฉ ]")
    for row in dataset :
        print(row[0], row[1], row[2], row[3])
    print('์ „์ฒด ๋ ˆ์ฝ”๋“œ ์ˆ˜ : ', len(dataset))
    
    print("\t[ ์ƒํ’ˆ๋ณ„ ์ด๊ธˆ์•ก ]")
    for row in dataset :
        tot = row[2] * row[3]
        print("{0} ์ƒํ’ˆ์˜ ์ด๊ธˆ์•ก์€ {1:3,d}".format(row[1],tot))    
    
except Exception as e :
    print('db error :', e)
finally:
    cursor.close(); conn.close()

 

 

 

 

๋ฌธ์ œ2) ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฉ”๋‰ด๋ฅผ ์ด์šฉํ•˜์—ฌ goods ํ…Œ์ด๋ธ”์„ ๊ด€๋ฆฌํ•˜์‹œ์˜ค.

[๋ ˆ์ฝ”๋“œ ์ฒ˜๋ฆฌ ๋ฉ”๋‰ด]
1. ๋ ˆ์ฝ”๋“œ ์กฐํšŒ
2. ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€
3. ๋ ˆ์ฝ”๋“œ ์ˆ˜์ •
4. ๋ ˆ์ฝ”๋“œ ์‚ญ์ œ
5. ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
    ๋ฉ”๋‰ด๋ฒˆํ˜ธ ์ž…๋ ฅ : 

 

import pymysql

config = {
    'host' : '127.0.0.1',
    'user' : 'scott',
    'password' : 'tiger',
    'database' : 'work',
    'port' : 3306,
    'charset':'utf8',
    'use_unicode' : True}

try:
    #db ์—ฐ๊ฒฐ ๊ฐ์ฒด ์ƒ์„ฑ 
    conn = pymysql.connect(**config)
    #SQL ์‹คํ–‰ ๊ฐ์ฒด ์ƒ์„ฑ 
    cursor = conn.cursor()    
    
    while True :  # ๋ฌดํ•œ๋ฃจํ”„ 
        print('\t[๋ ˆ์ฝ”๋“œ ์ฒ˜๋ฆฌ ๋ฉ”๋‰ด ]')
        print('1. ๋ ˆ์ฝ”๋“œ ์กฐํšŒ')
        print('2. ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€')
        print('3. ๋ ˆ์ฝ”๋“œ ์ˆ˜์ •')
        print('4. ๋ ˆ์ฝ”๋“œ ์‚ญ์ œ')
        print('5. ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ')    
        menu = int(input('\t๋ฉ”๋‰ด๋ฒˆํ˜ธ ์ž…๋ ฅ : '))
        
        if menu == 1 : # 1. ๋ ˆ์ฝ”๋“œ ์กฐํšŒ
            sel = int(input('1.์ „์ฒด, 2.์ƒํ’ˆ๋ช… : '))
            if sel == 1 : 
                query = "select * from goods"
                cursor.execute(query) # sql๋ฌธ ์‹คํ–‰ 
                dataset = cursor.fetchall() # ์ „์ฒด ๋ ˆ์ฝ”๋“œ ๊ฐ€์ ธ์˜ค๊ธฐ 
                print('-'*30)
                for row in dataset :
                    print(row[0], row[1], row[2], row[3])
                print('-'*30)
                print('์ „์ฒด ๋ ˆ์ฝ”๋“œ ์ˆ˜ : ', len(dataset))
            elif sel == 2: 
                name = input('์ƒํ’ˆ๋ช… ์ž…๋ ฅ : ')
                query =f"select * from goods where name like '%{name}%'"
                cursor.execute(query)
                dataset = cursor.fetchall()
                
                if dataset :
                    for row in dataset :
                        print(row)
                else :
                    print('๊ฒ€์ƒ‰๋œ ์ƒํ’ˆ์€ ์—†์Šต๋‹ˆ๋‹ค.')                
        elif menu == 2: # 2. ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€
            code = int(input('code input : ')) 
            name = input('name input : ') 
            su = int(input('su input : ')) 
            dan = int(input('dan input : ')) 
            
            query = f"insert into goods values({code},'{name}',{su},{dan})"
            cursor.execute(query) #๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€ 
            conn.commit()  # db ๋ฐ˜์˜
        elif menu == 3: #3. ๋ ˆ์ฝ”๋“œ ์ˆ˜์ •
            code = int(input('update code input : ')) 
            su = int(input('update su input : ')) 
            dan = int(input('update dan input : ')) 
            query = f"""update goods 
                        set su = {su}, dan = {dan} 
                        where code={code}"""
            cursor.execute(query) # ๋ ˆ์ฝ”๋“œ ์ˆ˜์ • 
            conn.commit() #db ๋ฐ˜์˜
        elif menu == 4: #4. ๋ ˆ์ฝ”๋“œ ์‚ญ์ œ
            code = int(input('delete code input : '))
            query = f"select * from goods where code = {code}"
            cursor.execute(query) 
            row = cursor.fetchone() #1๊ฐœ ๋ ˆ์ฝ”๋“œ ๊ฐ€์ ธ์˜ค๊ธฐ 
            
            if row : #code ์žˆ๋Š” ๊ฒฝ์šฐ 
                cursor.execute(f"delete from goods where code = {code}") #์‚ญ์ œ 
                conn.commit()  # db ๋ฐ˜์˜
            else :
                print('ํ•ด๋‹น code๋Š” ์—†์Šต๋‹ˆ๋‹ค.')
        elif menu == 5 :
            print('ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ')
            break #๋ฐ˜๋ณต exit
        else :
            print('ํ•ด๋‹น ๋ฉ”๋‰ด๋Š” ์—†์Šต๋‹ˆ๋‹ค.')
        
#DB ์—ฐ๊ฒฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ          
except Exception as e :
    print('db ์—ฐ๋™ ์˜ค๋ฅ˜ : ', e)
    conn.rollback()
finally:
    cursor.close()
    conn.close()

 

 

 

 

 

๋ฌธ์ œ3) HidiSQL์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ student ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๊ณ , 
      DB ์—ฐ๋™ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ๋ ˆ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์กฐํšŒํ•˜์‹œ์˜ค.
  
์กฐ๊ฑด1> HidiSQL ์ด์šฉ : student ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ       
       ํ…Œ์ด๋ธ” ์นผ๋Ÿผ ๊ตฌ์„ฑ : ํ•™๋ฒˆ,์ด๋ฆ„,์ „ํ™”๋ฒˆํ˜ธ,ํ•™๊ณผ,์ง€๋„๊ต์ˆ˜ 
create or replace table student( 
  studno int primary key,              
  sname varchar(10) not null,                        
  tel varchar(15),     
  deptno int,                
  profno  int                
);  


์กฐ๊ฑด2> DB ์—ฐ๋™ ํ”„๋กœ๊ทธ๋žจ : ํ…Œ์ด๋ธ”์— ๋ ˆ์ฝ”๋“œ๊ฐ€ ์—†์œผ๋ฉด ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€(insert)  
insert into student values (9411,'์„œ์ง„์ˆ˜','055)381-2158',201,1001);
insert into student values (9413,'์ด๋ฏธ๊ฒฝ','02)266-8947',103,3002);
insert into student values (9415,'๋ฐ•๋™ํ˜ธ','031)740-6388',202,4003);

์กฐ๊ฑด3> DB ์—ฐ๋™ ํ”„๋กœ๊ทธ๋žจ : ํ…Œ์ด๋ธ”์— ๋ ˆ์ฝ”๋“œ๊ฐ€ ์žˆ์œผ๋ฉด ๋ ˆ์ฝ”๋“œ ์กฐํšŒ(select)   

 

import pandas as pd 
import pymysql # driver = python + DB(Mysql)
import os 
os.chdir('D:/ITWILL/3_Python-I/workspace/chap10_MariaDB/data')

st = pd.read_csv('student.csv')

config = {
    'host' : '127.0.0.1',
    'user' : 'scott',
    'password' : 'tiger',
    'database' : 'work',
    'port' : 3306,
    'charset':'utf8',
    'use_unicode' : True}

try :
    #db ์—ฐ๋™ ๊ฐ์ฒด ์ƒ์„ฑ 
    conn = pymysql.connect(**config)
    #sql๋ฌธ ์‹คํ–‰ ๊ฐ์ฒด 
    cursor = conn.cursor()    
    
    #1. table ์œ ๋ฌด ํŒ๋‹จ : student
    cursor.execute("show tables") #table ๋ชฉ๋ก ์กฐํšŒ 
    tables = cursor.fetchall() # table ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ 
    
    sw = False # off
    for table in tables : 
        if 'student' in table :
            sw = True # on
    
    #2. ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€ or ๋ ˆ์ฝ”๋“œ ์กฐํšŒ 
    if sw : # table ์žˆ๋Š” ๊ฒฝ์šฐ 
        #๋ ˆ์ฝ”๋“œ ์กฐํšŒ 
        sql = "select * from student"
        cursor.execute(sql)
        dataset = cursor.fetchall()
        
        if not(dataset) : #1) ๋ ˆ์ฝ”๋“œ ์—†๋Š” ๊ฒฝ์šฐ 
            #๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€      
            sql = """insert into student values (9411,'์„œ์ง„์ˆ˜','055)381-2158',201,1001)"""                    
            cursor.execute(sql)        
            sql = """insert into student values (9413,'์ด๋ฏธ๊ฒฝ','02)266-8947',103,3002)"""                    
            cursor.execute(sql)
            sql = """insert into student values (9415,'๋ฐ•๋™ํ˜ธ','031)740-6388',202,4003)"""                    
            cursor.execute(sql)
            conn.commit() # db ๋ฐ˜์˜ 
            
            print('student ํ…Œ์ด๋ธ” ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€\n')
        
            #๋ ˆ์ฝ”๋“œ ์กฐํšŒ 
            sql = "select * from student"
            cursor.execute(sql)
            dataset = cursor.fetchall()
            
            for row in dataset : 
                print(row[0], row[1], row[2], row[3], row[4])
                
        else : #2) ๋ ˆ์ฝ”๋“œ ์žˆ๋Š” ๊ฒฝ์šฐ    
            for row in dataset : 
                print(row[0], row[1], row[2], row[3], row[4], sep=',')

        
    else : #table ์—†๋Š” ๊ฒฝ์šฐ         
        print('student ํ…Œ์ด๋ธ”์ด ์—†์Šต๋‹ˆ๋‹ค.')        
            
except Exception as e :
    print('db error : ', e)
finally :
    cursor.close(); conn.close()

+ Recent posts