Nota: questo è con SQLite, anche se mi aspetto che il problema sia sul lato Qt.Qt - Come associare una QList a una QSqlQuery con una clausola "WHERE ... IN"?
In primo luogo, ho creato una tabella di database dalla SQLite strumento da riga di comando:
sqlite> create table testtable (id INTEGER PRIMARY KEY NOT NULL, state INTEGER);
sqlite> insert into testtable (state) values (0);
sqlite> insert into testtable (state) values (1);
sqlite> insert into testtable (state) values (9);
sqlite> insert into testtable (state) values (20);
Poi ho testare la mia domanda: (. Risultati Quelli sono attesi)
sqlite> SELECT id,state FROM testtable WHERE state IN (0,1,2);
1|0
3|1
Quindi eseguo questo codice C++:
void runQuery() {
QSqlQuery qq;
qq.prepare("SELECT id,state FROM testtable WHERE state IN (:states)");
QList<QVariant> statesList = QList<QVariant>();
statesList.append(0);
statesList.append(1);
statesList.append(2);
qq.bindValue(":states", statesList);
qq.exec();
qDebug() << "before";
while(qq.next()) {
qDebug() << qq.value(0).toInt() << qq.value(1).toInt();
}
qDebug() << "after";
}
che stampa questo:
prima
dopo
sono stati stampati Nessuna riga. Presumo questo perché non posso associare una lista direttamente a un segnaposto in una clausola "in". Ma c'è un modo per farlo? Non sono stato in grado di trovare nulla a riguardo.