Querying database with list of dynamic values

  • Hi!

    I have three tables (just an example to illustrate my problem):

    1. signatures with columns ID, name.
    2. meta with columns ID, metadata.
    3. signatures2meta with columns ID, meta_id, signature_id.

    The reason behind there tables is that there could be different signatures with exactly the same meta information.

    I have a QListView, which is bound to column 'name' of table 'signatures'. It shows all the names. This works fine. User can select items in QListView.

    I would like to get the selected items, obtain their IDs and find corresponding metadata information.

    I started with slot for QListView click event and there I have:

    QVariantList signatureIds;
    QModelIndexList indexes = ui->lstSignatures->selectionModel()->selectedIndexes();
    foreach (QModelIndex idx, indexes) {
        QSqlRecord record = signaturesModel_->record(idx.row());
        signatureIds << record.value("id").toInt();

    At this point I have the list of IDs, but I do not understand how to proceed with the further query. In SQL this is achievable with such a query:

    select * from meta inner join signatures2meta on signatures2meta.meta_id = meta.id where signatures2meta.signature_id in (a, b)

    where (a, b) are IDs of interest. Could also be another select.

    For now I ended up by creating a string with list of all ids (like "1, 2") and inserting this string into the query directly (without bind). This works, but doesn't feel elegant at all.

    Any advise on this?


  • Lifetime Qt Champion


    Do you mean get additional data using QSqlQuery ?

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.