#define SELECT_FROM_SQL "select * from video_record limit ? offset ?" //limit 范围 offset 超时
History_Video_Record_Node* HistoryManager::getRecords(int startIndex,int endIndex)
{
//releaseNode(videoRecordHead);
int rc;
int offset = 0;
int ncols = 0;
History_Video_Record_Node* temp = NULL;
History_Video_Record_Node* next = NULL;
offset = startIndex -1 < 0 ? 0 : startIndex -1;
sqlite3_stmt* stmt = NULL;
const char* tail = NULL;
rc = sqlite3_prepare(db,SELECT_FROM_SQL,strlen(SELECT_FROM_SQL),&stmt,&tail); //
if(rc != SQLITE_OK)
return NULL;
rc = sqlite3_bind_int(stmt,1,endIndex - startIndex + 1 > 0 ? endIndex - startIndex + 1 : 0);//limit
if(rc != SQLITE_OK)
return NULL;
rc = sqlite3_bind_int(stmt,2,offset); //offset 绑定数据?
if(rc != SQLITE_OK)
return NULL;
rc = sqlite3_step(stmt); //保存到结构stmt中
if(rc == SQLITE_ROW)
{
printf("exec sql:%s ========>success\n",SELECT_FROM_SQL);
}
releaseNode(videoRecordHead);
ncols = sqlite3_column_count(stmt);
if(ncols <= 0)
return NULL;
printf("HERE %d\n",ncols);
while(rc == SQLITE_ROW) //构建History_video_Record_Node 链表
{
temp = new History_Video_Record_Node;
memset(temp,0,sizeof(History_Video_Record_Node));
if(next)
{
next->next = temp;
}
next = temp;
if(!videoRecordHead)
videoRecordHead = temp;
for(int i = 0 ; i < ncols ; i++)
{
if(strcasecmp(sqlite3_column_name(stmt,i),"id") == 0)//定位到id列
{
if(sqlite3_column_text(stmt,i))//获取id的值
{
memcpy(temp->id,(const char*)sqlite3_column_text(stmt,i),strlen((const char*)sqlite3_column_text(stmt,i)));
}
}
if(strcasecmp(sqlite3_column_name(stmt,i),"name") == 0)
{
if(sqlite3_column_text(stmt,i))
{
memcpy(temp->name,(constchar*)sqlite3_column_text(stmt,i),strlen((const char*)sqlite3_column_text(stmt,i)));
}
}
if(strcasecmp(sqlite3_column_name(stmt,i),"url") == 0)
{
if(sqlite3_column_text(stmt,i))
{
memcpy(temp->url,(const char*)sqlite3_column_text(stmt,i),strlen((const char*)sqlite3_column_text(stmt,i)));
}
}
if(strcasecmp(sqlite3_column_name(stmt,i),"time") == 0)
{
if(sqlite3_column_text(stmt,i))
{
memcpy(temp->time,(const char*)sqlite3_column_text(stmt,i),strlen((const char*)sqlite3_column_text(stmt,i)));
}
}
}
rc = sqlite3_step(stmt);
}
sqlite3_finalize(stmt); //析构stmt
return videoRecordHead; //返回链表头结点
|