SQLITE BLOB二进制文件读取

前端之家收集整理的这篇文章主要介绍了SQLITE BLOB二进制文件读取前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JMutexAutoLock autolock( &dbJMutex );

CString strTemp ;

//1.连接到数据库
//2.添加数据
//3.关闭数据库

try
{
time_t t ;
time(&t);
INT64 n64time = t;

char **dbResult;
char *errmsg;
int nRow,nColumn;
int index = 0;
int i,j,rc;

rc = sqlite3_open("E:\\EmsServer\\bin\\EMS_DB.db",&m_sqliteDB.mpDB);
if( rc != sqlITE_OK )
{
fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(m_sqliteDB.mpDB));
sqlite3_close(m_sqliteDB.mpDB);
exit(1);
}

rc = sqlite3_get_table( m_sqliteDB.mpDB,"select * from t_template_tasks",&dbResult,&nRow,&nColumn,&errmsg);

if (rc == sqlITE_OK)
{
printf("表格共%d 记录!\n",nRow);
printf("表格共%d 列!\n",nColumn);
// 前两个字段为字段名 field0,field1,row[0][0],row[0][1],row[1][0],row[1][1] ... ... ....
// 是一维数组,不是二维数组,反正记着第0,第1列的值为字段名,然后才是字段值;
printf( "字段名|字段值\n");
printf( "%s | %s\n",dbResult[0],dbResult[1]);
printf("--------------------------------\n");
index = nColumn; //字段值从index开始呀
for( i = 0; i < nRow ; i++ )
{
for( j = 0 ; j < nColumn; j++ )
{
printf( "%-5s ",dbResult[index++]);
}
printf("\n");
}
printf("--------------------------------\n");
}

int nCount = m_sqliteDB.execScalar(_T("select max(tempid) from t_template_tasks") );
nCount += 1;
strTemp.Format(" insert into t_template_tasks(tempid,tempname,tasktime,usedcount,taskinfo) values( %d,'%s',%d,?)",nCount,n64time,0);

//存入
sqlite3_stmt * stat;
sqlite3_prepare( m_sqliteDB.mpDB,strTemp,-1,&stat,0 );

sqlite3_bind_blob( stat,1,taskinfo,taskinfolen,NULL );

int result = sqlite3_step( stat );

sqlite3_finalize( stat ); //把刚才分配的内容析构掉

//取出

sqlite3_stmt * stat2;
sqlite3_prepare( m_sqliteDB.mpDB,&stat2,0 );

int result2 = sqlite3_step( stat2 );

//此处取出
while ( result2 == sqlITE_ROW )
{

int id = sqlite3_column_int( stat2,0 );
const unsigned char *name = sqlite3_column_text( stat2,1 );
int time = sqlite3_column_int( stat2,2 );
int count = sqlite3_column_int( stat2,3 );
const void * pFileContent = sqlite3_column_blob( stat2,4 );

int len = sqlite3_column_bytes( stat2,4 );

printf(" %d,%s,%d\n",id,name,time );


result2 = sqlite3_step( stat2 );
}

sqlite3_reset(stat2); sqlite3_finalize( stat2 ); //把刚才分配的内容析构掉 sqlite3_close(m_sqliteDB.mpDB);

原文链接:https://www.f2er.com/sqlite/201675.html

猜你在找的Sqlite相关文章