博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQLite入门(二)读写二进制数据
阅读量:7199 次
发布时间:2019-06-29

本文共 4357 字,大约阅读时间需要 14 分钟。

//读二进制数据的函数

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;

}

转载于:https://www.cnblogs.com/qinfengxiaoyue/p/3669352.html

你可能感兴趣的文章
查找两个有序数组中的第K个元素(find kth smallest element in 2 sorted arrays)
查看>>
sqlmap注入之tamper绕过WAF防火墙过滤
查看>>
如何用Tacker将NFV带入OpenStack?
查看>>
快速排序简明教程
查看>>
【转】Unity3D学习日记(二)使用UGUI制作虚拟摇杆控制摄像机
查看>>
【BZOJ3110】【Zjoi2013】K大数查询 - 2
查看>>
zabbix安装源
查看>>
java例程练习(多线程的两种创建方式)
查看>>
随题而学(二)多维数组转一维数组
查看>>
创建Dynamic Web Project工程
查看>>
Confirm the Ending
查看>>
day5 python全栈学习笔记
查看>>
ntp网络时间服务搭建
查看>>
php 正则 常用基础
查看>>
选择排序
查看>>
结对-五子棋游戏-设计文档
查看>>
lamp安装
查看>>
ubuntu 上的ruby安装
查看>>
ubuntu 16.04 安装jdk9错误
查看>>
设计模式之单例模式探究
查看>>