1

I got a annoying problem. There's a code in qt that does not work correctly, there's no syntax and runtime errors, only sqlite3 does not like my query.

This function removes friend or friends that user has selected on his profile window clicking on the pushbutton.

bool Profilewindow::removeFriends() {
QList<QListWidgetItem *> selectedFriends = ui -> userfriendsList -> selectedItems();
QSqlQuery query;
QStringList stringlist;

if ( MainWindow::getInstance() -> isConnectedToDB() ) {
        query.prepare("DELETE FROM user_friends WHERE user_id = :user_id AND f_fname = :f_fname AND f_lname = :f_lname");
    for ( int i = 0; i < selectedFriends.count(); ++i ) {

        stringlist = selectedFriends.at(i) -> text().split(" ");

        qDebug() << this -> user_id;
        query.bindValue(":user_id", this->user_id);

        qDebug() << stringlist.at(0);
        query.bindValue(":f_fname", stringlist.at(0));

        qDebug() << stringlist.at(1);
        query.bindValue(":f_lname", stringlist.at(1));

        if ( !query.exec() ) {
            qDebug() << query.lastError().text();
        }

    }
    MainWindow::getInstance() -> closeDBConnection();
    if ( this -> updateFriendsList() ) return true;
    return false;
}

return false;

}

all the parameters thats binding are correctly recieved and they are not null or empty string or any other wroung data.

when query is executing qDebug outputs this: "Parameter count mismatch"

can't understand what does this strange error mean ?

ampawd
  • 968
  • 8
  • 26
  • Are you sure the data is already in the database that you are trying to remove? – László Papp Apr 17 '14 at 02:46
  • Could you please print out and show `QSqlQuery::lastQuery()` after the before? Could you please post an [SSCCE](http://www.sscce.org) example? – László Papp Apr 17 '14 at 05:57
  • Have you tried to move the `query.prepare("DELETE FROM user_friends WHERE user_id = :user_id AND f_fname = :f_fname AND f_lname = :f_lname");` within the loop so that it always prepares? – László Papp Apr 17 '14 at 06:12
  • yeah all the data I want to delete is already in the database, also when I tried to move the prepare of query in the loop body, same error ("parameter count mismatch") was occuring – ampawd Apr 17 '14 at 18:50

1 Answers1

0

Sometime I make such simple queries like this:

QString querytext = QString("DELETE FROM user_friends WHERE user_id = '%1' AND f_fname = '%2' AND f_lname = '%3'")
    .arg(this->user_id)
    .arg(stringlist.at(0))
    .arg(stringlist.at(1));

qDebug() << querytext; // what is really sent do database engine?

query.exec(querytext);

The main adventage of QSqlQuery::prepare() method is that it really prepare the execution plan in database engine so the plan could not be prepared again and again. In case of repeated executions the run can be much faster, see:

In SQLite, do prepared statements really improve performance?

Community
  • 1
  • 1