QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "memdb");
db.setDatabaseName(":memory:");
if (!db.open())
{
qDebug() << db.lastError().text();
// std::exit(1);
}
QSqlQuery q("", db);
q.exec("create table Universities (id integer primary key, Location varchar, Population integer)");
q.exec("insert into Universities values (0, 'MSK', 12000000)");
q.exec("insert into Universities values (1, 'NSK', 1600000)");
q.exec("insert into Universities values (2, 'SPB', 6000000)");
q.exec("insert into Universities values (3, 'PRM', 1000000)");
int sz = ui->tableWidget->rowCount();
QString queryString = "SELECT * FROM Universities WHERE Location IN (";
for(int i = 0; i < sz; ++i)
{
queryString += "?,";
}
queryString.back() = ')';
QSqlQuery query(queryString, db);
for(int i = 0; i < sz; ++i)
{
query.bindValue(i, ui->tableWidget->item(i, 0)->text());
}
if (!query.exec())
{
qDebug() << query.lastError().text();
// std::exit(1);
}
while(query.next())
{
qDebug() << query.value(0).toInt() << " "
<< query.value(1).toString() << " "
<< query.value(2).toInt() << "\n";
}