Execute sqlite3 operation in python before displaying data in a QTableView

1

Good evening community.

I have a query in sqlite3 that I made in the Sales table which is the following:

import sqlite3


fecha ="12/08/2018"


conexion = sqlite3.connect("Users.db")
cursor = conexion.cursor()
cursor.execute("SELECT * FROM Ventas WHERE Fecha= ?",(fecha,))
for i in cursor:
    print(i)

and I have the function shown in the Sales table:

dbs = QtSql.QSqlDatabase.addDatabase('QSQLITE')
dbs.setDatabaseName('Users.db')
dbs.open()

model = QtSql.QSqlTableModel()
model.setTable('Ventas')
model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
model.select()
tabla.setModel(model)

I want to know how I could show in this case only the data of the query that I made in the above code, which would show the data only of the established date. And not that it shows all the data of the Table

    
asked by Mystic_Force 24.08.2018 в 05:09
source

1 answer

1

If you want to filter, you should use the setFilter()

method
fecha ="12/08/2018"
model = QtSql.QSqlTableModel()
model.setTable('Ventas')
model.setFilter("Fecha= \"{}\"".format(fecha))
model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
model.select()
tabla.setModel(model)
    
answered by 24.08.2018 / 05:38
source