SQLAlchemy查询数据库并以html形式显示

时间:2018-09-19 06:57:51

标签: python mysql flask sqlalchemy flask-sqlalchemy

正在使用Flask Web应用程序,并且我有此工作行SQL命令将数据插入到数据库中。

if request.method == 'POST':
    cur = connection.cursor()
    employee_id = request.form['employee_id']
    emp_nums = {'1000': 'DEV', '1001': 'ANA'}
    if employee_id in emp_nums:
        print(employee_id)
        resultValue = cur.execute('''SELECT * FROM employee_data WHERE team 
        = "{}"'''.format(emp_nums[employee_id]))
        if resultValue > 0:
            print(resultValue)
            userDetails = cur.fetchall()
            return 
            render_template('attendance.html',userDetails=userDetails)
        return render_template('login.html')

正在尝试修改此代码以使用SQLAlchemy,但无法获得查询结果

@app.route('/attendance', methods = ['GET', 'POST'])
def attendance():
    if request.method == 'POST':
        o_id = request.form['o__id']
        emp_nums = {'1000': 'DEV', '1001': 'ANA'}
        team = emp_nums.get(o_id,'')
        print('team is',team)
        userDetails = Employee_data.query.filter_by(team = team).all()
        print(userDetails)
       return render_template('take_attendance.html', 
       userDetails=userDetails)
    return 'no data corresponding '

上面的代码不返回任何内容。谢谢您的帮助

0 个答案:

没有答案