//读二进制数据的函数
BOOL OpenBinDataFile(BYTE **pBUf,UINT &len)
{
    if (pBUf == NULL)
    {
        return FALSE;
    }

    std::ifstream fs;
    fs.open(g_szBinDataPath,ios::binary);
    if (!fs.is_open())
    {
        cout<<"File:"<<g_szBinDataPath<<"not Open"<<endl;
        return FALSE;
    }

    fs.seekg(0,ios::end);
    len = fs.tellg();
    fs.seekg(0,ios::beg);

    *pBUf = new BYTE[len];
    if (NULL == *pBUf)
    {
        cout<<"内存分配不足"<<endl;
        return FALSE;
    }

    fs.read((char*)(*pBUf),len);

    fs.close();
    return TRUE;

}

//#define _USE_TRANSACTION //是否使用事务
#define _DB_TABLE_COUNTS 10 //100张表
#define LOOPSCOUNT 20 //记录数
#define TABLEFLAG 10 //第十张表写入1000个记录
#define RESETEST
BOOL SqlitePerfWithUnicode()//Unicode
{

    cout<<"UNICODE TEST:"<<endl;

    BYTE *pBuf=NULL;
    UINT len = 0;
    if (!OpenBinDataFile(&pBuf,len))
    {
        cout<<"打开二进制数据失败"<<endl;
        if (pBuf)
        {
            delete []pBuf;
        }
        return FALSE;
    }

    sqlite3 *db = NULL;
    TCHAR *errMsg = NULL;
    sqlite3_stmt *pstmt=NULL;
    TCHAR *psql = NULL;

    int nRet = -1,nRows=-1;
    nRet = sqlite3_open16(L"F:\\UnicodeTest.db",&db);
    if (nRet)
    {
        wcout<<L"无法打开UnicodeTest数据库:"<<sqlite3_errmsg16(db)<<endl;
        //sqlite3_free(errMsg);
        sqlite3_close(db);
        cin.get();

        return 1;
    }
    else
    {
        wcout<<L"成功打开UnicodeTest.db"<<endl;

    }

    TCHAR szNum[100]={0};
    TCHAR table[100]={0};
    //std::vector<std::wstring> vecTableNames;
    std::vector<std::wstring> vecID;
    std::wstring wst;

    std::vector<std::wstring> vecInsertStr,vecReadStr;
    vecInsertStr.reserve(_DB_TABLE_COUNTS);
    vecReadStr.reserve(_DB_TABLE_COUNTS);

    for (int j=0;j<LOOPSCOUNT*10;++j)
    {
        _itot(j,szNum,10);
        vecID.push_back(szNum);
    }
    for (int i=0;i<_DB_TABLE_COUNTS;++i)//建立100张表
    {
        _itot(i,szNum,10);
        //vecID.push_back(szNum);//不够
        wst = L"unicodetest_";
        wst += szNum;//表命名:unicodetest_x
        //vecTableNames.push_back(wst);
        psql = L"create table if not exists %s(id nchar(20) primary key,name blob)";
        _snwprintf_s(table,_countof(table),_TRUNCATE,psql,wst.c_str());
        //创建 _DB_TABLE_COUNTS 张表
        psql = table;
        sqlite3_prepare16(db,psql,-1,&pstmt,NULL);
        sqlite3_step(pstmt);
        sqlite3_reset(pstmt);

        psql = L"insert into %s values(?,?);";
        _snwprintf_s(table,_countof(table),_TRUNCATE,psql,wst.c_str());
        //psql = table;
        vecInsertStr.push_back(table);

        psql = L"select id,name from %s;";
        _snwprintf_s(table,_countof(table),_TRUNCATE,psql,wst.c_str());
        //psql = table;
        vecReadStr.push_back(table);

    }
   
   

    //psql = L"create table if not exists unicodetest(id nchar(20) primary key,name blob)";
    //sqlite3_prepare16(db,psql,-1,&pstmt,NULL);
    //sqlite3_step(pstmt);
    //sqlite3_reset(pstmt);
   

    DWORD dwBegin = GetTickCount();
#ifdef _USE_TRANSACTION
    sqlite3_exec(db,"begin transaction;",NULL,NULL,NULL);
#endif
    for(int j=1;j<_DB_TABLE_COUNTS;++j)
    {
        psql = const_cast<TCHAR*>(vecInsertStr[j].c_str());
#ifdef RESETEST
        sqlite3_prepare16(db,psql,-1,&pstmt,NULL);
#endif
        for (int i=0;i<LOOPSCOUNT;++i)//写
        {
#ifndef RESETEST
            sqlite3_prepare16(db,psql,-1,&pstmt,NULL);
#endif
           
            sqlite3_bind_text16(pstmt,1,vecID[i].c_str(),-1,NULL);
            sqlite3_bind_blob(pstmt,2,pBuf,len,NULL);
            sqlite3_step(pstmt);
#ifdef RESETEST
            sqlite3_reset(pstmt);
#else
            sqlite3_finalize(pstmt);
#endif

                   
        }

    }

    psql = const_cast<TCHAR*>(vecInsertStr[0].c_str());//id为"0"的表写入1000个记录
    for (int i=0;i<LOOPSCOUNT*10;++i)//写
    {
        sqlite3_prepare16(db,psql,-1,&pstmt,NULL);
        sqlite3_bind_text16(pstmt,1,vecID[i].c_str(),-1,NULL);//这里也相应的扩大
        sqlite3_bind_blob(pstmt,2,pBuf,len,NULL);
        sqlite3_step(pstmt);

        sqlite3_reset(pstmt);       
    }

#ifdef _USE_TRANSACTION
    sqlite3_exec(db,"commit transaction;",NULL,NULL,NULL);
#endif
    DWORD dwEnd = GetTickCount();

    cout<<"共有 "<< _DB_TABLE_COUNTS <<" 张表,每张表"<<"插入 "<<LOOPSCOUNT<<" 份记录(每份大小["<<len<<"B].)"<<endl;       
    cout<<"其中最后一张表插入"<<LOOPSCOUNT*10<<"记录"<<endl;
    cout<<"总耗时:"<<dwEnd-dwBegin<<endl;

   
    dwBegin = GetTickCount();
#ifdef _USE_TRANSACTION
    sqlite3_exec(db,"begin transaction;",NULL,NULL,NULL);
#endif
    for (int i=0;i<_DB_TABLE_COUNTS;++i)//读
    {       
        sqlite3_prepare16(db,vecReadStr[i].c_str(),-1,&pstmt,NULL);
        nRet = sqlite3_step(pstmt);
        while(nRet == SQLITE_ROW)
        {
            sqlite3_column_text16(pstmt,0);
            sqlite3_column_blob(pstmt,1);//数据先不读出
            nRet = sqlite3_step(pstmt);
        }
        sqlite3_reset(pstmt);
    }
#ifdef _USE_TRANSACTION
    sqlite3_exec(db,"commit transaction;",NULL,NULL,NULL);
#endif
    dwEnd = GetTickCount();

    cout<<"依次读 "<<_DB_TABLE_COUNTS<<" 张表内的 "<<LOOPSCOUNT<<"份记录,总耗时:"<<dwEnd-dwBegin<<endl;

    sqlite3_reset(pstmt);   
    sqlite3_finalize(pstmt);
    sqlite3_close(db);
    if (pBuf)
    {
        delete []pBuf;
    }

    //cin.get();
    return TRUE;

}

