加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQLITE BLOB二进制文件读取

发布时间:2020-12-12 20:24:37 所属栏目:百科 来源:网络整理
导读: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(

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:EmsServerbinEMS_DB.db",&m_SqliteDB.mpDB);
if( rc != SQLITE_OK )
{
fprintf(stderr,"Can't open database: %sn",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 | %sn",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,%dn",id,name,time );


result2 = sqlite3_step( stat2 );
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读