SQLite Forum

how to insert a structure data into blob?
Login
I am newer to  sqlite3, now i want insert a struct data into table,

char* sRadar1 = "CREATE TABLE IF NOT EXISTS radar_1 ("\
     "FrmNo         INT       PRIMARY KEY     NOT NULL," \
     "CapTime       DOUBLE                    NOT NULL," \
     "Data          BLOB," \
     "CamTime       DOUBLE," \
     "Mark          INT );";

typedef struct {
	int ID;           /* target's ID */
	float rx;         /* range of x  */ 
	float ry;         /* range of y  */
	float vx;         /* velocity of x  */ 
	float vy;         /* velocity of y  */
	float ax;         /* acceleration of x  */ 
	float ay;         /* acceleration of y  */
	float PoE;        /* target's probability of existence */ 
	float length;     /* target's length  */
	float width;      /* target's width   */
 	float height;     /* target's height  */
	int   type;       /* target's type    */
	double life;      /* target's life    */
}hh_RadarTarget_t;


hh_RadarTarget_t  mydata;
char sql[200];
char *errmsg = NULL;
sqlite3_stmt *stmt; 
int ret = 0;


mydata.id = 100;

sqlite3_exec(db, sRadar1, NULL, 0, &zErrMsg); // create table 

sprintf(sql, "INSERT INTO radar_1(FrmNo, CapTime,CamTime) VALUES(%d,%lf,%lf)", 1, 123.1,456.1);
sqlite3_exec(db,sql, NULL, NULL, &errmsg);
ret = sqlite3_prepare_v2(db, "INSERT INTO radar_1(Data) VALUES(?)", -1, &stmt, NULL);
if(SQLITE_OK!= ret || !stmt)
{
   printf("sqlite3_prepare_v2 failed,ret = %d\n",ret);
}

sqlite3_bind_blob(stmt,1, mydata, sizeof(hh_RadarTarget_t), NULL);
if(SQLITE_OK!= ret || !stmt)
{
   printf("sqlite3_bind_blob failed,ret = %d\n",ret);
 }


sqlite3_step(stmt);  
if((SQLITE_DONE!= ret) && (SQLITE_ROW!= ret))
{
   printf("sqlite3_step failed,ret = %d \n",ret);
}

                                           
sqlite3_finalize(stmt);





It can not work, why?
Any help will be apprecicated.