SQLite入门(二)读写二进制数据的更多相关文章

  1. IO流-文本IO\读写二进制数据

    文本IO 一.简述 OutputStreamWriter类使用选定的编码方式吧Unicode字符流转换为字节流,InputStreamReader类将包含字节的输入流转为可以产生Unicode字符的读 ...

  2. DevExpress XtraReports 入门二 创建 data-aware(数据感知) 报表

    原文:DevExpress XtraReports 入门二 创建 data-aware(数据感知) 报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的, ...

  3. 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据

    [源码下载] 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 读写文本数 ...

  4. Java读写二进制数据

    import java.io.*; import java.time.LocalDate; public class Test { public static void main(String[] a ...

  5. echart图表控件配置入门(二)常用图表数据动态绑定

    上一节 <echart图表控件配置入门(一)>介绍了echarts图表控件的入门配置,使开发人员可以快速搭建出一个静态的图表.但是在实际开发过程这还是不够的,不可能所有的图表控件都是静态数 ...

  6. Qt里怎么处理二进制数据

    Qt里有个专门的类QDataStream就是专门读写二进制数据的, 它与QByteArray搭配在网络编程中有奇效. 来个栗子: // write data QByteArray data; QDat ...

  7. 重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表

    原文:重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表 [源码下载] 重新想象 Windows 8 S ...

  8. SQLite 入门教程(二)创建、修改、删除表 (转)

    转于 SQLite 入门教程(二)创建.修改.删除表 一.数据库定义语言 DDL 在关系型数据库中,数据库中的表 Table.视图 View.索引 Index.关系 Relationship 和触发器 ...

  9. SQLite入门与分析(二)---设计与概念(续)

    SQLite入门与分析(二)---设计与概念(续)   写在前面:本节讨论事务,事务是DBMS最核心的技术之一.在计算机科学史上,有三位科学家因在数据库领域的成就而获ACM图灵奖,而其中之一Jim G ...

随机推荐

  1. JS中将XML转为JSON对象

    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script> <scr ...

  2. Comparación para 2019 Nueva Lonsdor K518S y K518ISE

    Comparación para 2019 Nueva Lonsdor K518S y Lonsdor K518ISE: Igual: Capacidades de Immo y cobertura ...

  3. 初识iVew table表属性

    <template> <Table :row-class-name="rowClassName" :columns="columns1" :d ...

  4. 微信公共平台注册 bug: 验证码不应该输入后,就立即检查其有效性

    本文链接: https://www.cnblogs.com/hchengmx/p/10793037.html 刚刚想注册个微信公众号,就发现了这个问题,在这里记录一下. 已经发到testhome了,链 ...

  5. 关于sql通配符检索问题-【.NET】

    确定给定的字符串是否与指定的模式匹配.模式可以包含常规字符和通配符字符.模式匹配过程中,常规字符必须与字符串中指定的字符完全匹配.然而,可使用字符串的任意片段匹配通配符.与使用 = 和 != 字符串比 ...

  6. 转 Hibernate中cascade和inverse的作用

    Inverse和cascade是Hibernate映射中最难掌握的两个属性.两者都在对象的关联操作中发挥作用.1.明确inverse和cascade的作用inverse 决定是否把对对象中集合的改动反 ...

  7. hibernate抓取问题

    当使用xml配置类之间的关系时 ,例如 学生 班级,多对一关系 /** * 默认情况会发出2条SQL语句,一条取student,一条取Classroom,其实这只需要一条sql             ...

  8. Java中break、continue及标签等跳转语句的使用[下]

    作为上一篇使用for循环演示的跳转,这一篇将使用while.相比较来说,while比for循环更简单.代码如下: public class LabeledWhile { public static v ...

  9. [转]angularjs之ui-grid 使用详解

    本文转自:http://blog.csdn.net/qhkabuqiluo/article/details/52237710 最近一段时间在使用angularjs 然后就找到ui-grid 这个比较不 ...

  10. 另一个C#模拟post请求的例子

    private string returninstallTmnl(AddTmnlInstallParameter model) { string url = ConfigurationSettings ...