mysql.connector.errors.ProgrammingError:光标未连接

在输入页面上写下我想要搜索的单词和单词内的字母后,它会转到/search4,并且我在页面上收到错误mysql.connector.errors.ProgrammingError: 检查这里的代码后,光标未连接。


from flask import Flask, render_template, request, escape

from vsearch import search4letters

from DBcm import UseDatabase


app = Flask(__name__)


app.config['dbconfig'] = {'host': '127.0.0.1',

                            'user': 'vsearch',

                            'password': 'vsearchpasswd',

                            'database': 'vsearchlogDB', }


def log_request(req: 'flask_request', res: str) -> None:

    with UseDatabase(app.config['dbconfig']) as cursor:

        _SQL = """insert into log

                (phrase, letters, ip, browser_string, results)

                values

                (%s, %s, %s, %s, %s)"""

    cursor.execute(_SQL, (req.form['phrase'],

                            req.form['letters'],

                            req.remote_addr,

                            req.user_agent.browser,

                            res, ))


@app.route('/search4', methods=['POST'])

def do_search() -> 'html':

    phrase = request.form['phrase']

    letters = request.form['letters']

    title = 'Here are your results:'

    results = str(search4letters(phrase, letters))

    log_request(request, results)

    return render_template('results.html',

                            the_title=title,

                            the_phrase=phrase,

                            the_letters=letters,

                            the_results=results,)


@app.route('/')

@app.route('/entry')

def entry_page() -> 'html':

    return render_template('entry.html',

                            the_title='Welcome to search4letters on the web!')




慕尼黑的夜晚无繁华
浏览 1703回答 1
1回答

冉冉说

调用cursor.execute应该在with块内,因为cursor仅存在于该块内。这实际上是一个缺失的缩进:def log_request(req: 'flask_request', res: str) -> None:    with UseDatabase(app.config['dbconfig']) as cursor:        _SQL = """insert into log                (phrase, letters, ip, browser_string, results)                values                (%s, %s, %s, %s, %s)"""        cursor.execute(_SQL, (req.form['phrase'],                            req.form['letters'],                            req.remote_addr,                            req.user_agent.browser,                            res, ))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python