Add sqlite engine

Closes #2808
This commit is contained in:
Noémi Ványi 2021-05-13 21:40:25 +02:00
parent 0627fab511
commit 8e90a214ce
2 changed files with 57 additions and 0 deletions

50
searx/engines/sqlite.py Normal file
View File

@ -0,0 +1,50 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
SQLite database (Offline)
"""
import sqlite3
engine_type = 'offline'
database = ""
query_str = ""
limit = 10
paging = True
result_template = 'key-value.html'
def init(engine_settings):
if 'query_str' not in engine_settings:
raise ValueError('query_str cannot be empty')
if not engine_settings['query_str'].lower().startswith('select '):
raise ValueError('only SELECT query is supported')
def search(query, params):
query_params = {'query': query}
query_to_run = query_str + ' LIMIT {0} OFFSET {1}'.format(limit, (params['pageno'] - 1) * limit)
connection = sqlite3.connect(database)
cur = connection.cursor()
cur.execute(query_to_run, query_params)
results = _fetch_results(cur)
cur.close()
connection.close()
return results
def _fetch_results(cur):
results = []
titles = [name for (name, _, _, _, _, _, _) in cur.description]
res = cur.fetchone()
while res:
result = dict(zip(titles, map(str, res)))
result['template'] = result_template
results.append(result)
res = cur.fetchone()
return results

View File

@ -1019,6 +1019,13 @@ engines:
timeout : 3.0
disabled : True
# - name : sqlite
# engine : sqlite
# shortcut: sq
# database : mydb
# query_str : 'SELECT * FROM mytable WHERE fieldname=:query'
# disabled : True
- name : torrentz
engine : torrentz
shortcut : tor