bpranoto

problem_inserting_record_into_QSqlTableModel

Sep 1st, 2018
99
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <QCoreApplication>
  2. #include <QSqlDatabase>
  3. #include <QSqlTableModel>
  4. #include <QSqlQuery>
  5. #include <QSqlError>
  6. #include <QSqlTableModel>
  7. #include <QSqlRecord>
  8. #include <QModelIndexList>
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     // DB Connection
  15.     QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
  16.     db.setHostName("127.0.0.1");
  17.     db.setDatabaseName("your_database");
  18.     db.setUserName("your_user_name");
  19.     db.setPassword("your_password");
  20.     if ( ! db.open())
  21.     {
  22.         cout << "Cannot open database" << endl;
  23.     }
  24.  
  25.     // drop table
  26.     cout << "drop table datatest..." << endl;
  27.     QSqlQuery q = QSqlQuery(db);
  28.     QString sql;
  29.     sql = "drop table datatest;";
  30.     q.exec(sql);
  31.  
  32.     // Create table
  33.     cout << "create table datatest..." << endl;
  34.     sql=
  35.     "CREATE TABLE datatest"
  36.     "("
  37.     " id serial"
  38.     " ,kode character(1)"
  39.     " ,nama character(10)"
  40.     "  ,CONSTRAINT datatest_pkey PRIMARY KEY (id)"
  41.     ")"
  42.     " WITH ( OIDS = TRUE);";
  43.  
  44.     if ( ! q.exec( sql ))
  45.     {
  46.         cout << "error create table" << endl;
  47.         cout << q.lastError().databaseText().toStdString() << endl;
  48.         return 0;
  49.     };
  50.  
  51.  
  52.     // Populate 2 records
  53.     cout << "Populate table..." << endl;
  54.     sql = "insert into datatest (kode,nama) values (:kode,:nama)";
  55.     q.prepare(sql);
  56.  
  57.     q.bindValue(":kode","A");q.bindValue(":nama","ALBERT"); q.exec();
  58.     q.bindValue(":kode","Z");q.bindValue(":nama","ZORRO"); q.exec();
  59.  
  60.     // Create model
  61.     cout << "Create QSqlTableModel..." << endl;
  62.     QSqlTableModel model;
  63.     model.setTable("datatest");
  64.     model.select();
  65.  
  66.     // insert record
  67.     cout << "Inserting a record to the model...";
  68.     QSqlRecord record = model.record();
  69.     record.setGenerated("id",false);
  70.     record.setValue("kode","P");
  71.     record.setValue("nama","PATRICK");
  72.     if ( ! model.insertRecord(-1,record))
  73.     {
  74.         cout << "Error inserting record to the model" << endl;
  75.         cout << model.lastError().databaseText().toStdString() <<endl;
  76.         return 0;
  77.     };
  78.  
  79.  
  80.     // get the new record id
  81.     int newRecId = model.query().lastInsertId().toInt();
  82.     cout << ", field ID of the new record is " << newRecId << endl;
  83.  
  84.     // find the new inserted record
  85.     QModelIndexList listNewRec = model.match(model.index(0,0),Qt::DisplayRole,newRecId);
  86.     if ( listNewRec.size() == 1 )
  87.     {
  88.         cout << "Found the new record..." << endl;
  89.     }
  90.     else
  91.     {
  92.         cout << "Can not locate the new inserted record" << endl;
  93.     }
  94.  
  95.     // reselect the model
  96.     cout << "doing model.select()" << endl;
  97.     model.select();
  98.  
  99.     // find the new inserted record
  100.     listNewRec = model.match(model.index(0,0),Qt::DisplayRole,newRecId);
  101.     if ( listNewRec.size() == 1 )
  102.     {
  103.         cout << "Found the new record only after reselect the model..." << endl;
  104.     }
  105.     else
  106.     {
  107.         cout << "Can not locate the new inserted record" << endl;
  108.     }
  109.  
  110.  
  111.     return 0;
  112.  
  113. }
RAW Paste Data