PDA

View Full Version : QTSQLITE, prepared query and bindValue()



YaK
30th May 2009, 18:15
I'm creating a query:


void Base::AddTrack(Track& newTrack)
{

QSqlQuery mainTrackQuery(db);//inserting to the track table
QString mainQueryString("INSERT INTO Tracks( :title, :path ) \
VALUES( :titleV, :pathV)");

mainTrackQuery.exec();


if(newTrack.path == "")
{
mainQueryString.remove(":path,");
mainQueryString.remove(":pathV,");
}
else
{

mainQueryString.replace(":pathV", "SELECT id FROM Genres WHERE value = :pathV");
}

if(newTrack.title == "")
{
mainQueryString.remove(":tetle,");
mainQueryString.remove(":tetleV,");
}
else
{

mainQueryString.replace(":titleV", "SELECT id FROM Titles WHERE value = :titleV");
}


//inserting values

mainTrackQuery.prepare(mainQueryString);

mainTrackQuery.bindValue(":path", "pathId");
mainTrackQuery.bindValue(":pathV", newTrack.path);
mainTrackQuery.bindValue(":title", "titleId");
mainTrackQuery.bindValue(":titleV", newTrack.title);
cout <<mainTrackQuery.exec()<<endl;
cout <<"MEGATRACK_"<< mainTrackQuery.lastError().text().toStdString()<<endl;
cout << mainTrackQuery.executedQuery().toStdString()<<endl;

}




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


lastExecutedQuery gives this:


INSERT INTO Tracks( ?, ? )
VALUES( SELECT id FROM Tit
les WHERE value = ?, SELECT id FROM Paths WHERE value = ?)
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?

Lykurg
30th May 2009, 19:13
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!