in Education by
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)

1 Answer

0 votes
by
@eyllanesc Thank you for leading me towards using setFilter. I've created something that seems to work. I converted the query results to a string. I then used that string to construct a SQL argument to be used with setFilter. Elegant? Pythonic? I'm not sure. 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") self.query = QSqlQuery() self.query.exec("SELECT customer_id FROM customers WHERE customer_id IN (2,4)") # Convert query results to string. self.ls = list() while self.query.next(): self.ls.append(self.query.value(0)) self.ls_string = ','.join(map(str, self.ls)) # Create setFiler argument. self.filter_criteria = "customer_id IN " + "(" + self.ls_string + ")" self.model.setFilter(self.filter_criteria) self.model.select() 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_())

Related questions

0 votes
    What's the standard way of updating a QTableView when its database (MySQL) was changed (outside of the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I have 2 entity: /** * @ORM\Entity * @ORM\Table(name="users") */ class User { /** * ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have this table where columns correspond to names: i.e. 11 = first, 12 = second, 13 = third.. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 15, 2022 in Education by JackTerrance
0 votes
    I have an ASP.NET MVC website. In my backend I have a table called People with the following ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I'm trying to write an Azure table Storage Query result to a .csv file and store it locally on ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I need to take the users input from the select box for Allergen1 once an option is selected and the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    In Ms Access Which query allow to create a table or alter data or database? Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    A_____ is a query that retrieves rows from more than one table or view: (a) Start (b) End (c ... Join Operations topic in chapter Query Processing Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    A function, together with an environment, makes up what is called a ______ closure. (a) formal (b) ... Debugging of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    _______ grammar makes a clear distinction between your data and what gets displayed on the screen or page. (a) ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    1. An ___________ is a system software. It manages the resources of a computer system and makes a computer easy to ... (d) Multi-user Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    Which of the following makes use of pandas and returns data in a series or dataFrame? (a) pandaSDMX (b ... questions and answers pdf, Data Science interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    I asked a question yesterday and none of the answers are working so I decided to start a fresh one ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    _________ is new package that makes it easy to tidy your data. (a) tidy (b) tidyr (c) tidyneat (d ... Data Analysis of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    ________ makes it incredibly easy to fit time series models like ARIMA, ARMA, AR, Exponential Smoothing, etc. (a ... of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
...