首页 技术 正文
技术 2022年11月7日
0 收藏 945 点赞 853 浏览 4773 个字

//读二进制数据的函数
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;

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,918
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,444
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,255
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,069
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,702
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,741