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