基于HTML选择输入的SQL查询

时间:2018-08-15 15:53:18

标签: html sql flask

我正在用Flask开发一个webapp。我的数据存储在BigQuery中。我想根据用户从html输入界面中选择的内容编写sql查询。 Click here to see my Data Table in BigQuery

当前,在这种情况下,我正在对要查询的输入“ Glycol_Supply”进行硬编码,但是我希望用户从HTML输入中进行选择并基于此显示结果。

def queryBuilder():
query = """SELECT Record_Start_Time, Glycol_Supply 
         FROM Furnace_Output.DataTable
         WHERE Record_Start_Time BETWEEN TIMESTAMP("{}") 
         AND TIMESTAMP("{}")
         ORDER BY Record_Start_Time
         LIMIT 100""".format(request.form['start'],request.form['end'])  
return query

用户将在HTML输入界面中进行选择:

<form action="{{ url_for('query') }}" method="post">
            <p>Query Database</p>
            <select name ="Data Points " id="Data Points" widht="300px" required/>
            <option value="Record_Start_Time ">Record Start Time</option>
            <option value="Glycol_Supply ">Glycol Supply</option>
            <option value=" Glycol_Return ">Glycol Return</option>
            </select>

            <p>Start Time <input type = "text" name = "start" /></p>
            <p>End Time <input type = "text" name = "end" /></p>
            <input type="submit">
</form>

1 个答案:

答案 0 :(得分:0)

这应该有效:

def queryBuilder():
    query = """SELECT Record_Start_Time, {}
    FROM Furnace_Output.DataTable
    WHERE Record_Start_Time BETWEEN TIMESTAMP("{}") 
    AND TIMESTAMP("{}")
    ORDER BY Record_Start_Time
    LIMIT 100""".format(request.form['Data Points'] , 
  request.form['start'],request.form['end'])  
return query