这个搜索查询(Python和SQLite3)有什么问题?

xpcnnkqh  于 12个月前  发布在  SQLite
关注(0)|答案(1)|浏览(126)

我试图搜索我的数据库并显示结果,但我的代码什么也没有返回,我无法找出错误在哪里。
这就是我如何抓取数据并将其插入数据库:

def getJobsData(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0'}
    r = requests.get(url, headers=headers)
    soup = BeautifulSoup(r.content, 'html.parser')

    data = []
    for e in soup.select('.card'):
        data.append({
            'title': e.h2.text,
            'desc': e.ul.get_text('\n', strip=True)
            })
    
    try:
        for item in data:
            c.execute('''INSERT INTO job VALUES(?,?)''',
                (item['title'], item['desc']))
        return
    except:
        return

字符串
这是搜索代码:

@app.route("/search", methods=['GET', 'POST'])
def search():
    error = ""
    user_input = None
    data = None
    try:
        if request.method == "POST":
            conn = sqlite3.connect('jobs.db')
            c = conn.cursor()
            c.execute('''SELECT * FROM job WHERE title = (?,)''', (request.form["search"],))
            data = c.fetchall()
            user_input = (request.form["search"],)
            return render_template("search.html", data=data, user_input=user_input)

    except Exception as e:
        error = (str(e))
        return render_template("search.html", error=error, user_input=user_input)


超文本标记语言:

<form class="navbar-form navbar-left" action="{{ url_for('search') }}" method="POST" role="search">
    <div class="form-group">
      <input type="text" class="form-control" placeholder="Search" name="search" value="{{ request.form.search }}">
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
  </form>


我正在使用Python,Flask和SQLite3。我做错了什么,我如何修复这个?谢谢!

**编辑:**我意识到代码确实从数据库返回数据,但搜索查询必须与标题完全匹配。试图找出如何解决这个问题。这是我更新的内容,打印语句只是为了测试:

if request.method == "POST":
        conn = sqlite3.connect('jobs.db')
        c = conn.cursor()
        user_input = request.form["search"]
        data = c.execute('''SELECT * FROM job WHERE title LIKE ?''', (user_input,)).fetchall()
        print("USER INPUT:" + user_input)
        print(data)
        for d in data:
            print(d)
        return render_template("search.html", data=data, user_input=user_input)

bxfogqkk

bxfogqkk1#

SQL中的=运算符应该取一个值:

c.execute('''SELECT * FROM job WHERE title = ?''', (request.form["search"],))
# Here --------------------------------------^

字符串

相关问题