欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

qt sqlite create select

程序员文章站 2022-07-06 09:33:56
#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

相关标签: # Qt # sqlite