sqlite 二进制 数据的修改

修改 sqlite 中的二进制文件 如下:

1.执行修改语句 update tb set content=? where id=x

2.重新设置参数长度 sqlite3_bind_zeroblob(stmt, 1, len); 第三个参数就是修改后的长度。//bind_zeroblob可以用于几个G的数据写入

3. SQLITE_API int sqlite3_blob_open(
  sqlite3*,                                  //数据库句柄
  const char *zDb,                   //数据库名称
  const char *zTable,              //表名   

  const char *zColumn,          //列名  

        sqlite3_int64 iRow,              //行号

  int flags,                                 //If the flags parameter is non-zero, then the BLOB is opened for read and write access.If it is zero, the BLOB is opened for read access.
  sqlite3_blob **ppBlob        // 二进制文件句柄
    );

  主要目的: 获得需要修改行的文件句柄;


4. sqlite3_blob_write(..) 写入数据; //  在sqlite3_blob_open之后你可以任意次调用sqlite3_blob_write写入数据,只要不超过sqlite3_bind_zeroblob中设定的长度就可以了。


5.sqlite3_blob_close关闭


以上为获取自网上主键,总感觉不如直接删除,然后插入一个新的来方便; 或者在某些不能项里面有用吧!

2.执行修改语句 update tb set content=? where id=x

3.重新设置参数长度 sqlite3_bind_zeroblob(stmt, 1, len); 第三个参数就是修改后的长度。//bind_zeroblob可以用于几个G的数据写入

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C++中更新SQLite3中的二进制数据,您可以使用SQLite3提供的BLOB类型。以下是一个简单的示例代码,它演示如何更新一个名为"my_table"的表中的二进制数据列"my_blob_column": ```c++ #include <sqlite3.h> #include <iostream> #include <fstream> using namespace std; int main() { // Open database sqlite3 *db; int rc = sqlite3_open("my_database.db", &db); if (rc != SQLITE_OK) { cerr << "Error opening database: " << sqlite3_errmsg(db) << endl; sqlite3_close(db); return 1; } // Read binary data from a file ifstream file("my_binary_data.bin", ios::binary | ios::ate); if (!file.is_open()) { cerr << "Error opening file." << endl; sqlite3_close(db); return 1; } streamsize size = file.tellg(); file.seekg(0, ios::beg); char *buffer = new char[size]; if (!file.read(buffer, size)) { cerr << "Error reading file." << endl; delete[] buffer; sqlite3_close(db); return 1; } file.close(); // Update binary data in database sqlite3_stmt *stmt; const char *sql = "UPDATE my_table SET my_blob_column = ? WHERE id = 1;"; rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL); if (rc != SQLITE_OK) { cerr << "Error preparing statement: " << sqlite3_errmsg(db) << endl; delete[] buffer; sqlite3_close(db); return 1; } sqlite3_bind_blob(stmt, 1, buffer, size, SQLITE_TRANSIENT); rc = sqlite3_step(stmt); if (rc != SQLITE_DONE) { cerr << "Error updating data: " << sqlite3_errmsg(db) << endl; delete[] buffer; sqlite3_finalize(stmt); sqlite3_close(db); return 1; } // Cleanup delete[] buffer; sqlite3_finalize(stmt); sqlite3_close(db); return 0; } ``` 在这个示例中,我们首先打开了一个名为"my_database.db"的SQLite3数据库。然后,我们从一个名为"my_binary_data.bin"的文件中读取二进制数据,并将其存储在一个名为"buffer"的字符数组中。接下来,我们准备了一个SQL语句,使用"sqlite3_prepare_v2"函数编译该语句,并使用"sqlite3_bind_blob"函数绑定二进制数据到该语句中。最后,我们使用"sqlite3_step"函数执行该语句并更新数据库中的二进制数据。最后,我们清理并关闭数据库。 请注意,在这个示例中,我们使用了一个名为"SQLITE_TRANSIENT"的特殊标记来告诉SQLite3库,我们正在使用动态分配的内存来存储二进制数据。这意味着SQLite3库将复制这些数据,而不是仅仅在内部保存指向该数据的指针。这可以确保在更新期间,我们的内存不会被释放或修改
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值