我正在研究一個與簡單的sqlite數據庫交互的Python程序。我試圖構建一個搜索工具,根據用戶輸入,它將交互式地「過濾」數據庫,然後返回與搜索匹配的行(項目)。例如...過濾sqlite - 逐個執行動作
我的Python程序(通過if語句,cgi.FieldStorage()和whatnot)應該能夠接受用戶輸入,然後通過數據庫搜索。這裏是該程序的通用代碼:
import cgitb; cgitb.enable()
import cgi
import sys
import sqlite3 as lite
import sys
con = lite.connect('bikes.db')
form = cgi.FieldStorage()
terrain_get = form.getlist("terrain")
terrains = ",".join(terrain_get)
handlebar_get = form.getlist("handlebar")
handlebars = ",".join(handlebar_get)
kickstand = form['kickstand'].value
正如你所看到的,那部分是接收用戶的輸入;工作正常(我認爲)。接下來,我需要幫助:
if 'dirtrocky' not in terrains:
FILTER the database to not return items that have "dirtrocky' in their terrain field
然後在以後的程序,我希望能夠在我的過濾器擴展:
if 'drop' not in handlebars:
FILTER the database to, much like in previous one, not return items that have 'drop' in their 'handlebar' field
我的問題是,如何才能篩選數據庫?理想情況下,我的最終結果應該是在「過濾掉」上述內容後留下的行的ID元組。
謝謝!
你可以顯示你想要的數據庫模式嗎?你想如何存儲像地形這樣的多值字段? – mvp
我明白你的意思了。我想我會只使用多個表。但是,在查看[這個問題](http://stackoverflow.com/questions/11535685/multiple-elements-in-one-database-cell)等答案,我有點不清楚如何人們會設置它。我理解一個*主表*的想法,其中包括ID,標題,價格等。但是,對於多值字段,您如何設置?謝謝! – tehsockz