Consult mysql with Flask

0

I am trying to make a query to MySQL that should bring data in a set date range and it does not work, if I could make other queries but it does not work, the data of the date range are two input type="date" I show them the code:

<form action="/" method="POST">
    <div class="row clearfix">

        <div class="col-md-3">
            <div class="form-group-horizontal">
                <label class="control-label">Patente</label>
                <input type="text" class="form-control" name="Numer">
            </div>

        </div>

        <div class="col-md-3">
            <h2></h2>
            <button type="submit" class="btn bg-blue btn-lg-l-15 waves-effect">
                <i class="material-icons">Buscar</i>
            </button>
        </div>


        <!--/span-->
        <div class="col-md-6">
            <div class="form-group-horizontal">
                <label class="control-label">Area</label>
                <input type="text" class="form-control " placeholder="Area Responsable" name="area" id="area">
                </small>
            </div>
        </div>
        <!--/span-->

        <!--/span-->
        <div class="col-md-6">
            <div class="form-horizontal">
                <label class="control-label">Interno</label>
                <input type="text" class="form-control form-control-danger" placeholder="Interno" name="interno" id="interno">
                </small>
            </div>
        </div>
        <!--/span-->

        <!--/span-->
        <div class="col-md-6">
            <div class="form-horizontal">
                <label class="control-label">Marca</label>
                <input type="text" id="Marca" class="form-control form-control-danger" placeholder="Marca movil" name="marca" id="marca">
                </small>
            </div>
        </div>
        <!--/span-->


        <div class="col-md-6">
            <div class="form horizontal">
                <label class="control-label">Fecha desde:</label>
                <input name="Fechadesde" type="date" class="form-control" placeholder="dd/mm/yyyy" id="Date1" value="yyyy-MM-dd">
                </small>
            </div>


        </div>

        <div class="col-md-6">
            <div class="form horizontal">
                <label class="control-label">Fecha hasta:</label>

                <input name="Fechahasta " type="date" class="form-control" placeholder="dd/mm/yyyy" value="yyyy-MM-dd" id="Date2">
                </small>
            </div>
        </div>
    </div>
</form>

And this is the driver code:

@app.route('/consultaxmovil.html', methods=['GET','POST'])
def consultaxmovil():
    #patente = request.form.get("Numer",False)
    Date1 = str(request.form.get("Fechadesde",False)).format("yyyy-MM-dd")
    Date2 = str(request.form.get("Fechahasta",False)).format("yyyy-MM-dd")
    #Date2.date().toString("yyyy-MM-dd")

    cur1 = conn.cursor()
    query ="SELECT  * from tablaunion1 where Fecha_Carga between '"+ Date1 +"%' and '"+ Date2 +"%'"

    cur1.execute(query)

    var = cur1.fetchall()
    return render_template('consultaxmovil.html', var=var)
    cur1.close()
    
asked by Julio 22.06.2018 в 14:09
source

1 answer

0

The correct thing is that you pass the parameters to execute , this way you avoid formatting as you are doing:

@app.route('/consultaxmovil.html', methods=['GET','POST'])
def consultaxmovil():
    Date1 = str(request.form.get("Fechadesde",False)).format("yyyy-MM-dd")
    Date2 = str(request.form.get("Fechahasta",False)).format("yyyy-MM-dd")

    cur1 = conn.cursor()
    query = '''
        SELECT * 
        FROM tablaunion1 
        WHERE Fecha_Carga BETWEEN %s AND %s
    '''
    params = [Date1, Date2]
    # Ejecuta este query usando estos parametros
    cur1.execute(query, params)

    var = cur1.fetchall()
    cur1.close()

    return render_template('consultaxmovil.html', var=var)

As mentioned by Pablo, the cur1.close() has to go before the return . Also note how I am now passing the parameters to query using a list, these parameters are going to be replaced by every %s that has been used in the query . This form is not only more readable, it is also safer.

    
answered by 23.06.2018 в 00:25