I have two tables like:
Person (Id, Name, JobID, Age)
Jobs (Id, Company, Salary)
I'm using a SqlRelationalTableModel and it's working with the following QSqlRelation:
model-> setTable("Person")
model-> setRelation(2,QSqlRelation("Jobs", "Id", "Company"))
My problem is that now I want to show only the jobs with salaries over 50000 and I don't know how to filter Jobs table to do it since setFilter only affects Person table.
You can use QSqlQueryModel
QSqlQueryModel* model = new QSqlQueryModel();
model->setQuery("select * from Person left join Jobs on Person.JobID=Jobs.Id where Salary>50000");
Related
Hi all how I have a product, order and product order(bridge entity) table.
Im building a cart based order system c++ program where when user make a new order ,it will have product id listed on a list and every product will have its own orderID. Im kinda confused on the sql side on how to insert data that have brige entity and join them together
The table have the following attrbs
Table product
ProductID
Table Order1
OrderID,
Totalprice,
description,
agentid
Table product_order(bridge entity)
ProductID,
OrderID,
quantity
I'm planning to insert data only 1 time simultaneously inserting into the other table as well . Its possible to use join + insert?
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();
I have a table 'Person' with columns as 'Person_id as primary key','DOB' and 'place' as follows:
'Person'
Person_id |Name|DOB | place
Another table is "employee" where emp_id is primary key as follows:
'employee'
Person_id |emp_id|dateofjoin
And one more table "Details":
'Details'
emp_id|competency|rating
Now what i want is once i add the 'Person' table details the rest of the two tables as'employe' and 'Details' to get updated also with respect to the new Person added in the Person table. So, how can i have this using sql query? Also i want to clear that i am not very much familiar with database.
I think your after something like this ( for SQL Server ):
Create Procedure dbo.CreateMyEmployee ( #empName varchar(50),
#dob datetime,
#doj datetime,
#place as varchar(100),
#competency varchar(100),
#rating int)
As
Begin
Declare #empId int
Begin Transaction
Begin Try
Insert into Person (Name, DOB, Place)
Values ( #empName, #dob, #place)
Insert into employe (Name, dateofJoin) -- Assuming emp_id is identity columen
Values ( #empName, #doj)
Select #empId = SCOPE_IDENTITY()
Insert Into Details(emp_id, competency, rating)
Values (#empId, #competency, #rating)
Commit transaction
End Try
Begin Catch
Rollback Transaction
SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_MESSAGE() AS ErrorMessage
End Catch
End
How can I list data from two table in tableView?
Database (Example) Sqlite:
tb_sales
tb_product
tb_value
tb_customer_id (Customer Id "tb_customer")
tb_customer
tb_customer_id (id primary key)
tb_name
tb_state
With QSqlRelationalTableModel fetch only the client's name. Also need the state.
model= new QSqlRelationalTableModel(this);
model->setTable("tb_sales");
model->setRelation(2, QSqlRelation("tb_customer", "tb_customer_id", "tb_name"));
model->select();
ui->tableView->setModel(model);
QSqlRelationalTableModel only allows you to include one column from the secondary table. You can use QSqlQueryModel to have a query in which you join the two tables :
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT tb_sales.* , tb_customer.tb_name, tb_customer.tb_state FROM tb_sales LEFT JOIN tb_customer ON tb_sales.tb_customer_id = tb_customer.tb_customer_id");
ui->tableView->setModel(model);
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