#include#include #include #include int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("student.db"); if(!db.open()) { qDebug() << "open err"; } else { qDebug() << "database is ok"; } QSqlQuery query(db); bool isok = query.exec("create table stud(id int primary key,name varchar(20))"); query.exec("insert into stud values(111,'liming')"); query.exec("insert into stud values(102,'li')"); query.exec("insert into stud values(103,'lisi')"); query.exec("insert into stud values(105,'zhangsan')"); if(!isok) { qDebug() << "table err"; } else { qDebug() << "table is ok"; } query.exec("select * from stud"); while(query.next()) { qDebug() << "id:"< <<"name:"<
if(QSqlDatabase::contains("myconn")) { db = QSqlDatabase::database("myconn"); }else { db = QSqlDatabase::addDatabase("QSQLITE","myconn"); } db.setDatabaseName("sqlite.db"); bool ok = db.open(); if(ok) { return true; }else { return false; }