sqlite3数据库怎么存储数组类型
1个回答
展开全部
/*思路是把数组保存成blob格式
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
/*#define DEBUG*/
int main(int argc, char *argv[]) {
int a[10] = {1,3,8,4,6,6,7,8,9};
int len;
len = sizeof(a[0])*10;
#ifdef DEBUG
printf("%d\n",len);
#endif
sqlite3 *db;
sqlite3_stmt *stmt;
char *sql,*zErr;
int rc;
rc = sqlite3_open_v2(":memory:",&db,SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE,NULL);
if(rc){
fprintf(stderr,"%s\n",sqlite3_errmsg(db));
return rc;
}
rc = sqlite3_exec(db,"create table if not exists test(array blob);",NULL,NULL,&zErr);
if(rc != SQLITE_OK){
fprintf(stderr,"%s\n",zErr);
sqlite3_free(zErr);
sqlite3_close(db);
return rc;
}
sql = "insert into test values(?);";
sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
sqlite3_bind_blob(stmt,1,a,len,SQLITE_STATIC);
sqlite3_step(stmt);
sqlite3_reset(stmt);
sql = "select * from test;";
sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
sqlite3_step(stmt);
len = sqlite3_column_bytes(stmt,0);
int *data = malloc(len);
memcpy(data,sqlite3_column_blob(stmt,0),len);
sqlite3_finalize(stmt);
sqlite3_close(db);
#ifdef DEBUG
printf("%d\n",sizeof(a[0]));
#endif
int i = 0;
while(i<(len/sizeof(a[0]))){
printf("a=%d, data=%d\n",a[i],*(data+i));
i++;
}
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
/*#define DEBUG*/
int main(int argc, char *argv[]) {
int a[10] = {1,3,8,4,6,6,7,8,9};
int len;
len = sizeof(a[0])*10;
#ifdef DEBUG
printf("%d\n",len);
#endif
sqlite3 *db;
sqlite3_stmt *stmt;
char *sql,*zErr;
int rc;
rc = sqlite3_open_v2(":memory:",&db,SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE,NULL);
if(rc){
fprintf(stderr,"%s\n",sqlite3_errmsg(db));
return rc;
}
rc = sqlite3_exec(db,"create table if not exists test(array blob);",NULL,NULL,&zErr);
if(rc != SQLITE_OK){
fprintf(stderr,"%s\n",zErr);
sqlite3_free(zErr);
sqlite3_close(db);
return rc;
}
sql = "insert into test values(?);";
sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
sqlite3_bind_blob(stmt,1,a,len,SQLITE_STATIC);
sqlite3_step(stmt);
sqlite3_reset(stmt);
sql = "select * from test;";
sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
sqlite3_step(stmt);
len = sqlite3_column_bytes(stmt,0);
int *data = malloc(len);
memcpy(data,sqlite3_column_blob(stmt,0),len);
sqlite3_finalize(stmt);
sqlite3_close(db);
#ifdef DEBUG
printf("%d\n",sizeof(a[0]));
#endif
int i = 0;
while(i<(len/sizeof(a[0]))){
printf("a=%d, data=%d\n",a[i],*(data+i));
i++;
}
system("pause");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询