代码:mian.cpp
- #include<QtCore/QCoreApplication>
- #include<Qtsql>
- #include<QDebug>
- intmain(intargc,char*argv[])
- {
- QCoreApplicationa(argc,argv);
- QsqlDatabasedb=QsqlDatabase::addDatabase("QsqlITE");
- db.setDatabaseName("/tmp/my.db");
- if(!db.open())
- {
- qDebug()<<"opendatabaseFailed---"<<db.lastError().text()<<"/n";
- return-1;
- }
- QsqlQueryquery;
- boolok=query.exec("CREATETABLEIFNOTEXISTSpeople(idINTEGERPRIMARYKEYAUTOINCREMENT,"
- "nameVARCHAR(20)NOTNULL,"
- "ageINTEGERNULL)");
- if(ok)
- qDebug()<<"ceatetablepartitionsuccess/n";
- else
- qDebug()<<"ceatetablepartitionFailed/n";
- for(inti=0;i<3;++i)
- query.prepare("INSERTINTOpeople(id,name,age)VALUES(:id,:name,:age)");
- query.bindValue(":name",QString("smith_%1").arg(i+1));
- query.bindValue(":age",20+i*5);
- query.exec();
- }
- //QsqlQueryquery;
- query.exec("SELECTid,ageFROMpeople");
- while(query.next())
- qDebug()<<"people("<<query.value(0).toInt()<<")name:"<<query.value(1).toString()<<"age:"<<query.value(2).toInt();
- returna.exec();
- }
sql.pro:
?@H_361_301@