我在谷歌上搜索了很多,但找不到任何解决方案。我目前正在给自己写一个 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/