db.py 990 Bytes
import os

try:
    import sqlite3
except ImportError:
    try:
        from pysqlite3 import dbapi2 as sqlite3
    except ImportError:
        raise ImportError("系统缺少 sqlite3 且未安装 pysqlite3-binary。请运行: pip3 install pysqlite3-binary")


def get_connection(db_path):
    conn = sqlite3.connect(db_path)
    conn.execute("PRAGMA journal_mode=WAL;")
    return conn


def init_fts5_merchants(conn):
    cursor = conn.cursor()
    cursor.execute("DROP TABLE IF EXISTS merchants;")
    cursor.execute(
        "CREATE VIRTUAL TABLE merchants USING fts5("
        "merchantNo UNINDEXED, "
        "name, "
        "tokenize='trigram'"
        ");"
    )
    conn.commit()


def init_fts5_carbrands(conn):
    cursor = conn.cursor()
    cursor.execute("DROP TABLE IF EXISTS carbrands;")
    cursor.execute(
        "CREATE VIRTUAL TABLE carbrands USING fts5("
        "brandId UNINDEXED, "
        "name, "
        "tokenize='trigram'"
        ");"
    )
    conn.commit()