I need to perform a query to limit the rows displayed in a table. I chose QTableView/QSqlTableModel for the read-write functionality. Unfortunately, my query makes the table uneditable.
Edit: This is not my real program. I will be using a few tables and a relational division query to determine the rows to be displayed. I do need to use a query.
How do I execute a query and keep the read-write functionality?
import sys
from PyQt5.QtWidgets import QApplication, QTableView
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel, QSqlQuery
def createDb():
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("temp.db")
if not db.open():
print("Cannot establish a database connection.")
return False
query = QSqlQuery()
query.exec_("DROP TABLE IF EXISTS customers")
query.exec_("CREATE TABLE customers (customer_id INTEGER PRIMARY KEY NOT NULL, "
"customer CHAR NOT NULL)")
query.exec_("INSERT INTO customers (customer) VALUES ('Customer 1')")
query.exec_("INSERT INTO customers (customer) VALUES ('Customer 2')")
query.exec_("INSERT INTO customers (customer) VALUES ('Customer 3')")
query.exec_("INSERT INTO customers (customer) VALUES ('Customer 4')")
return True
class MainForm(QTableView):
def __init__(self):
super().__init__()
self.model = QSqlTableModel(self)
self.model.setTable("customers")
# This query results in a non-editable table.
self.query = QSqlQuery("SELECT customer FROM customers WHERE customer_id = 2")
self.model.setQuery(self.query)
self.view = QTableView(self)
self.view.setModel(self.model)
if __name__ == "__main__":
app = QApplication(sys.argv)
createDb()
w = MainForm()
w.show()
sys.exit(app.exec_())
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)