https://blog.csdn.net/u014252478/article/details/79980899
#include <QtSql/QSqlDatabase> #include <QtSql/QSqlQuery> #include <QtSql/QSqlRecord> #include <QtSql/qsqlerror.h> int main(int argc, char *argv[]) { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); QString path =/* QApplication::applicationDirPath() + */"F:\record.db"; db.setDatabaseName(path); bool bOpen = db.open(); if (!bOpen) { return 0; } QSqlQuery query(db); QString sqlStr ="CREATE TABLE [detail] ([ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, [Time] DATETIME NOT NULL, [DevModel] VARCHAR NOT NULL, [DevSN] VARCHAR,[Result] VARCHAR NOT NULL,[Item] VARCHAR NOT NULL, [Mark] VARCHAR,[ChangeSystem] VARCHAR,[CTMEI] VARCHAR)"; QString selectStr ="select * from detail "; bool success = query.exec(selectStr);//"select * from test " QSqlRecord rec = query.record(); int a = rec.count(); if(success) { while(query.next()) { for(int index = 0; index < 10; index++){ QVariant a =(QVariant)query.value(index); } } }else{ //QString strErr = query.lastError().text() ; } query.clear(); //不加释放时内存异常 db.close(); return 1; /* QApplication a(argc, argv); camera_emt w; w.show(); return a.exec();*/ }
另外 GetSystemDirectory 路径问题