Notice
Recent Posts
Recent Comments
Link
아미(아름다운미소)
Python SQLite 쿼리 본문
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()
'랭귀지 > python' 카테고리의 다른 글
python에서 sqlite3 사용하여 조건 카운트하기 (fetchone) (0) | 2018.10.12 |
---|---|
python list (0) | 2018.10.10 |
python "No module win32com.client"오류 발생시 (0) | 2018.10.08 |
python 정규식 표현 중 자주 사용되는 패턴 (0) | 2018.10.07 |
[ python] Extract sentences with specific words in a text file (0) | 2018.10.05 |
Comments