zoukankan      html  css  js  c++  java
  • Qt笔记——QSqlLite

    静态数据库,简单方便

    在.pro文件里添加 +sql

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    
    namespace Ui {
    class Widget;
    }
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Widget(QWidget *parent = 0);
        ~Widget();
    
    private:
        Ui::Widget *ui;
    };
    
    #endif // WIDGET_H
    #include "widget.h"
    #include "ui_widget.h"
    #include <QSqlDatabase>
    #include <QDebug>
    #include <QMessageBox>
    #include <QSqlError>
    #include <QSqlQuery>
    #include <QVariantList>
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
        //打印Qt支持的数据库驱动
        qDebug()<<QSqlDatabase::drivers();
        //添加Sqlite数据库
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        //设置数据库
        db.setDatabaseName("../info.db");
    
        //打开数据库
        if(!db.open())
        {
            QMessageBox::warning(this,"error",db.lastError().text());
            return;
        }
    
    
        QSqlQuery query;
        query.exec("create table if not exists student(id int primary key, name varchar(255), age int, score int)");
        query.prepare("insert into student(id,name, age, score) values(:id,:name, :age, :score)");
        //给字段设置内容 list
        QVariantList idList;
        idList<<1<<2<<3;
        QVariantList nameList;
        nameList << "xiaoming" << "xiaolong" << "xiaojiang";
        QVariantList ageList;
        ageList << 11 << 22 <<33;
        QVariantList scoreList;
        scoreList << 59 << 69 << 70;
        //给字段绑定相应的值 按顺序绑定
        query.addBindValue(idList);
        query.addBindValue(nameList);
        query.addBindValue(ageList);
        query.addBindValue(scoreList);
        //执行预处理命令
        query.execBatch();
    
        query.exec("select * from student");
        while(query.next())
        {
            //取出当前行的内容
            qDebug()<<query.value(0).toInt()
                   << query.value(1).toString()
                   << query.value("age").toInt()
                   << query.value("score").toInt();
        }
    
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
  • 相关阅读:
    [转] ArcGIS自定义工具完全教程 1/2
    ArcMap工具栏中嵌入自定义按钮
    自定义ArcGIS应用程序
    制作ArcGIS DLL插件的安装包
    如何编写和注册.Net的Com组件
    空间参考(三)ArcGIS Engine的支持
    ArcGIS Desktop开发基础
    ArcGIS中坐标系统简介
    Ao开发
    ArcGIS Desktop Addin插件开发系列
  • 原文地址:https://www.cnblogs.com/dalanjing/p/8849518.html
Copyright © 2011-2022 走看看