I have a table that contains duplicated records.
I used following code to put the data into a QTableView:
QSqlTableModel *dataModel = new QSqlTableModel();
dataModel->setTable("table_name");
dataModel->select();
now I wanna to query duplicated records. I use group by and having for do that in sql but I haven`t any idea to how do this in qt.
Finally I found the solution, I used "setFilter" method as bellow:
dataModel = new QSqlTableModel();
dataModel->setTable("CUSTOMER");
QString filter_txt = "id in (select id FROM CUSTOMER GROUP BY id HAVING count(*) >1)" ;
dataModel->setFilter(filter);
dataModel->select();
Related
What I want to do is take the input from a text field (i.e cust_name), match and fetch the record from database with column cust_name.
Database has table cust and column cust_name, address. I want to find a way to display the data in tablview of QSqlTableModel.
QSqlTableModel *model = new QSqlTableModel;
model->setTable("cust");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
ui->tableView->setModel(model);
ui->tableView->setColumnHidden(0,1);
ui->tableView->setColumnHidden(5,1);
ui->list->setModel(model);
ui->list->setModelColumn(1);
model->select();
This successfully displays the whole table named cust. I want to be able to change it into displaying the single record matching the search term.
Use function index() to access custom row/column.
QSqlTableModel *model = new QSqlTableModel;
int value = model->index(row,column).data().toInt();
In your situation like this:
int value = model->index((model->rowCount()-1),column).data().toInt();
or custom sql query :
SELECT * FROM tablename ORDER BY id DESC LIMIT 1;
When i will view the data from my mysql database " mydb " , from the table "testtable", it cant open the database.
this->model = new QSqlQueryModel();
meineView->setModel(model);
must i write it so :
model->setQuery("SELECT id, Nachname, Vorname, Ort FROM mydb");
Or so ? :
model->setQuery("SELECT `testtable`.`id`,`testtable`.`Nachname`,`testtable`.`Vorname`,`testtable`.`Ort`FROM `mydb`.`testtable`;");
what do i wrong ? when i delete this , my program works ( without views the data )
and when i can open it, how i put the data in my table ? ?
You need to call overriden method with your Database name. Because the database you are trying to open is not default database.
Try this :
model->setQuery("SELECT `testtable`.`id`,`testtable`.`Nachname`,`testtable`.`Vorname`,`testtable`.`Ort
`FROM `mydb`.`testtable`;","mydb");
First of all you need to connect to database using QSqlDatabase class. Then you can connect QSqlQueryModel to sql connection using proper sql query.
your first query is wrong because mydb is the database and this query needs table name:
SELECT id, Nachname, Vorname, Ort FROM testtable.
Second query is the option to choose when the query references multiple tables.
SELECT testable.id, testable.name, othertable.data FROM testable, othertable, WHERE testable.someRow = othertable.someRow
Hi all i have the following code:
tx = session.beginTransaction();
Query query = session.createQuery("UPDATE com.nisid.entities.Payment set amount=:amount,paymentMethod=:method,paymentExpire=:expireDate"
+ "Where paymentId=:payid,actionId=:actionid");
query.setParameter("amount", amount);
query.setParameter("method", method);
query.setParameter("expireDate", expireDate);
query.setParameter("payid", projectId);
query.setParameter("actionid", actionId);
int resutl=query.executeUpdate();
Trying to do an update using HQL but i am getting error: galArgumentException: node to traverse cannot be null!
my table in the DB is called Payment and it has A COMPOSITE KEY ( projectId,actionId)
Could you please help me further???
The concept is that i have a JSP page which retrieves and displayes the results from DB retrieving info from Project Table, Payment Table and Action Table. Project has many to many relationship with Action and i am using Payment Table as the intermetiary table which holds the 2 FK of the other table.
You missed space before where, and replace , to and after where.
Query query = session.createQuery("UPDATE com.nisid.entities.Payment set amount=:amount,paymentMethod=:method,paymentExpire=:expireDate"
+ " Where paymentId=:payid and actionId=:actionid");
Im working on a project where im trying to build a QTreeWidget that has multiple QTreeWidgetItems
and once i click on a particular item it connects to a database and shows a query result in a tableview model, until now every thing works fine.
the problem is that i want each item to output different resut depending on some criteria on the same table where this criteria is only changing the value of an attribute, and this value is the same of the item name. for example item named 122 and the table has atribute called no. when we click on item 122 the result of this query must be shown (select * from table where no=122)
any help :)
I am assuming you have a QTableView backed by a QSqlQueryModel or QSqlTableModel.
You can connect to the signal QTreeWidget::itemSelectionChanged() and then in the slot you get the current item with selectedItems()[0]. Then you create your query:
QSqlQuery query;
query.prepare("select * from table where no=:no");
query.bindValue(":no", number);
query.exec();
Finally you can use setQuery (const QSqlQuery & query) on the model. This should update your view.
I hope I understood correctly what you want to achieve.
After setting an INSERT or DELETE query on QSqlQueryModel, my QTableView becomes screwed up. For example I hid the ID column by calling view->hideColumn(ID); but after an INSERT or DELETE the ID column becomes visible.
How can I automatically reset my view to the previous settings in these cases?
I guess the problem is in QSqlQueryModel::setQuery you're eventually calling every time content gets reloaded and rows inserts\deletes. Looking at the setQuery implementation I would suggest: depending on the query your model can be reset including columns settings change which should trigger view columns update.
As Qt documentation suggests:
The QSqlQueryModel class provides a
read-only data model for SQL result
sets.
so I would use direct QSqlQuery calls for the data updates and then would reload the model with the same query. Or consider switching to QSQLTableModel, which is quite handy for single table content manipulation and supports inserts updates and deletes. See if an example below would work for you:
set up database, view and model:
QSqlTableModel *_model;
QTableView *_view;
...
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
db.open() ;
QSqlQuery query;
query.prepare("CREATE TABLE IF NOT EXISTS person (id INTEGER UNIQUE PRIMARY KEY, name VARCHAR(30))");
query.exec();
query.prepare("INSERT INTO person (name) VALUES ('test1')");
query.exec();
query.prepare("INSERT INTO person (name) VALUES ('test2')");
query.exec();
_model = new QSqlTableModel(this, db);
_model->setTable("person");
_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
_model->select();
_model->setHeaderData(1, Qt::Horizontal, tr("name"));
_model->setSort(1, Qt::AscendingOrder);
_view = new QTableView(this);
_view->setModel(_model);
_view->hideColumn(0);
add new row:
QSqlRecord record;
_model->insertRecord(-1, record);
delete selected row(s):
QModelIndexList selected = _view->selectionModel()->selectedIndexes();
for (int i = 0; i < selected.size(); ++i)
_model->removeRows(selected.at(i).row(), 1);
submit changes:
_model->submitAll();
hope this helps, regards