QT:控制台数据新增、删除、查询、更新

效果图:

 CH1301.pro 详细代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
QT -= gui
QT += sql
 
CONFIG += c++11 console
CONFIG -= app_bundle
 
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
 
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 
SOURCES += \
        main.cpp
 
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

  

main.cpp 详细代码:

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
110
111
112
113
#include <QCoreApplication>
#include <QTextCodec>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
//#include <QTime>  //不建议使用,将来禁用
#include <QElapsedTimer>
#include <QtDebug>
 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // <QTime>  不建议使用,将来禁用,建议改 用 <QElapsedTimer>
    //QTextCodec::setCodecForLocale(QTextCodec::setCodecForLocale()); // 设置显示中文
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("DESKTOP-O9O3HH7");// 设置主机名称
    db.setDatabaseName("qtDB.db"); // 设置数据库名称
    db.setUserName("zhouhejun");// 设置数据库用户名
    db.setPassword("123456"); //设置数据库密码
    db.open();
 
    // 创建数据库表
    QSqlQuery query ;
    bool success = query.exec("create table automobile "
                              "( id int primary key, attribute varchar, type varchar,"
                              "kind varchar, nation int, carnumber int,"
                              "elevaltor int, distance int,"
                              "oil int, temperature int ) ");
    if(success)
        qDebug()<<QObject::tr("数据库表创建成功!\n");
    else
       qDebug()<<QObject::tr("数据库表创建失败!\n");
    // 查询
    query.exec("select * from from automobile");
    QSqlRecord rec = query.record();
    qDebug()<<QObject::tr("automobile表字段数:")<<rec.count();
 
    //插入记录
    QElapsedTimer t; // 运行时间组件
   // QTime t;
    t.start(); //启动一个计时器,统计操作耗时
    query.prepare("insert into automobile values(?,?,?,?,?,?,?,?,?,?)");
    long records = 100 ;// 向表中插入任意的100条记录
    for (int i=0; i<records; i++){
        query.bindValue(0,i);
        query.bindValue(1,"四轮");
        query.bindValue(2,"轿车");
        query.bindValue(3,"富康");
        query.bindValue(4,rand()%100);
        query.bindValue(5,rand()%10000);
        query.bindValue(6,rand()%300);
        query.bindValue(7,rand()%200000);
        query.bindValue(8,rand()%52);
        query.bindValue(9,rand()%100);
        success=query.exec();
        if(!success)
        {
            QSqlError lastError = query.lastError();
            qDebug()<<lastError.driverText()<<QString(QObject::tr("插入失败"));
        }
        else
        {
           qDebug()<< QString(QObject::tr("插入成功,第 "))<<i<<QString(QObject::tr(" 行。"));
        }
 
    }
    qDebug()<<QObject::tr("插入 %1 条记录,耗时: %2 ms").arg(records).arg(t.elapsed());
 
    // 排序
    t.restart();//重启计时器
    success=query.exec("select * from automobile order by id desc"); // 降序
    if(!success)
    {
        qDebug()<<QObject::tr("排序 %1 条记录,耗时: %2 ms").arg(records).arg(t.elapsed());
    }
    else{
        qDebug()<<QObject::tr("排序失败!");
    }
 
    //更新记录
    t.restart();
    for(int i =0; i<records; i++){
        query.clear();
        query.prepare(QString("update automobile set attribute=?,"
                              "type=?, kind=?, elevaltor=?,"
                              "distance=?, oil=?, temperature=?  "
                              "where id= %1").arg(i));
        query.bindValue(0,"四轮");
        query.bindValue(1,"轿车");
        query.bindValue(2,"富康");
        query.bindValue(3,rand()%100);
        query.bindValue(4,rand()%10000);
        query.bindValue(5,rand()%300);
        query.bindValue(6,rand()%200000);
        query.bindValue(7,rand()%52);
        query.bindValue(8,rand()%100);
        success= query.exec();
        if(!success){
            QSqlError lastError= query.lastError();
            qDebug()<<lastError.driverText()<<QString(QObject::tr("更新失败"));
        }
    }
     qDebug()<<QObject::tr("更新 %1 记录,耗时: %1 ms").arg(records).arg(t.elapsed());
    // 删除
    t.restart();
    query.exec("delete from automobile where id = 15 ");
    // 输出操作耗时
    qDebug()<<QObject::tr("删除一条记录,耗时: %1 ms").arg(t.elapsed());
    return 0; // 退出
    //return a.exec();
}

  

posted @   samrv  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示