#include <stdio.h>
#include <string.h>
#include "sqlite3.h"

#pragma comment(lib,"SQLite3.lib")

sqlite3 *db;

typedef struct book_entry_t book_entry_t ;
struct book_entry_t 
{
    unsigned __int64 key ;
    unsigned __int16 move ;
    __int32 score ;
    __int32 win_count ;
    __int32 draw_count ;
    __int32 lost_count ;
    unsigned __int16 valid ;
    char comments[64];
};

int GetBookEntry(book_entry_t *entry,unsigned __int64 key)
{    
    int result ;
    char *errmsg=NULL ;
    char **dbResult ;
    int nRow,nColumn ;
    int i,j ;
    int index ;
	char szSql[1024]={0}; 
	_snprintf(szSql,1024,"select * from bhobk where vkey=%I64u;",key);
    result=sqlite3_get_table(db,szSql,&dbResult,&nRow,&nColumn,&errmsg);
    if(SQLITE_OK==result)
    {
        index=nColumn ;
        for(i=0;i<nRow;i++)
        {
            if(i>127) break ;
            for(j=0;j<nColumn;j++)
            {
                if(j==2)entry[i].move=atoi(dbResult[index]);
                else if(j==3)entry[i].score=atoi(dbResult[index]);
                else if(j==4)entry[i].win_count=atoi(dbResult[index]);
                else if(j==5)entry[i].draw_count=atoi(dbResult[index]);
                else if(j==6)entry[i].lost_count=atoi(dbResult[index]);
                else if(j==7)entry[i].valid=atoi(dbResult[index]);
                else if(j==8)strncpy(entry[i].comments,dbResult[index],64);
                index++;
            }
        }
    }
    sqlite3_free_table(dbResult);
    
    return nRow>128?128:nRow ;
}

int main(int argc,char **argv)
{
	//某个局面开局库棋步总数
	int nCount;
	//记录某个局面开局库数据
	book_entry_t entry[128]={0}; 
	//这里的key是某个局面的ZobrisKey,采用无符号64位整数,具体计算方法见说明文件,这里给定常数作为实例
	unsigned __int64 key=15265892774299731920;

    int result=sqlite3_open("c:\\test.obk",&db);
    if(result!=SQLITE_OK)
    {
        return 1;
    }

    nCount=GetBookEntry(entry,key);

	sqlite3_close(db);
    return 0 ;
}