赞
踩
Python3 + Sqlite3 FTS3全文搜索
最近在做一款桌面小程序,学习用Sqlite3管理本地数据,想用全文搜索功能。网上找的教程千篇一律,要么没头没尾,要么长篇大论,小白啃起来晦涩难懂,其实搞清楚了就是建个虚拟表,其他和普通表差不多,可惜不支持模糊查找:
import sqlite3 def fts_search(): con = sqlite3.connect("database.db") cur = con.cursor() cur.execute("DROP TABLE IF EXISTS test") cur.execute("create virtual table if not exists test using fts3(Customer_id varchar(15)," "Customer varchar(30))") cur.execute("insert into test(Customer_id, Customer) values('001', '吉利汽车')") cur.execute("insert into test(Customer_id, Customer) values('002', '吉利研究院')") cur.execute("insert into test(Customer_id, Customer) values('003', '浙江远景')") #a = cur.execute("select * from test where test match '浙江远景';").fetchall() # 精确查找 a = cur.execute("select * from test where test match '吉利*';").fetchall() # 通配符查找 con.commit() print(a) fts_search()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。