Results 1 to 2 of 2

Thread: QTSQLITE, prepared query and bindValue()

  1. #1
    Join Date
    Nov 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Default QTSQLITE, prepared query and bindValue()

    I'm creating a query:
    Qt Code:
    1. void Base::AddTrack(Track& newTrack)
    2. {
    3.  
    4. QSqlQuery mainTrackQuery(db);//inserting to the track table
    5. QString mainQueryString("INSERT INTO Tracks( :title, :path ) \
    6. VALUES( :titleV, :pathV)");
    7.  
    8. mainTrackQuery.exec();
    9.  
    10.  
    11. if(newTrack.path == "")
    12. {
    13. mainQueryString.remove(":path,");
    14. mainQueryString.remove(":pathV,");
    15. }
    16. else
    17. {
    18.  
    19. mainQueryString.replace(":pathV", "SELECT id FROM Genres WHERE value = :pathV");
    20. }
    21.  
    22. if(newTrack.title == "")
    23. {
    24. mainQueryString.remove(":tetle,");
    25. mainQueryString.remove(":tetleV,");
    26. }
    27. else
    28. {
    29.  
    30. mainQueryString.replace(":titleV", "SELECT id FROM Titles WHERE value = :titleV");
    31. }
    32.  
    33.  
    34. //inserting values
    35.  
    36. mainTrackQuery.prepare(mainQueryString);
    37.  
    38. mainTrackQuery.bindValue(":path", "pathId");
    39. mainTrackQuery.bindValue(":pathV", newTrack.path);
    40. mainTrackQuery.bindValue(":title", "titleId");
    41. mainTrackQuery.bindValue(":titleV", newTrack.title);
    42. cout <<mainTrackQuery.exec()<<endl;
    43. cout <<"MEGATRACK_"<< mainTrackQuery.lastError().text().toStdString()<<endl;
    44. cout << mainTrackQuery.executedQuery().toStdString()<<endl;
    45.  
    46. }
    To copy to clipboard, switch view to plain text mode 



    As the result:
    mainTrackQuery.exec() returns 0,
    mainTrackQuery.lastError().text() returns "Parameter count mismatch"


    lastExecutedQuery gives this:

    Qt Code:
    1. INSERT INTO Tracks( ?, ? )
    2. VALUES( SELECT id FROM Tit
    3. les WHERE value = ?, SELECT id FROM Paths WHERE value = ?)
    To copy to clipboard, switch view to plain text mode 
    AFAIK, that should be correct: SQLITE receives the arguments separately with main body of the prepared query?
    But how can I remove such a problem?
    Or how to live without prepared queries and correctly save the paths with the << ' >> apostrophe symbol, par example?
    Last edited by YaK; 30th May 2009 at 18:27. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTSQLITE, prepared query and bindValue()

    Sorry to say it so rude but your code is **§&%$&**! Instead of remove and replace better join single pieces or use "element bit masks"...

    To your errors:

    Line 8: skip mainTrackQuery.exec();
    Line 13,14: Won't remove anything because of the mismatching ","!
    Line 19/30: Mustn't the SELECT statement in squares?
    Line 24,25: Typo! title not tetle...


    Now working?


    Regards, Lykurg


    EDIT: And if you remove e.g ":title" you can't bind it later!

Tags for this Thread

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.