qt连接数据库Sqlite
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了qt连接数据库Sqlite相关的知识,希望对你有一定的参考价值。
#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(); }
以上是关于qt连接数据库Sqlite的主要内容,如果未能解决你的问题,请参考以下文章