๋ฌธ์ 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()
'๊ฐ์ธ๊ณต๋ถ > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
57. Python Matplot ์ฐ์ต๋ฌธ์ (0) | 2021.11.21 |
---|---|
56. Python Pandas ์ฐ์ต๋ฌธ์ (0) | 2021.11.20 |
50. Python ํด๋์ค, ๋ฐ์ดํฐ ์ ์ถ๋ ฅ ์ฐ์ต๋ฌธ์ (0) | 2021.11.11 |
48. Python ํจ์ ์ฐ์ต๋ฌธ์ (2) (0) | 2021.11.09 |
47. Python ํจ์ ์ฐ์ต๋ฌธ์ (1) (0) | 2021.11.08 |