QKingbase的批量操作

#include <QCoreApplication>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QtSql/QSqlRecord>
#include <QVariantList>
#include <QTime>
#include <QDateTime>
#include <QDebug>
#include <QTextCodec>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QSqlDatabase db = QSqlDatabase::addDatabase("QKINGBASE");
    int elapseTotal = 0, i = 0, j = 0;
    QSqlQuery query;

    db.setHostName("localhost"); //设置数据库主机名,如果主机名不设置,那么认为DatabaseName就是服务名,通过服务名去连接。行为同QOCI一致
    db.setDatabaseName("TEST"); //设置数据库名
    db.setUserName("SYSTEM"); //设置数据库登入用户名
    db.setPassword("MANAGER"); //设计数据库登入密码
    db.open();//打开数据库连接

    db.exec("set client_encoding to 'gbk'");
    db.exec("drop table PERFORMANCE_COLLECTION");

    //Func-QKingbase-Create-Table
    query = db.exec("create table PERFORMANCE_COLLECTION(id bigint, description varchar(200), record_time timestamp)");
    if (query.lastError().type() == QSqlError::NoError)
        printf("%-50s: OK\n", "Func-QKingbase-Create-Table");
    else
        printf("%-50s: FAILED\n", "Func-QKingbase-Create-Table");

    //Func-QKingbase-Alter-Table
    query = db.exec("Alter table PERFORMANCE_COLLECTION add constraint PERFORMANCE_COLLECTION_PKEY primary key (id)");
    if (query.lastError().type() == QSqlError::NoError)
        printf("%-50s: OK\n", "Func-QKingbase-Alter-Table");
    else
        printf("%-50s: FAILED\n", "Func-QKingbase-Alter-Table");

    //Func-QKingbase-Insert
    query = db.exec("insert into PERFORMANCE_COLLECTION values(1,'This is description, for test Func-QKingbase-Insert', now())");
    if (query.lastError().type() == QSqlError::NoError)
        printf("%-50s: OK\n", "Func-QKingbase-Insert");
    else
        printf("%-50s: FAILED\n", "Func-QKingbase-Insert");

    //Func-QKingbase-Update
    query = db.exec("update PERFORMANCE_COLLECTION set description = 'This is description, for test Func-QKingbase-Update' where id = 1");
    if (query.lastError().type() == QSqlError::NoError)
        printf("%-50s: OK\n", "Func-QKingbase-Update");
    else
        printf("%-50s: FAILED\n", "Func-QKingbase-Update");

    //Func-QKingbase-Select
    query = db.exec("select count(*) from PERFORMANCE_COLLECTION where id = 1 and description like '%Update'");
    if (query.lastError().type() == QSqlError::NoError &&
        1 == query.record().count())
        printf("%-50s: OK\n", "Func-QKingbase-Select");
    else
        printf("%-50s: FAILED\n", "Func-QKingbase-Select");

    //Func-QKingbase-Delete
    query = db.exec("delete from PERFORMANCE_COLLECTION where id = 1");
    if (query.lastError().type() == QSqlError::NoError)
        printf("%-50s: OK\n", "Func-QKingbase-Delete");
    else
        printf("%-50s: FAILED\n", "Func-QKingbase-Delete");


    //Per-QKingbase-Batch-Insert
    for (j = 0; j < 100; j++)
    {
        QTime t;
        QVariantList ids, descs, rts;
        int elapsed;
        for (i = 0; i < 10000; i++)
        {
            QString strDateTime;
            QDateTime dt = QDateTime::currentDateTime();
            strDateTime.sprintf("%04d-%02d-%02d %02d:%02d:%02d",
                                dt.date().year(), dt.date().month(), dt.date().day(),
                                dt.time().hour(), dt.time().minute(), dt.time().second());
            ids << i + j *  10000;
            descs << "'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'";
            rts << dt;//strDateTime;
        }

        QSqlQuery query;
        query.prepare("insert into PERFORMANCE_COLLECTION values(?, ?, ?)");

        query.addBindValue(ids);
        query.addBindValue(descs);
        query.addBindValue(rts);

        t.start();
        query.execBatch();
        elapsed = t.elapsed();
        elapseTotal += elapsed;

        qDebug() << "inserted 10000 rows, elapsed: " << elapsed << " ms";
    }

    qDebug() <<"AVG: " << elapseTotal / j << " ms" << endl;

    //Func-QKingbase-Drop-Table
    query = db.exec("drop table  PERFORMANCE_COLLECTION");
    if (query.lastError().type() == QSqlError::NoError)
        printf("%-50s: OK\n", "Func-QKingbase-Drop-Table");
    else
        printf("%-50s: FAILED\n", "Func-QKingbase-Drop-Table");

    db.close();
    return a.exec();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值