zoukankan      html  css  js  c++  java
  • QT5中使用SQLite

    SQLite(sql)是一款开源轻量级的数据库软件,不需要server,可以集成在其他软件中,非常适合嵌入式系统。
    Qt5以上版本可以直接使用SQLite。

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

    QT += sql
    1
    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);

    //建立并打开数据库
    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." ;
    }

    //创建表格
    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!";
    }

    //插入数据
    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!";
    }

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

    //查询数据
    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);
    }
    }

    //删除数据
    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";
    }

    //关闭数据库
    database.close();
    return a.exec();
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    3、应用程序输出如下:


    4、创建的 MyDataBase.db 在build的这个文件夹下:
    D:\QT\project\build-sl-Desktop_Qt_5_10_1_MinGW_32bit-Debug
    ————————————————
    版权声明:本文为CSDN博主「姜亚轲」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/weixin_41656968/article/details/80473137

  • 相关阅读:
    jsp第六周作业
    jsp第四次作业
    JSP第二次作业 2021 0310
    软件测试作业 NO.1
    北航软工优秀作业汇总
    Alpha阶段评审结果和意见反馈
    转会候选人名单
    人员转会流程
    关于团队项目阶段目标的说明
    2021年软工-热身阅读作业
  • 原文地址:https://www.cnblogs.com/chinasoft/p/15251409.html
Copyright © 2011-2022 走看看