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

效果图:

 CH1301.pro 详细代码

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 详细代码:

#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 @ 2024-11-09 09:48  samrv  阅读(12)  评论(0编辑  收藏  举报