[Solved]Add new row at last in SQLite
-
wrote on 27 Apr 2015, 09:44 last edited by Awadhesh Maurya
Hi,
I want add new row after last row in QTableView, my code is:void AbstractItemModel :: UpdateData(QModelIndex start, QModelIndex end)
{
bool ret;
int columns = columnCount();
int row = rowCount()+1;
beginInsertRows(end, row, row);ret = setData(index(row, 0), gID, Qt::EditRole); fprintf(stdout,"\rsetData gID %d\n", ret); ret = setData(index(row, 1), gTitle); fprintf(stdout,"\rsetData gTitle %d\n", ret); setData(index(row, 2), gFName); setData(index(row, 3), gMName); setData(index(row, 4), gLName); setData(index(row, 7), gDob); setData(index(row, 5), gUsrTp); setData(index(row, 6), gGend); setData(index(row, 8), gGroup); setData(index(row, 9), gGrade); setData(index(row, 10), gEmpTp); setData(index(row, 11), gShift); endInsertRows(); emit dataChanged(end, end);
}
In this way there is only blank row added in table, but actual rowCount() is not changed. setData always return 0, i.e. its not setting data.
I think I am missing something, please let me know where I am wrong.Thanks.
-
Hi,
Maybe a silly question, but since you are using SQLite, why don't use use a QSqlTableModel ?
-
wrote on 27 Apr 2015, 10:04 last edited by
@SGaist said:
QSqlTableModel
hi,
I thought Qt using SQLite so I have written these. I am using QSqlTableModel. I don't have enough knowledge about SQL. After searching in google I found that I should use QAbstractItemModel to modify the table. I setaim = (AbstractItemModel*)model;
where is aim - pointer AbstractItemModel derived class from QAbstractItemModel;
model is QSqlTableModel pointer type.I want to add row in table, so I am using QAbstractItemModel. What are the issue why my program is not working.
-
You absolutely don't need all of that, QSqlTableModel provides all what you need.
One way to do it:
QSqlRecord record = model->record(); record.setValue(0, gID); record.setValue(1, gTitle); record.setValue(2, gFName); //etc. model->insertRecord(-1, record);
You should also take a look at the QDataWidgetMapper class
-
wrote on 27 Apr 2015, 10:36 last edited by Awadhesh Maurya
hi,
thanks for your reply.
I have tried this also but in table row number is star symbol and after any activity like sorting, this row will be disappeared from table, i.e. I am getting new data in new row nunbered as '*'.I am using QtableView with setSortingEnabled.
Please can you suggest me to solve it.
-
@Awadhesh-Maurya said:
I have tried this also but in table row number is star symbol and after any activity like sorting, this row will be disappeared from table, i.e. I am getting new data in new row nunbered as '*'.
I don't understand the "*" can you show maybe a picture of what you are getting ?
-
wrote on 28 Apr 2015, 04:31 last edited by Awadhesh Maurya
Previous row like:
15 96765432 Mr. Awadhesh skumar
New inserted row:
* 98765432 Mr. XYZ ABC
Before insert new at end there were 15 rows numbered as 1, 2,3 ....15. Newly added row should be 16 but here it is *. After sorting column this row will be removed automatically.
I think now you can understand what am I trying to say, because there is no option to attach file, so I am not able send image of table.
-
Are you using the OnManualSubmit strategy ? If so did you submit the data ?
-
wrote on 29 Apr 2015, 04:39 last edited by
Thank you for your help.
Now it is solved. I am using the OnManualSubmit strategy. After
model->insertRecord(-1, record);I do submit
model->submitAll();Now its working properly.
-
Good !
Since you have it running properly, please mark the thread as solved so other forum users may know a solution has been found :)
1/10