์ƒˆ์†Œ์‹

๐Ÿ ํŒŒ์ด์ฌ (Python)

sqlite) ์˜ˆ์ œ - ์ƒํ’ˆ๊ณผ ์ฃผ๋ฌธ ํ…Œ์ด๋ธ” CRUD

  • -

* ๋ฌธ์ œ ์กฐ๊ฑด.

1. ์ƒํ’ˆ ํ…Œ์ด๋ธ”, ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑ

2. ์ƒํ’ˆ ํ…Œ์ด๋ธ”์— ์ƒํ’ˆ๋“ค์„ ์‚ฝ์ž…ํ• ๊ฒƒ

3. print์™€ input์œผ๋กœ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ƒํ’ˆ๋ฒˆํ˜ธ์™€ ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅ๋ฐ›๊ธฐ.

4. ์ž…๋ ฅ๋ฐ›์€ ์ฃผ๋ฌธ์ •๋ณด๋ฅผ ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”์— ์ €์žฅํ• ๊ฒƒ.

5. db์— ์ €์žฅ๋œ ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”์˜ ๋ชฉ๋ก์„ ์ถœ๋ ฅํ• ๊ฒƒ.

 

 

 

 

 

 

python
import sqlite3

connect = sqlite3.connect('al.sqlite', isolation_level=None)
c = connect.cursor()

color = ['red', 'blue']
size = ['S', 'M', 'L']
goods_code = ['T8081', 'T8082']
product_list = []
order_list = []

c.execute("CREATE TABLE order_list(id INTEGER PRIMARY KEY, num INTEGER, EA INTEGER)")
c.execute("CREATE TABLE product_list(id INTEGER PRIMARY KEY, goods_code TEXT)")
idx = 0

print("----์ƒํ’ˆ ๋ชฉ๋ก----")
for code in  goods_code:
    for s in size:
        for co in color:
            product = code + ", ์‚ฌ์ด์ฆˆ : " + s + ", ์ƒ‰๊น” : " + co
            product_list.append(product)
            idx += 1
            c.execute("INSERT INTO product_list(id, goods_code) VALUES(?,?)",(idx, product))
            print("%s๋ฒˆ : %s" %(idx, product))
idx = 0
while(True):
    num = input("์ƒํ’ˆ์„ ๊ณ ๋ฅด์„ธ์š”! ์ƒํ’ˆ์— ํ•ด๋‹นํ•˜๋Š” ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.")
    EA = input("์ฃผ๋ฌธํ•  ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•˜์„ธ์š”.")
    exitt = input("์ฃผ๋ฌธ์ด ๋๋‚ฌ์œผ๋ฉด 'x'๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”, ๊ณ„์†ํ•˜์‹œ๋ ค๋ฉด ์—”ํ„ฐ๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.")
    idx +=1
    c.execute("INSERT INTO order_list(id, num, EA) VALUES(?,?,?)", (idx, num, EA))
    order_list.append(num)
    if exitt == "x":
        break

for i,num in enumerate(order_list):
    c.execute("SELECT * FROM product_list WHERE id = '%s'"%num)
    op = c.fetchone()
    c.execute("SELECT * FROM order_list WHERE id = '%s'"%(i+1))
    oe = c.fetchone()
    print("์ฃผ๋ฌธ ์ƒํ’ˆ : %s, ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰ :%s๊ฐœ" %(op[1:] ,oe[2]))
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.