랭귀지/python
Python SQLite 쿼리
유키공
2018. 10. 9. 09:00
SQLite를 사용하기 위해서는 우선 사용할 db 파일 (예: C:/Users/Lee/eclipse-workspace/systemTrading/analyze.db)을 sqlite3.connect() 메서드로 오픈하고, SQL 쿼리를 실행하여 데이타를 사용한 후, 마지막에 Connection을 닫으면 됩니다. DB Connection이 연결되면 Connection 객체가 리턴되는데, 이 객체로부터 커서를 생성하고 커서 객체의 execute() 명령을 실행하여 SQL 쿼리를 실행합니다. 아래 예제는 간단한 SELECT 문을 실행한 후, 전체 ROW 데이타를 출력하는 예입니다.
import sqlite3 # SQLite DB 연결 conn = sqlite3.connect("C:/Users/Lee/eclipse-workspace/systemTrading/analyze.db") # Connection 으로부터 Cursor 생성 cur = conn.cursor() # SQL 쿼리 실행 cur.execute("select code from customer") # 데이타 Fetch rows = cur.fetchall() for row in rows: print(row[0]) # Connection 닫기 conn.close()