์ƒˆ์†Œ์‹

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

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

  • -

*Sqlite ์—ฐ๋™

import sqlite3

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

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

์ปค๋ฐ‹์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๊ด€๋ จ๋œ ๋ช…๋ น์–ด๋ฅผ ์Œ“์•„๋‘์—ˆ๋‹ค๊ฐ€, ์‹คํ–‰์‹œํ‚ค๋Š” ๋ช…๋ น์„ ๋œปํ•ฉ๋‹ˆ๋‹ค. (์ฆ‰, ์ปค๋ฐ‹์„ ํ•˜์ง€ ์•Š๊ฒŒ๋˜๋ฉด ์ง์ ‘์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฐ˜์˜๋˜๋Š” ์˜ํ–ฅ์€ ์—†์Šต๋‹ˆ๋‹ค.)

 

*๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฐ์ดํ„ฐํƒ€์ž…

TEXT, NUMERIC , INTEGER, REAL , BLOB

blob์€ ํŒŒ์ผ, integer์€ ์ •์ˆ˜, numeric๊ณผ real์€ ์‹ค์ˆ˜, text๋Š” ๋ฌธ์ž์—ด์„ ๋œปํ•ฉ๋‹ˆ๋‹ค.

 

*ํ…Œ์ด๋ธ” ์ƒ์„ฑ

5๊ฐ€์ง€ ํ•ญ๋ชฉ(column, ์—ด)์ด ์žˆ๋Š”, ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ด๋ณด๋ฉด ๋‹ค์Œ์ฒ˜๋Ÿผ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

c.execute("CREATE TABLE IF NOT EXISTS users\
(id INTEGER PRIMARY KEY, username TEXT, email TEXT, phone TEXT, regist_date TEXT)")

์ด๋•Œ id๋Š” primary key๋กœ ์„ค์ •ํ–ˆ๋Š”๋ฐ, ์ฃผ๋กœ ์ˆœ๋ฒˆ์„ ๋งค๊ธฐ๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ณ ์œ ๊ฐ’์ด๊ธฐ์—, ์ค‘๋ณต๋˜๋Š” id๋ฅผ ๊ฐ€์งˆ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค.

์ƒ์„ฑ๋œ ๋ชจ์Šต์€ ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. usersํ…Œ์ด๋ธ”์— 5๊ฐ€์ง€ Column์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค.

 

*๋ฐ์ดํ„ฐ ์‚ฝ์ž…

๋ฌธ์ž์—ด ๊ฐ™์€๊ฒฝ์šฐ๋Š” ๋ฐ”๋กœ ์‚ฝ์ž…์„ ํ•˜๋ฉด ๋˜์ง€๋งŒ, ๋ณ€์ˆ˜์— ์ €์žฅ๋œ๊ฐ’๋“ค์€ ?๋ฅผ ์ด์šฉํ•ด ํฌ๋งทํŒ…ํ•˜๋“ฏ์ด ๋„ฃ์–ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

*์ง์ ‘์‚ฝ์ž…

1.c.execute("INSERT INTO ํ…Œ์ด๋ธ”๋ช… VALUES (์‚ฝ์ž…ํ•  ๋ฐ์ดํ„ฐ, ๋ฐ์ดํ„ฐ,๋ฐ์ดํ„ฐ,?)", (?์—๋Œ€ํ•œ ๋ณ€์ˆ˜))

import datetime
now = datetime.datetime.now()

c.execute("INSERT INTO users VALUES\
(5, 'Kim', 'kim@naver.com','010-2512-1212',?)",(now,))

2.c.execute("INSERT INTO ํ…Œ์ด๋ธ”๋ช… VALUES (?,?,?,?,?)", (?์—๋Œ€ํ•œ ๋ณ€์ˆ˜))

c.execute("INSERT INTO users(id, username, email, phone, regist_date) VALUES(?,?,?,?,?)"\
,(2,'park','Park@naver,com','010-2224-2412','park.com',now))

*Many์‚ฝ์ž…

์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•ญ๋ชฉ์„ ์‚ฝ์ž…ํ• ๋•Œ๋Š” Many๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

userList = (
    (13, 'Lee','Lee@naver.com','010-2222-2222',now),
    (23, 'Lee','Lee@naver.com','010-2222-2222',now),
    (33, 'Lee','Lee@naver.com','010-2222-2222',now),
)

์˜ˆ๋ฅผ๋“ค์–ด ์ด๋Ÿฐ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ์œผ๋ฉด, ์œ„์˜ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” 3๋ฒˆ์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด์•ผํ•˜์ง€๋งŒ , many๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ ๋˜๋Š” ํŠœํ”Œํ˜•ํƒœ๋ฅผ ํ•œ๋ฒˆ์— ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

c.executemany("INSERT INTO users(id, username, email, phone, regist_date) VALUES(?,?,?,?,?)",userList)

 

 

* rollback() commit() close()

rollback : ๋กค๋ฐฑ์€ ์Œ“์•„๋†จ๋˜ ๋ช…๋ น์–ด๋ฅผ ๋˜๋Œ๋ฆฌ๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. Ctrl+Z์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์ด ๊ธ€ ๋งจ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ์˜คํ† ์ปค๋ฐ‹(isolation_level = None)์„ ์ง€์›Œ์•ผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

commit : ์Œ“์•„๋†จ๋˜ ๋ช…๋ น๋“ค์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. (๋ฐ˜์˜)

close() : ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ์—ฐ๋™์„ ๋Š์Šต๋‹ˆ๋‹ค.

Contents

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

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