我正在使用 qt 处理数据库。我需要做的一件事是使用一些键集作为输入数据来检索表的某些部分。这是我使用的代码:
bool getData(const QList<int> &idlist, QVector<DataStruct> &result)
{
result.resize(idlist.size());
QSqlDatabase &dbase = QSqlDatabase::database(conname); //conname is the QString holding the name of a connection
if (!dbase.transaction())
return false;
QString queryString(
"SELECT <column list> "
"FROM MyTableAS k "
"JOIN JoinedTable AS d ON k.foreign_key=d.primary_key "
"WHERE k.key=:key");
QSqlQuery *query = new QSqlQuery(QSqlDatabase::database(conname));
if(!query->prepare(queryString))
{
delete query;
dbase.rollback();
return false;
}
for (int i = 0; i < idlist.size(); i++)
{
query->bindValue(":key",idlist[i]);
if (!query->exec())
{
delete query;
dbase.rollback();
return false;
}
DataStruct &k = result[i];
memset(&k,0,sizeof(DataStruct));
if(query->next())
{
// fields of structure are mapped to query results
}
//result.append(k);
}
dbase.commit();
delete query;
return true;
}
我想知道,如何加快检索速度。我应该更改查询 - 如果是,如何更改?我应该在这里使用交易吗?我应该将使用数据库的方式更改为其他方式吗?
预先感谢!