qt连接数据库Sqlite
时间:2021-07-01 10:21:17
帮助过:20人阅读
<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
标签:insert basename [] set data include tab bug arch