//main.cpp
#include <QApplication>
#include <QSqlQuery>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
db.setDatabaseName( "test.db" );
if( !db.open() )
return false;
QSqlQuery query;
query.exec( "CREATE TABLE student("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"name VARCHAR,"
"age INT)" );
query.exec( "insert into student values( 0, ‘LL‘, 16 )" );
query.exec( "insert into student values( 1, ‘MM‘, 17 )" );
query.exec( "insert into student values( 2, ‘NN‘, 18 )" );
query.exec( "select * from student" );
while( query.isActive() && query.next() )
{
qDebug() << query.value( 0 ).toInt() << query.value( 1 ).toString()
<< query.value( 2 ).toInt() << "\n";
}
return a.exec();
}
结果:
原文地址:http://blog.51cto.com/weiyuqingcheng/2119897