Results 1 to 8 of 8

Thread: i can't update database,

  1. #1
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Cool i can't update database,

    i can't update ,insert or edit the database ,someone help me pleaze

    Qt Code:
    1. void WellDataManage::loadWellData(QString projectAddress)
    2. {
    3. QString address=projectAddress;
    4. address+="//";
    5. address+="Data";
    6. address+="//";
    7. address+="SystemData";
    8. address+=".db3";
    9.  
    10. if (!QFile::exists(address))
    11. {
    12. QMessageBox::information(this,tr("Information"),tr("There is something wrong with the wells databases"),QMessageBox::Ok,QMessageBox::Ok);
    13. }
    14.  
    15. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    16. db.setDatabaseName(address);
    17. db.open();
    18.  
    19.  
    20. welldatamodel = new QSqlRelationalTableModel;
    21. welldatamodel->setTable("wells");
    22. welldatamodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    23. welldatamodel->setSort(0,Qt::AscendingOrder);
    24.  
    25. //set Comobox model
    26.  
    27.  
    28. wellTypeComoBox->setModel(welldatamodel);
    29. wellTypeComoBox->setModelColumn(welldatamodel->fieldIndex("welltype"));
    30.  
    31. //set header name
    32. welldatamodel->setHeaderData(0,Qt::Horizontal,tr("Name"));
    33. welldatamodel->setHeaderData(1,Qt::Horizontal,tr("Group"));
    34. welldatamodel->setHeaderData(2,Qt::Horizontal,tr("X"));
    35. welldatamodel->setHeaderData(3,Qt::Horizontal,tr("Y"));
    36. welldatamodel->setHeaderData(4,Qt::Horizontal,tr("Type"));
    37. welldatamodel->setHeaderData(5,Qt::Horizontal,tr("Production"));
    38. welldatamodel->setHeaderData(6,Qt::Horizontal,tr("Depth"));
    39. welldatamodel->setHeaderData(7,Qt::Horizontal,tr("BeginDepth"));
    40. welldatamodel->setHeaderData(8,Qt::Horizontal,tr("EndDepth"));
    41. welldatamodel->setHeaderData(9,Qt::Horizontal,tr("Altitude"));
    42.  
    43.  
    44. welldatamodel->select();
    45. welldatamodel->removeColumn(0);
    46.  
    47. wellTableView->setModel(welldatamodel);
    48. wellTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    49. wellTableView->resizeRowsToContents();
    50.  
    51. // wellTableView->enabledChange(false);
    52. // wellTableView->resizeColumnsToContents();
    53. // wellTableView->horizontalHeader()->setStretchLastSection(true);
    54.  
    55.  
    56. welldatamapper->setModel(welldatamodel);
    57. //welldatamapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    58. welldatamapper->setItemDelegate(new QSqlRelationalDelegate(this));
    59.  
    60. welldatamapper->addMapping(wellNameEdit,0);
    61. welldatamapper->addMapping(wellGroupEdit,1);
    62. welldatamapper->addMapping(wellXPositionEdit,2);
    63. welldatamapper->addMapping(wellYPositionEdit,3);
    64.  
    65. welldatamapper->addMapping(wellProductionEdit,5);
    66. /*welldatamapper->addMapping(wellDepthEdit,6);*/
    67. welldatamapper->addMapping(wellTypeComoBox,6);
    68. welldatamapper->addMapping(wellBeginDepthEdit,7);
    69. welldatamapper->addMapping(wellEndDepthEdit,8);
    70. welldatamapper->addMapping(wellAltitudeEdit,9);
    71.  
    72. //create connect of the buttons
    73. welldatamapper->toFirst();//初始化时选择选择第一行
    74. connect(firstAction,SIGNAL(triggered()),welldatamapper,SLOT(toFirst()));
    75. connect(lastAction,SIGNAL(triggered()),welldatamapper,SLOT(toLast()));
    76. connect(previousAction,SIGNAL(triggered()),welldatamapper,SLOT(toPrevious()));
    77. connect(nextAction,SIGNAL(triggered()),welldatamapper,SLOT(toNext()));
    78. connect(newAction,SIGNAL(triggered()),this,SLOT(addWellRecord()));
    79. connect(deleteAction,SIGNAL(triggered()),this,SLOT(deleteWellRecord()));
    80. connect(updateAction,SIGNAL(triggered()),this,SLOT(updateWellRecord()));
    81.  
    82.  
    83. connect(wellTableView->selectionModel(),SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),welldatamapper,SLOT(setCurrentModelIndex(QModelIndex) ));
    84. connect(welldatamapper,SIGNAL(currentIndexChanged(int)),wellTableView,SLOT(selectRow (int)));
    85. }
    86.  
    87. void WellDataManage::addWellRecord()
    88. {
    89.  
    90. int row=welldatamapper->currentIndex();
    91. welldatamapper->submit();
    92. welldatamodel->insertRow(row);
    93. welldatamapper->setCurrentIndex(row);
    94.  
    95.  
    96. wellNameEdit->clear();
    97. wellGroupEdit->clear();
    98. wellXPositionEdit->clear();
    99. wellYPositionEdit->clear();
    100. wellBeginDepthEdit->clear();
    101. wellEndDepthEdit->clear();
    102. wellProductionEdit->clear();
    103. wellTypeComoBox->clear();
    104. wellNameEdit->setFocus();
    105. }
    106.  
    107.  
    108.  
    109.  
    110. void WellDataManage::deleteWellRecord()
    111. {
    112.  
    113. int row=welldatamapper->currentIndex();
    114. welldatamodel->removeRow(row);
    115. welldatamapper->submit();
    116. welldatamodel->submit();
    117. //welldatamapper->setCurrentIndex(QMIN(row,welldatamodel->rowCount()-1));
    118.  
    119.  
    120. }
    121.  
    122. void WellDataManage::updateWellRecord()
    123. {
    124. welldatamapper->submit();
    125.  
    126. //welldatamodel->submitAll();
    127.  
    128. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: i can't update database,

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(address);
    db.open();


    just lookin in sql examples... dont you need also

    db.setHostName("bigblue");
    db.setUserName("acarlson");
    db.setPassword("1uTbSbAs");
    bool ok = db.open();


    ??????

  3. #3
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: i can't update database,

    i can read the data from the database ,i just can't update it

  4. #4
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: i can't update database,

    If you set the edit strategy to QSqlTableModel::OnManualSubmit,

    Qt Code:
    1. welldatamodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    To copy to clipboard, switch view to plain text mode 

    you must use QSqlTableModel::submitAll() or QSqlTableModel::revertAll().

  5. #5
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: i can't update database,

    i also use the
    Qt Code:
    1. QSqlTableModel::submitAll()
    To copy to clipboard, switch view to plain text mode 
    ,it still can't work ,i have tried.

  6. #6
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: i can't update database,

    can anyone help me ?

  7. #7
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: i can't update database,

    How do you create the database? When i use sqlite3 application to create database and tables, then open it with Qt, there are problems when updating the data, the data won't update when calling model->submitAll().

    Then i try to create database and tables from Qt using QSqlQuery, inserting some records, updating some data. And it works fine.

  8. #8
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: i can't update database,

    i used the blow code to create the databases
    Qt Code:
    1. QString address=projectAddress;
    2. address+="//";
    3. address+=projectName;
    4. address+="//";
    5. address+="Data";
    6. address+="//";
    7. address+="SystemData";
    8. address+=".db3";
    9.  
    10. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    11. db.setDatabaseName(address);
    12. db.open();
    13.  
    14. QSqlQuery query;
    15. query.exec("CREATE TABLE [wells] ("
    16. "[id] INTEGER PRIMARY KEY,"
    17. "[wellnames] NVARCHAR, "
    18. "[wellgroup] NVARCHAR,"
    19. "[XPosition] DOUBLE, "
    20. "[YPosition] DOUBLE,"
    21. "[welltype] CHAR, "
    22. "[production(m3)] DOUBLE,"
    23. "[depth] DOUBLE, "
    24. "[beginDetpth] DOUBLE, "
    25. "[endDepth] CHAR,"
    26. "[altitude] DOUBLE);");
    27. db.close();
    28. return true;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 9
    Last Post: 20th May 2010, 10:55
  2. how to use update()?
    By zgulser in forum Qt Programming
    Replies: 0
    Last Post: 6th February 2009, 10:45
  3. Is Update() the right way?
    By td in forum Newbie
    Replies: 1
    Last Post: 5th December 2008, 15:54
  4. Replies: 1
    Last Post: 7th June 2008, 19:29
  5. Update a row
    By dragon in forum Newbie
    Replies: 9
    Last Post: 17th January 2006, 18:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.