Filip Koper
Filip Koper

Reputation: 39

QString in sqlite select

I want to search string called x.

QString x=abc->text();

QSqlQuery qry("SELECT nazwa FROM piosenki WHERE nazwa like'%x%' ");

x is treat like a single char not like abc->text(); What i can do to pick QString x? Important for me is SQlite LIKE clause becouse i want to pick from my database all similar to abc->text().

Upvotes: 0

Views: 254

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

A simple option is to use .arg()

QString x=abc->text();
QSqlQuery query(QString("SELECT nazwa FROM piosenki WHERE nazwa like'%%1%'").arg(x));
if(query.exec()){
    int fieldNo = query.record().indexOf("nazwa");
    while (query.next()) {
        qDebug()<< query.value(fieldNo).toString();
    }
}

Or with prepare() and bindValue():

QString x=abc->text();
QSqlQuery query;
query.prepare("SELECT nazwa FROM piosenki WHERE nazwa LIKE :nazwa");
query.bindValue(":nazwa", "%" +x + "%");
if(query.exec()){
    int fieldNo = query.record().indexOf("nazwa");
    while (query.next()) {
        qDebug()<< query.value(fieldNo).toString();
    }
}

Upvotes: 1

Related Questions