欢迎使用金字塔普通技术服务论坛,您可以在相关区域发表技术支持贴。
我司技术服务人员将优先处理 VIP客服论坛 服务贴,普通区问题处理速度慢,请耐心等待。谢谢您对我们的支持与理解。


金字塔客服中心 - 专业程序化交易软件提供商教学与活动专区期货人生 → [原创]Text,Binary,Database

   

欢迎使用金字塔普通技术服务论坛,您可以在相关区域发表技术支持贴。
我司技术服务人员将优先处理 VIP客服论坛 服务贴,普通区问题处理速度慢,请耐心等待。谢谢您对我们的支持与理解。    


  共有2294人关注过本帖树形打印复制链接

主题:[原创]Text,Binary,Database

帅哥哟,离线,有人找我吗?
z7c9
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:小飞侠 帖子:1882 积分:3310 威望:0 精华:15 注册:2010/3/15 13:11:56
[原创]Text,Binary,Database  发帖心情 Post By:2013/8/5 15:54:39 [显示全部帖子]

#include <QCoreApplication>
#include <QDateTime>
#include <QDebug>
#include <QDataStream>
#include <QFile>
#include <QList>
#include <QStringList>
#include <QTime>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>

int size = 999;

class Bar
{
private:
    double open;
    double high;
    double low;
    double close;
public:
    Bar(double open=0,double high=0,double low=0,double close=0);
    friend QDataStream& operator <<(QDataStream &out,Bar &bar);
    friend QDataStream& operator >>(QDataStream &in,Bar &bar);
    friend QTextStream& operator <<(QTextStream &out,Bar &bar);
    friend QTextStream& operator >>(QTextStream &in,Bar &bar);
    friend void writeDatabase();
    friend void readBinary();
    void print();
};

Bar::Bar(double open, double high, double low, double close):open(open),high(high),low(low),close(close)
{
}

QDataStream& operator <<(QDataStream &out,Bar &bar)
{
    out << bar.open << bar.high << bar.low << bar.close;
    return out;
}

QDataStream& operator >>(QDataStream &in,Bar &bar)
{
    in >> bar.open >> bar.high >> bar.low >> bar.close;
    return in;
}

QTextStream& operator <<(QTextStream &out,Bar &bar)
{
    out << bar.open << "," << bar.high << "," << bar.low << "," << bar.close << endl;
    return out;
}

QTextStream& operator >>(QTextStream &in,Bar &bar)
{
    QStringList line= in.readLine().split(",");
    bar.open = line.at(0).toDouble();
    bar.high = line.at(1).toDouble();
    bar.low = line.at(2).toDouble();
    bar.close = line.at(3).toDouble();
    return in;
}

void Bar::print()
{
    qDebug() << open << high << low << close;
}

void writeText()
{
    QFile file("E:\\mytest.csv");
    file.open(QIODevice::WriteOnly);
    QTextStream out(&file);

    for(int i=0;i<size;++i)
    {
        Bar bar(i,i+1,i+2,i+3);
        out << bar;
    }

    file.close();
}

void writeBinary()
{
    QFile file("E:\\mytest.dat");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);

    for(int i=0;i<size;++i)
    {
        Bar bar(i,i+1,i+2,i+3);
        out << bar;
    }

    file.close();
}

void writeDatabase()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("E:\\mytest.db");
    db.open();

    QSqlQuery query;
    db.transaction();
    query.exec("create table bars(open double,high double,low double,close double)");

    for(int i=0;i<size;++i)
    {
        Bar bar(i,i+1,i+2,i+3);

        query.prepare("insert into bars(open,high,low,close) values(:open,:high,:low,:close)");
        query.bindValue(":open",bar.open);
        query.bindValue(":high",bar.high);
        query.bindValue(":low",bar.low);
        query.bindValue(":close",bar.close);

        query.exec();
    }
    db.commit();
}

void readText()
{
    QFile file("E:\\mytest.csv");
    file.open(QIODevice::ReadOnly);
    QTextStream in(&file);

    while(!in.atEnd())
    {
        Bar bar;
        in >> bar;
//        bar.print();
    }
}

void readBinary()
{
    QFile file("E:\\mytest.dat");
    file.open(QIODevice::ReadOnly);
    QDataStream in(&file);

    while(!in.atEnd())
    {
        Bar bar;
        in >> bar;
//        bar.print();
    }
}

void readDatabase()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("E:\\mytest.db");
    db.open();

    QSqlQuery query;
    query.exec("select open,high,low,close from bars");

    while(query.next())
    {
        Bar bar(query.value(0).toDouble(),query.value(1).toDouble(),query.value(2).toDouble(),query.value(3).toDouble());
//        bar.print();
    }
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QTime time;
    time.start();

//    writeText();
//    writeBinary();
//    writeDatabase();

//    readText();
//    readBinary();
    readDatabase();

    qDebug() << time.elapsed();
   
    return a.exec();
}

[此贴子已经被作者于2013/8/5 15:55:10编辑过]

 回到顶部