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

SQLite存取二进制数据

发布时间:2020-12-12 20:17:35 所属栏目:百科 来源:网络整理
导读:程序功能: 使用SQLite的C API操作SQLite数据库,存取二进制数据. 用来测试的文件都小于65535字节,没有测试更大的文件! 参考文档: http://www.cntxk.com/CataNews/56/info8106.html 编译环境: codeblock 10.05(svn 6906) */ #include stdio.h #include me
程序功能: 使用SQLite的C API操作SQLite数据库,存取二进制数据.
用来测试的文件都小于65535字节,没有测试更大的文件!
参考文档: http://www.cntxk.com/CataNews/56/info8106.html
编译环境: codeblock 10.05(svn 6906)
*/
#include <stdio.h>
#include <memory.h>
#include <sqlite3.h>

int main(int argc,char *argv[])
{
sqlite3* pDB=NULL;
char* pErrMsg;

if(SQLITE_OK==sqlite3_open("./mydb.db",&pDB))
{
sqlite3_exec(pDB,"create table bList(fileName varchar(16) primary key,binData blob);",NULL,&pErrMsg);
{
char buffer[65535];
int iLen=0;
sqlite3_stmt* stmt;

{//存储二进制数据
unsigned char hex[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
sqlite3_prepare(pDB,"insert into bList values ('hex',?);",-1,&stmt,NULL);
sqlite3_bind_blob(stmt,hex,16,NULL);
sqlite3_step(stmt);
}

stmt=NULL;
{//存储exe文件
memset(buffer,65535);
{
FILE* fp=fopen(argv[0],"rb");
iLen=fread(buffer,65535,fp);
fclose(fp);
}
sqlite3_prepare(pDB,"insert into bList values ('me.exe',buffer,iLen,NULL);
sqlite3_step(stmt);
}

stmt=NULL;
{//存储普通c文档
memset(buffer,65535);
{
FILE* fp=fopen("../main.c","insert into bList values ('main.txt',NULL);
sqlite3_step(stmt);
}
stmt=NULL;
{//从数据库中读取txt文件数据
char *data=NULL;
memset(buffer,65535);
sqlite3_prepare(pDB,"select binData from bList where fileName='main.txt';",0);
sqlite3_step(stmt);
data= (char *)sqlite3_column_blob(stmt,0);//得到纪录中的BLOB字段
iLen= sqlite3_column_bytes(stmt,0);//得到字段中数据的长度
memmove(buffer,data,iLen);
printf("%sn",buffer);
}
}
sqlite3_close(pDB);
}

{
puts("Press any key to exit...");
getchar();
}
return 0;
}


From:

http://hi.baidu.com/ejoywx/blog/item/4d7b418c8677cc00b31bbae9.html

(编辑:李大同)

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

    推荐文章
      热点阅读