db.py
990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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()