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, %d ,?)", nCount, tempname, 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,"select * from t_template_tasks", -1, &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);

你可能感兴趣的:(【DataBase】)