我将 QT 与 Sqlite3 一起使用。当我做
Select name from database.sqlite_master WHERE type = "table"
我的数据库锁定。如果运行任何其他查询,数据库可以正常工作。似乎问题是当我专门运行命令来获取数据库中的所有表时。
这是我检索数据的 sqlite3 方法
bool SqliteManager::executeSQL(QString query, bool onlyExecute, bool pagination, bool initialize){
this->query=query;
sqlite3_stmt *st;
if(results->size()>0)results->clear();
if(sqlite3_prepare_v2(connection, this->query.toStdString().c_str(), -1, &st, 0)==SQLITE_OK){
if(!onlyExecute){
this->columnCount=sqlite3_column_count(st);
//Get Row Count With Original Query
this->rowCount=this->getRowCount(this->originalQuery, helper::haveLimit(this->originalQuery));
this->queryRowCount=this->getRowCount(this->query, helper::haveLimit(this->originalQuery));
cout << "Query count " << this->queryRowCount << " query executed " << this->query.toStdString() << endl;
//Get Column names and store the data inside the QString vector
for(int col=0;col<this->columnCount;col++)
columnNames->push_back(QString((char*)sqlite3_column_name(st, col)));
//Store the values inside the vector, if you want to get only one row (columnNumber*row) is your index
while(sqlite3_step(st)==SQLITE_ROW)
for(int col=0;col<this->columnCount;col++)
results->push_back(QString((char*)sqlite3_column_text(st, col)).remove('\r').remove('\n'));
}
else{
bool executed=false;
int status;
while((status=sqlite3_step(st))==SQLITE_OK)executed=true;
cout << "Command executed: " << this->query.toStdString() << endl;
sqlite3_finalize(st);
return executed || status == SQLITE_DONE;
}
cout << "finalize true" << endl;
sqlite3_finalize(st);
return true;
}
cout << "prepare error=> " << this->query.toStdString() << endl;
sqlite3_finalize(st);
return false;
}
!onlyExecute 将数据存储到一个向量中,以便我以后可以获取这些字段。这是我列出表格时调用的代码部分。对于插入(我在之后运行),else 是运行插入的位置。
问候。