Python实现Sqlite数据库搜索引擎

69 阅读2分钟

我有一个包含2个表的sqlite数据库,需要为其创建一个cgi搜索引擎,搜索引擎在下拉菜单中提供2个搜索选项:“名称”和“关键字”。如果选择“关键字”,则在第一个表的任意列中按“关键字”进行搜索。如果选择“名称”,则在第二个表的第1列中搜索与输入的字符串完全匹配的记录。 我已经创建了包含搜索框和下拉菜单的cgi网页界面,并可以保存输入的搜索关键字。现在我卡在了如何编写cgi脚本,来根据输入的关键字对数据库进行搜索并且将结果显示到屏幕上。

huake_00152_.jpg

解决方法:

  1. 导入必要的库:
import sqlite3
import cgi
  1. 根据用户选择连接不同的数据库表:
def connect_table(dropdown):
    if dropdown == 'Keyword':
        connection = sqlite3.connect('table1.db')
    elif dropdown == 'Name':
        connection = sqlite3.connect('table2.db')
    return connection
  1. 处理用户输入数据和搜索数据库:
def search_database(connection, desc, dropdown):
    if dropdown == 'Keyword':
        query = "SELECT * FROM table WHERE COLUMN LIKE ?"
    elif dropdown == 'Name':
        query = "SELECT * FROM table WHERE COLUMN = ?"
    result = connection.execute(query, ('%' + desc + '%',))
    return result
  1. 将结果显示到屏幕上:
def display_results(result):
    print("<table border=1>")
    for row in result:
        print("<tr>")
        for column in row:
            print("<td>{}</td>".format(column))
        print("</tr>")
    print("</table>")
  1. 根据用户选择,调用相应的方法并显示结果:
# Get data from user
args = cgi.FieldStorage()
desc = args.getfirst("desc")
dropdown = args.getfirst("dropdown")

# Connect to database
connection = connect_table(dropdown)

# Search database
result = search_database(connection, desc, dropdown)

# Display results
display_results(result)

完整的cgi脚本如下:

import sqlite3
import cgi

"""set up the content type, open the html, title, and body"""

print('<form action=search.cgi>')
print('<select name='dropdown'>')
print('  <option> Name </option>')
print('  <option> Keyword </option>')
print('</select>')

print('<input type='text' name='desc' size='25'>')
print('<button type="submit"> Go </button>')
print('</form>')

args = cgi.FieldStorage()
desc = args.getfirst("desc")
dropdown = args.getfirst("dropdown")


def connect_table(dropdown):
    if dropdown == 'Keyword':
        connection = sqlite3.connect('table1.db')
    elif dropdown == 'Name':
        connection = sqlite3.connect('table2.db')
    return connection


def search_database(connection, desc, dropdown):
    if dropdown == 'Keyword':
        query = "SELECT * FROM table WHERE COLUMN LIKE ?"
    elif dropdown == 'Name':
        query = "SELECT * FROM table WHERE COLUMN = ?"
    result = connection.execute(query, ('%' + desc + '%',))
    return result


def display_results(result):
    print("<table border=1>")
    for row in result:
        print("<tr>")
        for column in row:
            print("<td>{}</td>".format(column))
        print("</tr>")
    print("</table>")


if dropdown == 'Keyword':
    connection = connect_table(dropdown)
    result = search_database(connection, desc, dropdown)
    display_results(result)
elif dropdown == 'Name':
    connection = connect_table(dropdown)
    result = search_database(connection, desc, dropdown)
    display_results(result)

复制这段代码并将其保存在一个.cgi文件中,然后将其上传到你的web服务器,这样就可以使用你的cgi搜索引擎来搜索sqlite数据库了。