我有一张桌子set_reminders。我正在运行SELECT查询以获取行。即使将fetchone()值存储在变量中,然后将其用于进一步使用(如告知in this question),它也会显示TypeError。下面是我的问题的代码。

@app.route('/', methods=['GET', 'POST'])
def index():
    recur_next = ''
    if request.method == 'POST' and request.form['btn']=='XYZ':
        date = request.form['date']
        subject = 'Event'
        reminders='Placement Drive'
        cur = mysql.connection.cursor()
        search = cur.execute('SELECT DATE,SUBJECT,DESCRIPTION,RECUR_NEXT FROM set_reminder WHERE DATE=%s AND SUBJECT=%s AND DESCRIPTION=%s',(date,subject,reminders))
        data = cur.fetchall()
        a = cur.fetchone()
        recur_next = a['RECUR_NEXT']
        print(recur_next)
        cur.close()

    return '''<form method="post">
    <input type="date" name="date">
    <input type="submit" name="btn" value="XYZ">
    </form>'''


 File "def.py", line 22, in index
    recur_next = a['RECUR_NEXT']
TypeError: 'NoneType' object is not subscriptable


这是显示的错误。请帮帮我。

最佳答案

fetchall()清空游标的结果,因此fetchone()返回None,如果您先使用fetchone(),则fetchall()将是一个空列表。如果您都需要仅使用fetchall()并从那里获取数据。

10-08 11:08