-
파이썬으로 SQLITE 사용하기technology 2018. 8. 25. 04:03
#pysqlite 은 Python 2.5 이상에서 기본적으로 내장되어 있다.
import sqlite3
# SQLite DB 연결
conn = sqlite3.connect("test.db")
# Connection 으로부터 Cursor 생성
cur = conn.cursor()
# SQL 쿼리 실행
cur.execute("select * from customer")
# 데이타 Fetch
rows = cur.fetchall()
for row in rows:
print(row)
cur.close()
# 파라미터 사용 쿼리
cur = conn.cursor()
sql = "select * from customer where category=? and region=?"
cur.execute(sql, (1, 'SEA'))
rows = cur.fetchall()
for row in rows:
print(row)
cur.close()
#inset하기
cur = conn.cursor()
sql = "insert into customer(name,category,region) values (?, ?, ?)"
cur.execute(sql, ('홍길동', 1, '서울'))
conn.commit()
data = (
('홍진우', 1, '서울'),
('강지수', 2, '부산'),
('김청진', 1, '서울'),
)
sql = "insert into customer(name,category,region) values (?, ?, ?)"
cur.executemany(sql, data)
# Connection 닫기
conn.close()'technology' 카테고리의 다른 글
파이썬으로 디렉토리 복사, 디렉토리 이동, 디렉토리 삭제 (3) 2018.08.25 파이썬으로 파일, 디렉토리 다루는 OS 함수 (0) 2018.08.25 파이썬에서 명령행 인자 받기 (SYS.ARGV) (1) 2018.08.25 파이썬으로 로또 번호 자동 생성 예제 (1) 2018.08.25 Javascript : 모든 링크를 새창으로 뜨게 하기 (1) 2018.08.25