qt sqlite create select
程序员文章站
2022-04-12 23:34:13
#include "mainwindow.h"#include #include #include#include#include#includeint main(int argc, char *argv[]){ QApplication a(argc, arg....
#include "mainwindow.h"
#include <QApplication>
#include <QApplication>
#include<QSqlDatabase>
#include<QSqlQuery>
#include<QDebug>
#include<QStringList>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//MainWindow w;
//w.show();
//add database driver
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
//set database name
db.setDatabaseName("sqlite3.db");
// db.setDatabaseName(":memory:");
//open database
if(!db.open()){
return false;
}
//execute following sql sentence
QSqlQuery query;
//new student table, set id as main key and name item
query.exec("create table student(id int primary key,name varchar)");
//insert 3 content into table
query.exec("insert into student values(1,'xiaoming')");
query.exec("insert into student values(2,'xiaogang')");
query.exec("insert into student values(3,'xiaowang')");
//check table id>=2
query.exec("select id, name from student where id>=2");
query.setForwardOnly(true);
//query.next() point to the first find data and move to the next record
while (query.next()) {
//query.value(0)
int value0 = query.value(0).toInt();
QString value1 = query.value(1).toString();
//output 2 values
qDebug()<<value0<<":"<<value1;
}
return a.exec();
}
本文地址:https://blog.csdn.net/nh5431313/article/details/108992796
上一篇: pycharm必知的一些简单设置方法
下一篇: 不买算了