Python3 比较数据库中的数据并将答案写回数据库

Python3 compare data from DB and write the answer back to DB

[简介]

我目前正在通过 CGI 在 Python 3.7.4 中创建 Web 应用程序。 (网络服务器不支持wsgi)

该应用程序是一项简单的调查,用户在轮播表格中回答问题。 根据 respondentID 将答案写入数据库 (MySql)。 到现在为止没有问题。一切正常。

但是,我被要求在调查中插入一个逻辑并根据它显示结果。

[Objective]

假设我有 30 个问题,用户可以为每个问题回答 0、1 或 2。 仅当用户完成调查时才会处理答案。 完成时,数据在数据库中作为整数存储为 si001、si002、si003、....si030

如果数据库中有值,我想统计每个可能的答案被记录了多少。比如多少个0,多少个1,多少个2。

结果将记录在数据库中的不同列中。 (simaru, sisankaku, sibatsu) 我想做的是类似于下面的代码。 (代码明显错误) 我知道数据库的SELECT状态只能用一个cursor.execute来概括,但暂时(逻辑细节尚未完成)我想保持原样。

[代码]

@app.route('/results1', methods=['GET', 'POST'])
def results1():
# Check survey completion status
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM private_survey WHERE privateid = %s', (session['username'],))
account = cursor.fetchone()
if account['surveystatus'] == 'Available' or account['surveystatus'] == 'Started':
    # Check survey status, if available redirect to noresults
    return redirect(url_for('noresults'))
else:
    # Survey already completed, show results1
    cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
    cursor.execute('SELECT si001, si002, si003, si004, si005, si006, si007, si008, si009, si010, si011, si012, si013, si014, si015, si016, si017, si018, si019, si020, si021, si022, si023, si024, si025, si026, si027, si028, si029, si030 FROM private_survey INNER JOIN private_survey_answers ON private_survey.surveyid=private_survey_answers.surveyid WHERE private_survey.privateid=%s', (session['username'],))
    answer1 = cursor.fetchone()
    maru = 0
    sankaku = 0
    batsu = 0
    simaru = 0
    sisankaku = 0
    sibatsu = 0
    for x in answer1:
        if x == 2:
            simaru = maru + 1
        if x == 1:
            sisankaku = sankaku + 1
        if x == 0:
            sibatsu = batsu + 1
        cursor.execute('UPDATE private_survey_answers INNER JOIN private_survey ON private_survey.surveyid=private_survey_answers.surveyid SET simaru = %s, sisankaku = %s, sibatsu = %s WHERE private_survey.privateid = %s', (simaru, sisankaku, sibatsu, session['username'],))
        mysql.connection.commit()
    return render_template('results1.html', answer1=answer1, account=account)

任何帮助或想法将不胜感激。

我用计数函数解决了这个问题。 我找到的解决方案既可以实时显示计数,也可以将其写入数据库以供CSV下载等..

@app.route('/results1', methods=['GET', 'POST'])
def results1():
# Check survey completion status
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM private_survey WHERE privateid = %s', (session['username'],))
account = cursor.fetchone()
if account['surveystatus'] == 'Available' or account['surveystatus'] == '開始':
    # Check survey status, if available redirect to noresults
    return redirect(url_for('noresults'))
else:
    # Survey already completed, show results1
    cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
    cursor.execute('SELECT si001, si002, si003, si004, si005, si006, si007, si008, si009, si010, si011, si012, si013, si014, si015, si016, si017, si018, si019, si020, si021, si022, si023, si024, si025, si026, si027, si028, si029, si030 FROM private_survey INNER JOIN private_survey_answers ON private_survey.surveyid=private_survey_answers.surveyid WHERE private_survey.privateid=%s', (session['username'],))
    answer1 = cursor.fetchone()
    cnt = [answer1['si001'], answer1['si002'], answer1['si003'], answer1['si004'], answer1['si005']]
    cnt.count(2)
    cnt.count(1)
    cnt.count(0)
    cursor.execute('UPDATE private_survey_answers INNER JOIN private_survey ON private_survey.surveyid=private_survey_answers.surveyid SET simaru = %s, sisankaku = %s, sibatsu = %s WHERE private_survey.privateid = %s', (cnt.count(2), cnt.count(1), cnt.count(0), session['username'],))
    mysql.connection.commit()
    return render_template('results1.html', answer1=answer1, account=account, cnt=cnt)