错误= 1054(42S22):“字段列表”中的未知列“ company_name”

时间:2019-05-06 07:44:24

标签: mysql python-3.x flask

我知道对此错误代码已经提出了一些问题,但是在我的情况下,查询正在对我的测试文件进行处理,但是在转换为flask方法之后,它给了我一个错误 我正在研究烧瓶,我的任务是在给定日期之间创建和下载特定用户的excel 我的烧瓶方法代码如下

@app.route('/excel_sheet')
def mk_excel_sheet():

    sort_user = request.args.get('sort_user2', 'abc1')
    from_date = request.args.get('from_date2', 'abc2')
    to_date = request.args.get('to_date2', 'abc3')
    company_name = session.get('company_name', 'abc4')

    print(f"{sort_user}\n{from_date}\n{to_date}\n{company_name}")

    file_name = f"{sort_user}-{from_date[:2]}-{from_date[3:5]}-{from_date[6:10]}-{to_date[:2]}-{to_date[3:5]}-{to_date[6:10]}.xlsx"
    print("file_name = ",file_name)
    file_path = 'static/'+str(file_name)
    print("file_path = ", file_path)
    #print(from_date, to_date)

    cur = connn.cursor()

    query = """
        SELECT date_format, type, username, time_in, address_in, time_out, address_out 
        FROM (
            SELECT date_today AS 'date_format', 'attendence' AS 'type', user AS 'username', 
                company_name AS 'comp_nam', STR_TO_DATE(in_time, '%d-%m-%Y %H:%i:%s') AS 'time_in', in_address AS 'address_in', STR_TO_DATE(out_time, '%d-%m-%Y %H:%i:%s') AS 'time_out', 
                out_address AS 'address_out' 
            FROM attendence 
            UNION ALL 
            SELECT add_time, 'visit', username, company_name, STR_TO_DATE(visit_time_in, '%d-%m-%Y %H:%i:%s'), 
                location_in, STR_TO_DATE(visit_time_out, '%d-%m-%Y %H:%i:%s'), location_out 
            FROM visits) t 
        WHERE t.username = '"""+str(sort_user)+"""' AND t.comp_nam = '"""+str(company_name)+"""' AND time_in BETWEEN STR_TO_DATE('"""+str(from_date)+"""', '%d-%m-%Y %H:%i:%s') AND STR_TO_DATE('"""+str(to_date)+"""', '%d-%m-%Y %H:%i:%s') 
        ORDER BY time_in ASC;"""

    try:
        cur.execute(query)
        data = cur.fetchall()
        print("data = ", data)

        book = xlsxwriter.Workbook(file_path)
        sheet = book.add_worksheet('sheet 1')
        bold = book.add_format({'bold': True})
        text_wrap = book.add_format({'text_wrap': True})

        sheet.write(0,0,'DATE',bold)
        sheet.write(0,1,'TYPE',bold)
        sheet.write(0,2,'USERNAME',bold)
        sheet.write(0,3,'TIME IN',bold)
        sheet.write(0,4,'LOCATION IN',bold)
        sheet.write(0,5,'TIME OUT',bold)
        sheet.write(0,6,'LOCATION OUT',bold)

        r = 2
        for r, row in enumerate(data):
            print(f"r = {r} | row = {row}")
            date = row[0]
            row_date = row[0]
            print(f"{date}|{row_date}")
            #if row_date is date

            for c, col in enumerate(row):
                print(f"c = {c} | col = {col}")
                sheet.set_column('{0}:{0}'.format(chr(c + ord('A'))), len(str(col)) + 2)
                sheet.write(r, c, col if type(col) is not datetime else col.strftime('%d-%m-%Y %H:%M:%S'), text_wrap)

        book.close()

    except Exception as e:
        print("ERROR = ",e)
    try:
        return send_file(file_path, mimetype='text/xlsx', attachment_filename=file_name, as_attachment=True)
    except Exception as e:
        print("FILE ERROR = ",e)

我得到ERROR = 1054 (42S22): Unknown column 'company_name' in 'field list' 什么是我的mysql查询中的字段列表 我不知道为什么,因为查询在正常的test.py文件中工作 所以有人可以指出我做错了吗

0 个答案:

没有答案