sqlite3中,blob字段的读取和写入
2017-09-05 20:43
225 查看
本文做演示用的代码,创建了student表格,仅有两列,分别为id,thumbnail,其中id为主键。
创建数据库,打开数据库,创建表格就不多说了,直接从blob字段开始解释。
第一步,update一下blob,将其内容全部值为零。
char sql[125] = "UPDATE student SET thumbnail= :1 WHERE id = :2";
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_zeroblob(stmt, 1, length);
sqlite3_bind_int(stmt, 2, id);
第二步,将文件中的内容读取到blob中。
先调用sqlite3_blob_open()这个函数,
然后分段将文件内容写入,调用sqlite3_blob_write()这个函数。
最后关闭blob,调用sqlite3_blob_close().
第三步, 将blob中的内容读取到文件中。
同样,先打开blob,在读取数据库中的blob,调用sqlite3_blob_read(),写入文件,关闭blob。
源代码
#include "sqlite3.h"
#pragma comment(lib,"sqlite3.lib")
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
int createDB(sqlite3 * db)
{
sqlite3_stmt * stmt = NULL;
char sql[125] = "CREATE TABLE IF NOT EXISTS student(id INTEGER PRIMARY KEY,thumbnail BLOB)";
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK)
{
printf("failed to prepare: %s \n", sqlite3_errmsg(db));
return -1;
}
sqlite3_step(stmt);
sqlite3_finalize(stmt);
return 0;
}
void insert (int id,sqlite3 *db)
{
sqlite3_stmt * stmt = NULL;
char sql[125] = "INSERT INTO student VALUES(:1)";
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_zeroblob(stmt, 1, id);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
int my_FileLength(const char* filename)
{
FILE* fp = NULL;
fopen_s(&fp,filename, "rb");
fseek(fp, 0, SEEK_END);
int length = ftell(fp);
fclose(fp);
return length;
}
void update(sqlite3* db,int id)
{
int length = my_FileLength("example.jpg");
sqlite3_stmt * stmt = NULL;
char sql[125] = "UPDATE student SET thumbnail= :1 WHERE id = :2";
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_zeroblob(stmt, 1, length);
sqlite3_bind_int(stmt, 2, id);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
int my_PutBlob(sqlite3* db,
const char* which_db, // "main"
const char* which_table,
const char* which_column,
int rowid,
const char* filename)
{
sqlite3_b
4000
lob* blob = NULL;
int rc;
// 以READ/WRITE(1)方式打开BLOB
rc = sqlite3_blob_open(db, which_db, which_table, which_column, rowid, 1, &blob);
if (rc != SQLITE_OK)
{
printf("Failed to open BLOB: %s \n", sqlite3_errmsg(db));
return -1;
}
// 打开文件
FILE* fp = NULL;
fopen_s(&fp, filename, "rb");
if (!fp)
{
printf("failed to open file: %s \n", filename);
return -1;
}
// 分段写入
char buf[256];
int offset = 0;
while (!feof(fp))
{
int n = fread(buf, 1, 256, fp);
if (n < 0) break;
if (n == 0) continue;
sqlite3_blob_write(blob, buf, n, offset);
offset += n;
}
// 关闭
fclose(fp);
sqlite3_blob_close(blob);
return 0;
}
int my_GetBlob(sqlite3* db,
const char* which_db, // "main"
const char* which_table,
const char* which_column,
int rowid,
const char* filename)
{
sqlite3_blob* blob = NULL;
int rc;
// 以READONLY(0)方式打开BLOB
rc = sqlite3_blob_open(db, which_db, which_table, which_column, rowid, 0, &blob);
if (rc != SQLITE_OK)
{
printf("Failed to open BLOB: %s \n", sqlite3_errmsg(db));
return -1;
}
// 打开文件
FILE* fp = NULL;
fopen_s(&fp, filename, "wb");
if (!fp)
{
printf("failed to open file: %s \n", filename);
return -1;
}
// 取得BLOB数据长度
int blob_length = sqlite3_blob_bytes(blob);
// 分段读取
char buf[256];
int offset = 0;
while (offset < blob_length)
{
int size = blob_length - offset;
if (size > 256) size = 256;
rc = sqlite3_blob_read(blob, buf, size, offset);
if (rc != SQLITE_OK)
{
printf("failed to read BLOB!\n");
break;
}
fwrite(buf, 1, size, fp);
offset += size;
}
printf("blob length: %d, read %d bytes .\n",
blob_length, offset);
// 关闭
fclose(fp);
sqlite3_blob_close(blob);
return 0;
return 0;
}
int main()
{
sqlite3 * db = NULL;
int rc = sqlite3_open("example.db", &db);
if (rc != 0)
{
printf("创建数据库失败");
}
if (1)
{
createDB(db);
insert(1, db);
update(db, 1);
my_PutBlob(db, "main", "student", "thumbnail", 1, "example.jpg");
}
if (1)
{
my_GetBlob(db, "main", "student", "thumbnail", 1, "get.jpg");
}
sqlite3_close(db);
system("pause");
return 0;
}
创建数据库,打开数据库,创建表格就不多说了,直接从blob字段开始解释。
第一步,update一下blob,将其内容全部值为零。
char sql[125] = "UPDATE student SET thumbnail= :1 WHERE id = :2";
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_zeroblob(stmt, 1, length);
sqlite3_bind_int(stmt, 2, id);
第二步,将文件中的内容读取到blob中。
先调用sqlite3_blob_open()这个函数,
然后分段将文件内容写入,调用sqlite3_blob_write()这个函数。
最后关闭blob,调用sqlite3_blob_close().
第三步, 将blob中的内容读取到文件中。
同样,先打开blob,在读取数据库中的blob,调用sqlite3_blob_read(),写入文件,关闭blob。
源代码
#include "sqlite3.h"
#pragma comment(lib,"sqlite3.lib")
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
int createDB(sqlite3 * db)
{
sqlite3_stmt * stmt = NULL;
char sql[125] = "CREATE TABLE IF NOT EXISTS student(id INTEGER PRIMARY KEY,thumbnail BLOB)";
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK)
{
printf("failed to prepare: %s \n", sqlite3_errmsg(db));
return -1;
}
sqlite3_step(stmt);
sqlite3_finalize(stmt);
return 0;
}
void insert (int id,sqlite3 *db)
{
sqlite3_stmt * stmt = NULL;
char sql[125] = "INSERT INTO student VALUES(:1)";
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_zeroblob(stmt, 1, id);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
int my_FileLength(const char* filename)
{
FILE* fp = NULL;
fopen_s(&fp,filename, "rb");
fseek(fp, 0, SEEK_END);
int length = ftell(fp);
fclose(fp);
return length;
}
void update(sqlite3* db,int id)
{
int length = my_FileLength("example.jpg");
sqlite3_stmt * stmt = NULL;
char sql[125] = "UPDATE student SET thumbnail= :1 WHERE id = :2";
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_zeroblob(stmt, 1, length);
sqlite3_bind_int(stmt, 2, id);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
int my_PutBlob(sqlite3* db,
const char* which_db, // "main"
const char* which_table,
const char* which_column,
int rowid,
const char* filename)
{
sqlite3_b
4000
lob* blob = NULL;
int rc;
// 以READ/WRITE(1)方式打开BLOB
rc = sqlite3_blob_open(db, which_db, which_table, which_column, rowid, 1, &blob);
if (rc != SQLITE_OK)
{
printf("Failed to open BLOB: %s \n", sqlite3_errmsg(db));
return -1;
}
// 打开文件
FILE* fp = NULL;
fopen_s(&fp, filename, "rb");
if (!fp)
{
printf("failed to open file: %s \n", filename);
return -1;
}
// 分段写入
char buf[256];
int offset = 0;
while (!feof(fp))
{
int n = fread(buf, 1, 256, fp);
if (n < 0) break;
if (n == 0) continue;
sqlite3_blob_write(blob, buf, n, offset);
offset += n;
}
// 关闭
fclose(fp);
sqlite3_blob_close(blob);
return 0;
}
int my_GetBlob(sqlite3* db,
const char* which_db, // "main"
const char* which_table,
const char* which_column,
int rowid,
const char* filename)
{
sqlite3_blob* blob = NULL;
int rc;
// 以READONLY(0)方式打开BLOB
rc = sqlite3_blob_open(db, which_db, which_table, which_column, rowid, 0, &blob);
if (rc != SQLITE_OK)
{
printf("Failed to open BLOB: %s \n", sqlite3_errmsg(db));
return -1;
}
// 打开文件
FILE* fp = NULL;
fopen_s(&fp, filename, "wb");
if (!fp)
{
printf("failed to open file: %s \n", filename);
return -1;
}
// 取得BLOB数据长度
int blob_length = sqlite3_blob_bytes(blob);
// 分段读取
char buf[256];
int offset = 0;
while (offset < blob_length)
{
int size = blob_length - offset;
if (size > 256) size = 256;
rc = sqlite3_blob_read(blob, buf, size, offset);
if (rc != SQLITE_OK)
{
printf("failed to read BLOB!\n");
break;
}
fwrite(buf, 1, size, fp);
offset += size;
}
printf("blob length: %d, read %d bytes .\n",
blob_length, offset);
// 关闭
fclose(fp);
sqlite3_blob_close(blob);
return 0;
return 0;
}
int main()
{
sqlite3 * db = NULL;
int rc = sqlite3_open("example.db", &db);
if (rc != 0)
{
printf("创建数据库失败");
}
if (1)
{
createDB(db);
insert(1, db);
update(db, 1);
my_PutBlob(db, "main", "student", "thumbnail", 1, "example.jpg");
}
if (1)
{
my_GetBlob(db, "main", "student", "thumbnail", 1, "get.jpg");
}
sqlite3_close(db);
system("pause");
return 0;
}
相关文章推荐
- C#写入和读取数据库blob字段代码
- C#对Oracle BLOB字段的写入读取方法
- c++写入oracle数据库blob字段,读取blob到本地文件
- C# 读取和写入oracle的blob字段的方法
- c++写入oracle数据库blob字段,读取blob到本地文件
- (一)上传图片至服务器,写入到数据库Blob字段中,以及从数据库读取Blob信息(iframe父子页面传值)
- (二)上传图片至服务器,写入到数据库Blob字段中,以及从数据库读取Blob信息(iframe父子页面传值)
- 上传图片至服务器,写入到数据库Blob字段中,以及从数据库读取Blob信息(iframe父子页面传值)(1)
- 上传图片至服务器,写入到数据库Blob字段中,以及从数据库读取Blob信息(iframe父子页面传值)(2)
- 对oracle中的BLOB的操作(读取,写入)
- BLOB读取写入word文件
- 在oracle 数据库中使用 Blob 字段存储 一张图片并读取
- 将oracle中blob数据写入文件,再把文件读取插入数据库
- JAVA读取Oracle中的blob图片字段并显示
- 写入到blob字段的存储过程
- Extjs2.0+gridPanel+checkbox读取从数据库中写入到xml文件的字段
- Oracle中,将VARchar2类型的字符串写入BLOB类型的字段中
- Oracle中Clob与Blob字段的读取
- 深入浅析mybatis oracle BLOB类型字段保存与读取
- 谈谈有关各数据库Blob、Clob类型字段的读取、保存更新