query database with python with date range from html form

Viewed 19

I have this html code to enter the starting and ending date ranges

  <input id="fechaInicio" type="text" 
  <input id="fechaFinal" type="text" 
  <a onclick = "listadoConsulta()" href="#" class="btn" 

the javascript code

  async function listadoConsulta() {
  document.getElementById('fechaInicio').value = date1
  document.getElementById('fechaFinal').value = date2

  var url = URLSERVIDOR + 'listado/consulta'
  var respuesta = await fetch(url, {
    "method": 'GET',
    "headers":{
        "Content-Type": 'application/json'
    }
    }) 
    listado = await respuesta.json();
    var html = ''
    for (registro of listado) {
    
    var row = `<tr>
    <td>${registro.folio}</td>
    <td>${registro.lote}</td>
    <td>${registro.tabla}</td>
    <td>${registro.cajas}</td>
    <td>${registro.peso_neto}</td>
    </tr>`
        html = html + row;
     }
    document.querySelector('#tablaFechas> tbody').outerHTML = html

and database query in python

    @app.route('/api/listado/consulta')
    @cross_origin()
    def listadoConsulta():
    ejecutarListado = mysql.connection.cursor()
    #date1 = "20220920"
    #date2 = "20220921"
    ejecutarListado.execute('SELECT id, folio, lote, tabla, cajas, peso_neto FROM 
    `registrocorte` WHERE `fecha` BETWEEN ' + date1 +  " AND " + date2 + ';')
    datos = ejecutarListado.fetchall()
    resultadoLista = []
    for fila in datos:
    contenido = {'id': fila[0],
                 'folio': fila[1],
                 'lote': fila[2],
                 'tabla': fila[3],
                 'cajas': fila[4],
                 'peso_neto': fila[5]
                 }
    resultadoLista.append(contenido)
    return jsonify(resultadoLista)

It works well if I put the dates directly in the query in python, the question is how can I make it do the query using the dates that I give to the html?

0 Answers
Related