
โ Python ํ๊ฒฝ์์ SQLite ์ฌ์ฉํ๊ธฐ
Placeholder
import sqlite3
connection = sqlite3.connect("users.db")
cursor = connection.cursor()
def init_table():
cursor.execute(
"""
CREATE TABLE users (
user_id integer primary key autoincrement,
username text not null,
password text not null
);
"""
)
cursor.execute(
"""
insert into users (username, password)
values ('nico', 123), ('lynn', 321);
"""
)
def print_all_users():
result = cursor.execute("select * from users;")
data = result.fetchall()
print(data)
def i_change_password(username, new_password):
cursor.execute(
f"UPDATE users SET password = '{new_password}' WHERE username = '{username}'"
)
def s_change_password(username, new_password):
cursor.execute(
"UPDATE users SET password = ? WHERE username = ?", (new_password, username)
)
s_change_password("nico", "hached again' --")
print_all_users()
connection.commit()
connection.close()
i_change_password() ๋ฐฉ์์ SQL Injection ์ํ์ ๋
ธ์ถ๋์ด ์๋ ๋ฐฉ์์ด๋ค.
s_change_password() ๋ฐฉ์์ ์ฌ์ฉํ์ฌ ?๋ฅผ placeholder๋ก ์ฌ์ฉํด ๋ณด์์ ๊ฐํํ๋ค.
executemany
import sqlite3
connection = sqlite3.connect("users.db")
cursor = connection.cursor()
def init_table():
cursor.execute(
"""
CREATE TABLE users (
user_id integer primary key autoincrement,
username text not null,
password text not null
);
"""
)
cursor.execute(
"""
insert into users (username, password)
values ('nico', 123), ('lynn', 321);
"""
)
def print_all_users():
result = cursor.execute("select * from users;")
data = result.fetchall()
print(data)
def i_change_password(username, new_password):
cursor.execute(
f"UPDATE users SET password = '{new_password}' WHERE username = '{username}'"
)
def s_change_password(username, new_password):
cursor.execute(
"UPDATE users SET password = ? WHERE username = ?", (new_password, username)
)
data = [
("lannna", 567),
("bora", 123),
("max", 123),
("jja", 898),
]
# cursor.executemany("INSERT INTO users (username, password) VALUES (?, ?)", data)
data = [
{"name": "lannna", "password": 567},
{"name": "bora", "password": 123},
{"name": "max", "password": 123},
{"name": "jja", "password": 898},
]
cursor.executemany(
"INSERT INTO users (username, password) VALUES (:name, :password)", data
)
print_all_users()
connection.commit()
connection.close()
placeholder๋ฅผ ์ฌ์ฉํ ๋ ์ ๋ฌํด์ฃผ์ด์ผ ํ๋ ๊ฐ์ด key์ value๋ฅผ ๊ฐ์ง ๋, :์ key๋ฅผ ์ฌ์ฉํ์ฌ value๋ฅผ ๋๊ธธ ์ ์๋ค.
Cursor, fetchmany, fetchone, etc
import sqlite3
conn = sqlite3.connect("movies_download.db")
cur = conn.cursor()
res = cur.execute(
"SELECT movie_id, title FROM movies ORDER BY movie_id"
) # ์ฌ๊ธฐ์ ์ ํ๋ ๋ฐ์ดํฐ๋ค์ ์์ง ๋ด ํ์ด์ฌ ํ๋ก๊ทธ๋จ์(๋ฉ๋ชจ๋ฆฌ์) ๋ก๋๋ ๊ฒ์ด ์๋๋ค.
# all_movies = res.fetchall() # ์ฌ๊ธฐ์ fetchall์ ํด์ผ ์ค์ ๋ก ๋ก๋๊ฐ ๋ ๊ฒ์ด๋ค.
print(res.fetchmany(20))
print(res.fetchone(), res.fetchone(), res.fetchone())
for movie in res:
print(
movie
) # ์ํํ๋ฉด์ ๋ชจ๋ ์ํ๋ค์ ๊ฐ์ ธ์ค์ง๋ง, fetchall์ฒ๋ผ ํ ๋ฒ์ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ํ์ง๋ ์์์, ๋ถ๋ด์ด ์ ๋ค.
conn.commit()
conn.close()
fetchmany(20)์ ํ์ ๋ cursor๋ 20๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ ๋ค ๋ง์ง๋ง ๋ฐ์ดํฐ ๊ฐ์ ์์น์ cursor๋ฅผ ๋๊ฒ ๋๋ค.
๊ทธ๋์ ๋ค์์ fetchmany(20)์ ๋ ํ๊ฒ ๋์ ๋, ๊ธฐ์กด ๊ฐ๋ค์ skipํ๊ณ 20๊ฐ์ ๊ฐ์ ๊ฐ์ ธ์ค๊ฒ ๋๋ค.
(์ด๋ฌํ ์ด์ ๋ก fetchmany(20)๊ฐ์ ๋์์ ํ๊ณ ๋ ํ, fetchall์ ํ๊ฒ ๋๋ฉด ๋ชจ๋ ๊ฐ์ ๊ฐ์ ธ์ค๋ ๊ฒ์ด ์๋๋ผ ์์ 20๊ฐ์ ๋ฐ์ดํฐ๋ ํฌํจ์ด ๋์ง ์๋๋ค.)
โ Python์์ SQLite, PostgreSQL, MySQL๊ณผ ์ํตํ๋ ๋ฐฉ์์ ๊ฐ๋ค.
๋ชจ๋ driver๊ฐ Python Database API Specification์ ๋ง์กฑํ์ฌ ๊ตฌํํ์๊ธฐ ๋๋ฌธ
PostgreSQL driver document
https://www.psycopg.org/psycopg3/docs/basic/usage.html
Basic module usage - psycopg 3.3.4.dev1 documentation
Previous Installation
www.psycopg.org
MySQL driver document
https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html
MySQL :: MySQL Connector/Python Developer Guide :: 5.1 Connecting to MySQL Using Connector/Python
5.1 Connecting to MySQL Using Connector/Python The connect() constructor creates a connection to the MySQL server and returns a MySQLConnection object. The following example shows how to connect to the MySQL server: import mysql.connector cnx = mysql.conn
dev.mysql.com
'๐งฉ SQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| SQL - Supabase & Drizzle (0) | 2026.05.22 |
|---|---|
| SQL - Drizzle(ORM) (0) | 2026.04.13 |
| SQL - SQL Injection (0) | 2026.04.09 |
| SQL - PostgreSQL, Extensions (0) | 2026.04.06 |
| SQL - PostgreSQL, JSON Column (0) | 2026.04.06 |