Page 1 of 2 12 LastLast
Results 1 to 20 of 34

Thread: EditStrategy, how to save data

  1. #1
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default EditStrategy, how to save data

    I'm beginning very slowly to catch some grip. Sometimes I even code more than search for things.

    I can't seem to save changes in a QSqlTableModel, not if I set it to OnFieldChange and not if I set it to OnManualSubmit.
    What am I overlooking?
    Thanks

    code:

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    2. db.setHostName("192.168.1.10");
    3. db.setDatabaseName("iris");
    4. db.setUserName("jean");
    5. db.setPassword("");
    6. if (!db.open())
    7. setWindowTitle(db.lastError().text());
    8.  
    9. model = new QSqlTableModel;
    10. model->setTable("pix");
    11. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    12. model->select();
    13. table->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 5th January 2008 at 22:57. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    What do submitAll() and lastError() return?

  3. #3
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Thanks jacek,

    it's empty:

    Qt Code:
    1. model->submitAll();
    2. QSqlDatabase db = QSqlDatabase::database();
    3. qDebug() << "commit" << db.lastError().text();
    To copy to clipboard, switch view to plain text mode 

    Message:
    commit " "
    Last edited by jpn; 5th January 2008 at 22:58. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Is the output the same after you change anything in the table?

  5. #5
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Ow I do this in an onclose event, the docs said it was nessecary:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database();
    2. db.close();
    3. db.removeDatabase("iris");
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 5th January 2008 at 22:59. Reason: missing [code] tags

  6. #6
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Yes,

    First I change a cell, clik on another, changes made are still visible, then click on the menuentry for the submit call.
    I noticed that when choosing OnFieldChange, if I edit a cell, then click on another, that editing is lost, so it's just not writing to the database.


    Quote Originally Posted by jacek View Post
    Is the output the same after you change anything in the table?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Quote Originally Posted by JeanC View Post
    the docs said it was nessecary:
    Could you post a link? Qt should close all database connections on exit.

    Quote Originally Posted by JeanC View Post
    QSqlDatabase db = QSqlDatabase::database();
    db.close();
    db.removeDatabase("iris");
    After you do that, you won't be able to change anything in the database.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Quote Originally Posted by JeanC View Post
    First I change a cell, clik on another, changes made are still visible, then click on the menuentry for the submit call.
    What happens if you press the return key before clicking anywhere?

  9. #9
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    hm, it does not say it's nessecary no.

    To remove a database connection, first close the database using QSqlDatabase::close(), then remove it using the static method QSqlDatabase::removeDatabase().

    I guess I was begin too carefull.

  10. #10
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    The field resets to what it was before.

    This is with OnFieldChange

    Quote Originally Posted by jacek View Post
    What happens if you press the return key before clicking anywhere?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Can you edit your database with sqlbrowser demo that comes with Qt?

  12. #12
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: EditStrategy, how to save data

    Well that has no QSQL driver.
    But I can edit it with all other tools, query browser, phpmyadmin, own php.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Quote Originally Posted by JeanC View Post
    Well that has no QSQL driver.
    It should have access to the same drivers as your application.

    Have you tried to change the database with QSqlQuery?

  14. #14
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Quote Originally Posted by jacek View Post
    It should have access to the same drivers as your application
    .

    I had a quick look at that code, it indeed queries for drives. Only ones it comes up with are QSQLITE, QSQLITE2 and QPSQL

    Quote Originally Posted by jacek View Post
    Have you tried to change the database with QSqlQuery?
    Gonna try that tomorrow and let you know.
    Thanks for your time so far, appreciated.

  15. #15
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Yes, the same result, no error message, but no query executed either:

    QString moviename = model->record(currentrow).value("name").toString();
    QString querystr = "DELETE FROM pix WHERE name=";
    querystr.append(moviename);
    qDebug() << querystr;
    QSqlQuery query(querystr);
    query.exec();
    qDebug() << "query" << db.lastError().text();

    The output is:

    "DELETE FROM pix WHERE name=ana.avi"
    query " "

    Quote Originally Posted by jacek View Post
    Have you tried to change the database with QSqlQuery?

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Quote Originally Posted by JeanC View Post
    QSqlQuery query(querystr);
    query.exec();
    This actually executes the query twice.

    Quote Originally Posted by JeanC View Post
    qDebug() << "query" << db.lastError().text();
    What does qDebug() << query.lastError() output? What does db.isOpen() and db.isValid() return? Do you get any errors if you issue an invalid query? Do you start any transactions?

  17. #17
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Quote Originally Posted by jacek View Post
    This actually executes the query twice.
    Ah thanks.

    Quote Originally Posted by jacek View Post
    What does qDebug() << query.lastError() output? What does db.isOpen() and db.isValid() return? Do you get any errors if you issue an invalid query? Do you start any transactions?
    I'm gonna investigate.
    Oddity I noticed is that I can edit data with phpmyadmin, but the Mysql Query Browser has al edit options greyed out if I right click on a cell.

  18. #18
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Quote Originally Posted by jacek View Post
    What does qDebug() << query.lastError() output? What does db.isOpen() and db.isValid() return? Do you get any errors if you issue an invalid query? Do you start any transactions?
    qDebug() << "error" << db.lastError();
    qDebug() << "open" << db.isOpen();
    qDebug() << "valid" << db.isValid();

    Output:

    error QSqlError(-1, "", "")
    open true
    valid true

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Quote Originally Posted by JeanC View Post
    but the Mysql Query Browser has al edit options greyed out if I right click on a cell.
    Then maybe you don't have rights to modify the database?

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EditStrategy, how to save data

    Quote Originally Posted by JeanC View Post
    error QSqlError(-1, "", "")
    open true
    valid true
    And what about "query.lastError()"?

Similar Threads

  1. QSqlQueryModel data update
    By psi in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2012, 03:59
  2. Replies: 4
    Last Post: 19th October 2007, 19:47
  3. Data model
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2007, 12:14
  4. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.