c++ - Sqlite C++ 图像到 Blob 不会存储任何内容

标签 c++ image sqlite blob storage

我在谷歌上搜索了很多,但找不到任何解决方案。我目前正在给自己写一个 Codegear 2007 C++ 中的应用程序。我正在为我的小猫写这个应用程序,它有点像奶制品 但我称之为 KittyBook。

所以我有两个表(抱歉不明白如何使用 CodeBlock):

  • 小猫信息
  • 小猫数据。

KittenInfo 存储他们的姓名和 ID(主键)、性别和出生。这个有效。

另一个应该存储一个 Blob。所以在尝试了很多方法之后。它不会存储在表中,如果我使用正常插入进行查询但不包括 blob 表,则甚至不会存储其他数据。

所以,我不知道我做错了什么,但到目前为止我喜欢 SQLite。那就不回头了吧?

函数是:

void CDatabase::InsertKittenData(int Kitten_ID, int kittenDay, bool DayOrWeek,
    char * kitten_weight, char * Kitten_Comment, string PhotoFile) {

    unsigned char * blob;

    ifstream::pos_type size;
    int size2 = 0;
    if (FileExists(PhotoFile.c_str())) {
        ifstream file(PhotoFile.c_str(), ios::in | ios::binary | ios::ate);
        if (file.is_open()) {
            size = file.tellg();
            blob = new char[size];
            file.seekg(0, ios::beg);
            file.read(blob, size);
            file.close();
        }

    }
    else {
        blob = NULL;
    }
    sqlite3 *dbp;
    sqlite3_stmt *ppStmt;
    // NULL = primary key autoinc.
    char * Sql = "INSERT INTO KittenData VALUES ( NULL, ? , ? ,? , ? , ? , ?);";


    int rc = sqlite3_open("KittyBook.db", &dbp);
    if (rc)
        return;

    if (sqlite3_prepare_v2(dbp, Sql, -1, &ppStmt, NULL) != SQLITE_OK) {
        return;
    }

    if (ppStmt) {
        if (sqlite3_bind_int(ppStmt, 1, Kitten_ID) != SQLITE_OK)
            return;

        if (sqlite3_bind_int(ppStmt, 2, kittenDay) != SQLITE_OK)
            return;
        if (sqlite3_bind_int(ppStmt, 3, DayOrWeek) != SQLITE_OK)
            return;

        if (sqlite3_bind_text(ppStmt, 4, // Index of wildcard
            kitten_weight, strlen(kitten_weight), // length of text
            SQLITE_STATIC) != SQLITE_OK)
            return;
        if (sqlite3_bind_text(ppStmt, 5, // Index of wildcard
            Kitten_Comment, strlen(Kitten_Comment), // length of text
            SQLITE_STATIC) != SQLITE_OK)
            return;

        if (sqlite3_bind_blob(ppStmt, 6, blob, size2, SQLITE_TRANSIENT)
            != SQLITE_OK)
            return;

        if (sqlite3_step(ppStmt) != SQLITE_DONE)
            return;
    }

    sqlite3_finalize(ppStmt);

    sqlite3_exec(dbp, "COMMIT", NULL, NULL, NULL);

    sqlite3_close(dbp);

}

最佳答案

您将 size2 声明并初始化为零:

int size2 = 0;

然后下一次使用 size2 是在绑定(bind) blob 时:

sqlite3_bind_blob(ppStmt, 6, blob, size2, SQLITE_TRANSIENT)

来自fine manual :

In those routines that have a fourth argument, its value is the number of bytes in the parameter. To be clear: the value is the number of bytes in the value, not the number of characters.

在这种情况下,第四个参数是 size2,它是零。结果是您告诉 SQLite 绑定(bind)一个零长度的 blob,然后您想知道为什么没有存储任何内容。好吧,没有任何东西被存储,因为你要求 SQLite 存储零字节并且它只是按照它被告知的去做。

也许您想使用 size 而不是 size2

关于c++ - Sqlite C++ 图像到 Blob 不会存储任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10589509/

相关文章:

c++ - 如何在 C++ 中减少较小字符串中的较大字符串?可能是通过哈希?

html5 canvas 应用程序中的 JavaScript base64 图像源内存管理

php - 如何在图像标签中放置标志状态

image - Azure Blob 图像横向加载

android - 从 sqlite 数据库中搜索一个值并在 ListView 中检索

C++ 默认构造函数 : string params vs string params()

c++ - 在 C++ 中以图形方式为图形绘制顶点

sql - sqlite-WITH子句中的预处理语句导致无限循环

c++ - 如何在没有函数或数组的情况下在 C++ 中创建条形图

ios - 没有模型版本控制或读取 sqlite wal 文件的 CoreData 迁移