#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();
}
QKingbase的批量操作
最新推荐文章于 2024-04-23 08:00:00 发布