LEE_BOMB 2021. 11. 12. 19:34
db ์—ฐ๊ฒฐ ํ…Œ์ŠคํŠธ 

 

import pymysql # driver = p/g + DB



DB ์—ฐ๊ฒฐ ํ™˜๊ฒฝ 

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


try :
    #db ์—ฐ๋™ ๊ฐ์ฒด ์ƒ์„ฑ 
    conn = pymysql.connect(**config)    
    #sql๋ฌธ ์‹คํ–‰ ๊ฐ์ฒด ์ƒ์„ฑ 
    cursor = conn.cursor()
    sql = "select * from goods"
    
    cursor.execute(sql) #sql๋ฌธ ์‹คํ–‰    
    dataset = cursor.fetchall() #์กฐํšŒ๋œ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ ๊ฐ€์ ธ์˜ค๊ธฐ
    
    for row in dataset :
        print(row)
    
    
except Exception as e:
    print('์˜ˆ์™ธ๋ฐœ์ƒ : ', e)
finally :
    pass

 

 

 

 

 

ํฌ๋ฃจ๋“œ(CRUD)

Create(insert), Read(select), Update, Delete


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()
    
    #4. ๋ ˆ์ฝ”๋“œ ์‚ญ์ œ : Delete(code -> ๋ ˆ์ฝ”๋“œ ์‚ญ์ œ)
    '''
    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๋Š” ์—†์Šต๋‹ˆ๋‹ค.')
    '''
        
    #3. ๋ ˆ์ฝ”๋“œ ์ˆ˜์ • : Update(code -> su, dan ์ˆ˜์ •)
    '''
    code = int(input('update code input : ')) #4
    su = int(input('update su input : ')) #3
    dan = int(input('update dan input : ')) #550000
    query = f"""update goods 
                set su = {su}, dan = {dan} 
                where code={code}"""
    cursor.execute(query) # ๋ ˆ์ฝ”๋“œ ์ˆ˜์ • 
    conn.commit() #db ๋ฐ˜์˜     
    '''
    
    #2. ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€ : Create
    '''
    code = int(input('code input : ')) #4
    name = input('name input : ') #test
    su = int(input('su input : ')) #4 
    dan = int(input('dan input : ')) #500000
    
    query = f"insert into goods values({code},'{name}',{su},{dan})"
    cursor.execute(query) # ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€ 
    conn.commit()  #db ๋ฐ˜์˜  
    '''
    
    #1. ๋ ˆ์ฝ”๋“œ ์กฐํšŒ : Read
    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))
except Exception as e:
    print('์˜ˆ์™ธ๋ฐœ์ƒ : ', e)
    conn.rollback() #์ด์ „ ์ƒํƒœ ๋ฆฌํ„ด 
finally :
    cursor.close(); conn.close()

 

 

 

 

 

emp table

1. emp table ์ƒ์„ฑ : HidiSQL ์ด์šฉ 
2. python + DB ์—ฐ๋™ 
table ์œ ๋ฌด ํŒ๋‹จ : show tables
- table ์žˆ๋Š” ๊ฒฝ์šฐ : ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€/์กฐํšŒ 
- table ์—†๋Š” ๊ฒฝ์šฐ : 'table ์—†์Œ' ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€ 


import pymysql # driver = python + DB



DB ์—ฐ๊ฒฐ ํ™˜๊ฒฝ 

config = {
    'host' : '127.0.0.1',
    'user' : 'scott',
    'password' : 'tiger',
    'database' : 'work',
    'port' : 3306,
    'charset':'utf8',
    'use_unicode' : True} #dict
    
try :
    #db ์—ฐ๋™ ๊ฐ์ฒด 
    conn = pymysql.connect(**config)
    #query ์‹คํ–‰ ๊ฐ์ฒด 
    cursor = conn.cursor()
    
    #1. table ์œ ๋ฌด ํŒ๋‹จ : emp 
    cursor.execute("show tables") #table ๋ชฉ๋ก ์กฐํšŒ 
    tables = cursor.fetchall()
    
    #sw๊ธฐ๋ฒ• 
    sw = False #off
    for table in tables :
        if 'emp' in table :
            sw = True #on 
        
    if sw : #emp ํ…Œ์ด๋ธ” ์กด์žฌ 
        query = "select * from emp"
        cursor.execute(query) # ๋ ˆ์ฝ”๋“œ ์กฐํšŒ
        dataset = cursor.fetchall() # ์ „์ฒด ๋ ˆ์ฝ”๋“œ ๊ฐ€์ ธ์˜ค๊ธฐ 
        
        if dataset : #๋ ˆ์ฝ”๋“œ ์žˆ์Œ 
            for row in dataset :
                print(row[0], row[1], row[2], row[3], row[4], row[5], row[6])   
                
            print('์ „์ฒด ๋ ˆ์ฝ”๋“œ ๊ฐœ์ˆ˜ : ', len(dataset))
        else : #๋ ˆ์ฝ”๋“œ ์—†์Œ 
            print('์กฐํšŒ๋œ ๋ ˆ์ฝ”๋“œ ์—†์Œ')
            
    else : #emp ํ…Œ์ด๋ธ” ์—†์Œ 
        print('table ์—†์Œ')
        
except Exception as e :
    print('db error :', e)
finally:
    cursor.close(); conn.close()