Skip to content

SQLite:FTS5

SQLite의 Full-Text Search.

Usage

To use FTS5, the user creates an FTS5 virtual table with one or more columns. For example:

CREATE VIRTUAL TABLE email USING fts5(sender, title, body);

Examples

Python example

import sqlite3

# 데이터베이스 연결 및 테이블 생성
conn = sqlite3.connect(":memory:")  # 임시 DB
cursor = conn.cursor()

# FTS5 테이블 생성
cursor.execute("CREATE VIRTUAL TABLE documents USING fts5(content)")

# 데이터 삽입
documents = [
    ("Python은 강력한 프로그래밍 언어입니다.",),
    ("데이터 분석과 머신러닝에 적합합니다.",),
    ("Python은 다양한 라이브러리를 제공합니다.",)
]
cursor.executemany("INSERT INTO documents(content) VALUES (?)", documents)

# 전문 검색
query = "Python"
cursor.execute("SELECT rowid, content FROM documents WHERE content MATCH ?", (query,))
results = cursor.fetchall()

# 결과 출력
for row in results:
    print(row)

See also

Favorite site