#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");
db.setDatabaseName("TEST");
db.setUserName("SYSTEM");
db.setPassword("MANAGER");
db.open();
db.exec("set client_encoding to 'gbk'");
db.exec("drop table PERFORMANCE_COLLECTION");
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");
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");
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");
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");
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");
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");
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;
}
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;
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();
}