Results 1 to 7 of 7

Thread: Problems with Quey

  1. #1
    Join Date
    Jul 2007
    Location
    Coimbra, Portugal
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Post Problems with Quey

    Hi,

    I've a problem in updating data using a Model/View approach.

    If I try to do this:

    Qt Code:
    1. bool PhonoLabSentenceSqlModel::setCode(int sentenceId, const QString &code)
    2. {
    3. QSqlQuery query;
    4. query.prepare("UPDATE sentence SET code='?' WHERE id=?");
    5. query.addBindValue(code);
    6. query.addBindValue(sentenceId);
    7. if (!query.exec()) {
    8. return false;
    9. }
    10. return true;
    11. }
    To copy to clipboard, switch view to plain text mode 

    But if change to this, it works fine.

    Qt Code:
    1. bool PhonoLabSentenceSqlModel::setCode(int sentenceId, const QString &code)
    2. {
    3. QSqlQuery query;
    4. QString s = "UPDATE sentence SET code='" + code + "' WHERE id=" + QString::number(sentenceId);
    5. query.prepare(s);
    6. if (!query.exec()) {
    7. return false;
    8. }
    9. return true;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Does anyone know what I'm doing wrong
    --
    Tiago Correia

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with Quey

    What if you drop the single quotes for the code value? Maybe there's a parsing problem.

    Regards

  3. #3
    Join Date
    Jul 2007
    Location
    Coimbra, Portugal
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems with Query

    I've also tried that. It is really strange.

    I've also tested with double qoute ".

    I'm using SQLITE as database engine.
    --
    Tiago Correia

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with Quey

    Well, the "?" placeholder syntax is supported only by ODBC.
    Do you connect using ODBC, or something else?

    Regards

  5. #5
    Join Date
    Jul 2007
    Location
    Coimbra, Portugal
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems with Query

    I'm using Qt SQLITE plugin. But I've another method that I use the ? place holder, and it works fine.

    Qt Code:
    1. bool PhonoLabSentenceSqlModel::insertRows (int row, int count, const QModelIndex & parent)
    2. {
    3. beginInsertRows(parent, row, row);
    4.  
    5. QSqlQuery query;
    6. QSqlDatabase db = QSqlDatabase::database();
    7. db.transaction();
    8.  
    9. int id = generateId("sentence");
    10.  
    11. query.prepare("INSERT INTO sentence(id, actionid, objectid) VALUES(?, 0, 0)");
    12. query.addBindValue(id);
    13. if (!query.exec()) {
    14. db.rollback();
    15. return false;
    16. }
    17.  
    18. QStringList languages;
    19. languages << "pt" << "en" << "es";
    20. QString lang;
    21. foreach (lang, languages) {
    22. query.prepare("INSERT INTO sentence_translation(sentenceid, language, sentencetext) VALUES(?, ?, '')");
    23. query.addBindValue(id);
    24. query.addBindValue(lang);
    25. if (!query.exec()) {
    26. db.rollback();
    27. return false;
    28. }
    29. }
    30.  
    31. db.commit();
    32. endInsertRows();
    33. return true;
    34. }
    To copy to clipboard, switch view to plain text mode 
    --
    Tiago Correia

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with Quey

    Well, I think those are the only constructs in which the "?" placeholder works.
    Also are the examples are like:
    INSERT INTO sentence(id, actionid, objectid) VALUES(?, 0, 0)
    .

    I think you can stick to your solution.

    Regards

  7. #7
    Join Date
    Jul 2007
    Location
    Coimbra, Portugal
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems with Quey

    So, if I understand, only when I use INSERT, I can use the placeholder ?.

    In UPDATE, for example, I can't use it.
    --
    Tiago Correia

Similar Threads

  1. Replies: 2
    Last Post: 8th March 2007, 22:22
  2. Utf8 problems
    By cristiano in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2006, 00:14
  3. Problems building mysql plugin for Qt 4.1.2 on windows XP
    By Philip_Anselmo in forum Installation and Deployment
    Replies: 3
    Last Post: 17th May 2006, 15:38
  4. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39
  5. problems with Opengl
    By SlawQ in forum Qt Programming
    Replies: 4
    Last Post: 12th February 2006, 22:49

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.