์ƒˆ์†Œ์‹

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

(python) Sqlite ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ฝ๊ธฐ , ์ˆ˜์ • , ์‚ญ์ œ (create, read, update, delete)

  • -

CRUD - ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฐฉ๋ฒ• Create(์ƒ์„ฑ), Read(์ฝ๊ธฐ), Update(๊ฐฑ์‹ ), Delete(์‚ญ์ œ)

https://infinitt.tistory.com/138?category=1100388

 

(python) Sqlite ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์‚ฌ์šฉ, ํ…Œ์ด๋ธ” ์กฐํšŒ, ๋ฐ์ดํ„ฐ ์‚ฝ์ž…

*Sqlite ์—ฐ๋™ import sqlite3 conn = sqlite3.connect('db.sqlite', isolation_level= None) c = conn.cursor() ํ˜„์žฌ ํŒŒ์ผ๊ฒฝ๋กœ์— db.sqlite๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. isolation_level = None์œผ๋กœ ํ•˜๊ฒŒ๋˜๋ฉด, ์ปค๋ฐ‹(commit)์ด..

infinitt.tistory.com

์ง€๋‚œ๋ฒˆ์— ์ƒ์„ฑํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ์ง„ํ–‰ํ•˜๋ฉด,

 

 

 

 

*์ปค์„œ (cursor)

import sqlite3
conn = sqlite3.connect ('db1.sqlite')
c = conn.cursor()

sqlite3์„ ์ž„ํฌํŠธํ•˜๊ณ , ํŒŒ์ผ๊ณผ ์—ฐ๊ฒฐ, ์ปค์„œ๋ฅผ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค.

์ฒ˜์Œ ์ปค์„œ๋ฅผ c๋กœ ์„ค์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ปค์„œ๋Š” ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ๋‹จ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์˜ ๊นŒ๋งŒ๋ถ€๋ถ„์— ์œ„์น˜ํ•œ๋‹ค๊ณ  ์ƒ๊ฐ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ ํ•œ์ค„์„ ์ฝ์œผ๋ผ๋Š” ๋ช…๋ น์„ ๋‚ด๋ฆฌ๋ฉด, ์ปค์„œ๋Š” ์ฒซ๋ฒˆ์งธ ์ค„์„ ์ฝ๊ณ  ๋‹ค์Œํ–‰(row)์œผ๋กœ ์ด๋™ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ ๋‚˜์„œ ๋˜ ํ•œ์ค„์„ ์ฝ์œผ๋ผ๊ณ  ํ•˜๋ฉด, ์ปค์„œ๊ฐ€ ์ด๋™ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฒซ๋ฒˆ์งธ ์ค„์ด ์•„๋‹Œ, ๋‘๋ฒˆ์งธ ์ค„์„ ์ฝ์Šต๋‹ˆ๋‹ค.

 

 

 

 

*ํ…Œ์ด๋ธ” ์กฐํšŒ (์ฝ๊ธฐ, read)

๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ธฐ์œ„ํ•ด์„œ๋Š” ํ…Œ์ด๋ธ”์„ ๋จผ์ € SELECTํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

c.execute("SELECT * FROM users")  # *๋Š” ์ „๋ถ€๋ผ๋Š” ์˜๋ฏธ, ๋”ฐ๋ผ์„œ users๋กœ๋ถ€ํ„ฐ ๋ชจ๋“ ๊ฒƒ์„ ์„ ํƒ

์„ ํƒ์„ ํ–ˆ์œผ๋ฏ€๋กœ, ์ด์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ํ„ฐ๋ฏธ๋„์ฐฝ์—์„œ ์ถœ๋ ฅ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ์–ด์˜ค๋Š” ๋ช…๋ น์–ด๋Š” ํฌ๊ฒŒ 3๊ฐ€์ง€์ž…๋‹ˆ๋‹ค.

fetchone - ํ•œ์ค„์ฝ๊ธฐ,

fetchall() - ์ „๋ถ€์ฝ๊ธฐ,

fetchmany(size=x) - x์ค„ ์ฝ๊ธฐ

read1 = c.fetchone()
print(read1)

read2 = c.fetchall()
print(read2)

read3 = c.fetchmany(size = 3)
print(read3)

 

*ํŠน์ • row ๊ฐ€์ ธ์˜ค๊ธฐ (WHERE ์ด์šฉ)

์ง€๊ธˆ ์‚ฌ์šฉ์ค‘์ธ ํ…Œ์ด๋ธ” users์—๋Š” id๊ฐ’์ด 2,5,13,23,33 ์œผ๋กœ ์ด 5๊ฐœ์˜ row๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์ค‘๊ฐ„๊ฐ’์ธ 13๋งŒ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด

๋‹ค์Œ์ฒ˜๋Ÿผ ๊ฐ€์ ธ ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

#1 ?
c.execute('SELECT * FROM users WHERE id =?',(2,))
print(c.fetchone())

#2 %ํฌ๋ฉง์ด์šฉ
c.execute('SELECT * FROM users WHERE id = "%s" ' %13)
print(c.fetchall())


#3 ๋”•์…”๋„ˆ๋ฆฌ ์ด์šฉ
c.execute('SELECT * FROM users WHERE id = ID',{"ID":23})
print(c.fetchone())


# ๋‘๊ฐœ ์ด์ƒ์˜ row ๊ฐ€์ ธ์˜ค๊ธฐ
#1
p = (2,33)
c.execute("SELECT * FROM users WHERE id IN(?,?)",p)
print(c.fetchall())

#2 OR ์ด์šฉ
c.execute("SELECT * FROM users WHERE id=:id1 OR id=:id2",{"id1":2,"id2":5})
print(c.fetchall())

(or์€ ๋‘˜์ค‘ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ํ•ด๋‹น๋˜๋„ ๊ฐ€์ ธ์˜ค๋ฉฐ, and๋Š” ๋‘˜๋‹ค ๋งŒ์กฑํ•ด์•ผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.)

 

 

 

 

*๋ฐ์ดํ„ฐ ์ˆ˜์ • (update)

# 1. ?
c.execute("UPDATE users SET username = ? WHERE id = ?",('you','2'))

#2. ๋”•์…”๋„ˆ๋ฆฌ
c.execute("UPDATE users SET username = :new_name WHERE id = :ID",  {"new_name":"park", "ID":2})

#3. %ํฌ๋ฉง
c.execute("UPDATE users SET username = '%s' WHERE id = '%s'"%('park',2))

 

 

 

 

*๋ฐ์ดํ„ฐ ์‚ญ์ œ(delete)

#1. ?
c.execute("DELETE FROM users WHERE id = ?", (2,))

#2. ๋”•์…”๋„ˆ๋ฆฌ
c.execute("DELETE FROM users WHERE id = :ID",{"ID":5})
#3. %ํฌ๋ฉง
c.execute("DELETE FROM users WHERE id = '%s'" %13)

ํ…Œ์ด๋ธ”์˜ row๊ฐ€ ์•„๋‹Œ, ํ…Œ์ด๋ธ” ์ž์ฒด๋ฅผ ์‚ญ์ œํ•˜์‹œ๋ ค๋ฉด (ํ…Œ์ด๋ธ” ์ „์ฒด์‚ญ์ œ)

conn.execute("DELETE FROM users").rowcount

 

 

*commit() ๋งˆ์ง€๋ง‰์œผ๋กœ, ์‚ญ์ œ, ์ˆ˜์ •, ์‚ฝ์ž…์˜ ๊ณผ์ •๋’ค์—๋Š” ์ปค๋ฐ‹์„ ํ•ด์•ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค.

conn.commit()

 

 

 

Contents

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

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