example:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setHostName("localhost"); db.setDatabaseName("D:/TEST.pro"); bool ok = db.open(); if (!ok) { QSqlError err = db.lastError(); QString dbtxt = err.databaseText(); QString drtxt = err.driverText(); QString alltxt = err.text(); } db.transaction(); QSqlQuery query(db); bool bok = query.exec("create table TESTDB" "(" "PersonId intege," "Age intege" "); "); if (!bok) { QSqlError err = db.lastError(); QString dbtxt = err.databaseText(); QString drtxt = err.driverText(); QString alltxt = err.text(); } query.prepare("insert into TESTDB(PersonId,Age)" "values(?,?)"); query.bindValue(0, 111); query.bindValue(1, 22); bok = query.exec(); if (!bok) { QSqlError err = db.lastError(); QString dbtxt = err.databaseText(); QString drtxt = err.driverText(); QString alltxt = err.text(); } db.commit(); db.close();
验证方法:GOOGLE插件SQLite Reader;插件可以执行SQL语句直接看到内容