#include <QCoreApplication> #include <QDebug> #include <QSqlDatabase> #include <QSqlQuery> 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:"<<query.value(0).toInt()<<"name:"<<query.value(1).toString(); } return a.exec(); }
时间: 2024-09-30 05:45:34