最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

QT5中使用SQLite的實(shí)現(xiàn)方法

 更新時(shí)間:2021年11月26日 16:31:59   作者:姜亞軻  
SQLite是一款開源輕量級(jí)的數(shù)據(jù)庫軟件,本文主要介紹了QT5中使用SQLite的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

SQLite(sql)是一款開源輕量級(jí)的數(shù)據(jù)庫軟件,不需要server,可以集成在其他軟件中,非常適合嵌入式系統(tǒng)。
Qt5以上版本可以直接使用SQLite。

1、修改.pro文件,添加SQL模塊:

QT += sql

2、main.cpp代碼如下:

#include "mainwindow.h"
#include <QApplication>
//添加頭文件
#include <qdebug.h>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    //建立并打開數(shù)據(jù)庫
    QSqlDatabase database;
    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("MyDataBase.db");
    if (!database.open())
    {
        qDebug() << "Error: Failed to connect database." << database.lastError();
    }
    else
    {
        qDebug() << "Succeed to connect database." ;
    }

    //創(chuàng)建表格
    QSqlQuery sql_query;
    if(!sql_query.exec("create table student(id int primary key, name text, age int)"))
    {
        qDebug() << "Error: Fail to create table."<< sql_query.lastError();
    }
    else
    {
        qDebug() << "Table created!";
    }

    //插入數(shù)據(jù)
    if(!sql_query.exec("INSERT INTO student VALUES(1, \"Wang\", 23)"))
    {
        qDebug() << sql_query.lastError();
    }
    else
    {
        qDebug() << "inserted Wang!";
    }
    if(!sql_query.exec("INSERT INTO student VALUES(2, \"Li\", 23)"))
    {
        qDebug() << sql_query.lastError();
    }
    else
    {
        qDebug() << "inserted Li!";
    }

    //修改數(shù)據(jù)
    sql_query.exec("update student set name = \"QT\" where id = 1");
    if(!sql_query.exec())
    {
        qDebug() << sql_query.lastError();
    }
    else
    {
        qDebug() << "updated!";
    }

    //查詢數(shù)據(jù)
    sql_query.exec("select * from student");
    if(!sql_query.exec())
    {
        qDebug()<<sql_query.lastError();
    }
    else
    {
        while(sql_query.next())
        {
            int id = sql_query.value(0).toInt();
            QString name = sql_query.value(1).toString();
            int age = sql_query.value(2).toInt();
            qDebug()<<QString("id:%1    name:%2    age:%3").arg(id).arg(name).arg(age);
        }
    }

    //刪除數(shù)據(jù)
    sql_query.exec("delete from student where id = 1");
    if(!sql_query.exec())
    {
        qDebug()<<sql_query.lastError();
    }
    else
    {
        qDebug()<<"deleted!";
    }

    //刪除表格
    sql_query.exec("drop table student");
    if(sql_query.exec())
    {
        qDebug() << sql_query.lastError();
    }
    else
    {
        qDebug() << "table cleared";
    }

    //關(guān)閉數(shù)據(jù)庫
    database.close();
    return a.exec();
}

3、應(yīng)用程序輸出如下:

這里寫圖片描述

4、創(chuàng)建的 MyDataBase.db 在build的這個(gè)文件夾下:
D:\QT\project\build-sl-Desktop_Qt_5_10_1_MinGW_32bit-Debug

到此這篇關(guān)于QT5中使用SQLite的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)QT5使用SQLite內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

繁昌县| 舒兰市| 桐乡市| 怀安县| 英德市| 大英县| 霍邱县| 长葛市| 慈利县| 青河县| 交城县| 喀喇沁旗| 日喀则市| 大城县| 涞水县| 藁城市| 宣武区| 阿克苏市| 南投市| 吉林省| 西藏| 都安| 合水县| 县级市| 曲松县| 台江县| 仁怀市| 九龙县| 浏阳市| 沂源县| 姜堰市| 右玉县| 浪卡子县| 孝感市| 泗阳县| 太仆寺旗| 龙里县| 石门县| 右玉县| 泌阳县| 绍兴市|