打開數據庫:
QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName(mDataName);
if(!database.open()) {
p; return false;
}
QSqlQuery *queryer = new QSqlQuery();
查詢數據庫:www.2cto.com
QString selectStr = "";
for(<pre name="code" class="cpp">QList<QString>::iterator iterator = args.begin();iterator < args.end();iterator++)
{
selectStr += *iterator + ",";
}
selectStr.remove(selectStr.size() - 1, 1);
QString queryStr = QObject::tr("select %1 from %2").arg(selectStr,table);
bool flag = queryer->exec(queryStr);
if(!flag) {
std::cout << "數據庫查詢失敗" << std::endl;
return;
}
if(!queryer->first())
{
std::cout << "沒有數據" << std::endl;
return;
}
do{
proxy->execut(queryer);
}while(queryer->next());
關閉數據庫:
if(queryer != NULL)
{
delete queryer;
}
database.close();
添加數據庫插件搜索路經:
QApplication::addLibraryPath(QObject::tr("./plugins"));
添加編譯選項:
QT += core gui sql network
摘自 緣起