在访问了数十个包含有关 SQLite 信息的网站后,我仍然找不到在绑定 blob 时修复错误的解决方案。这是表声明:
CREATE TABLE ONE (
ID INTEGER PRIMARY KEY AUTOINCREMENT
NOT NULL,
NAME CHAR( 50 ) NOT NULL,
LABEL CHAR( 50 ),
GRP CHAR( 50 ),
FILE BLOB
);
这是插入的代码:
int InsertFile(string name)
{
const char* dbname = name.c_str();
sqlite3 *database;
int rc = sqlite3_open(dbname, &database);
char *zErrMsg = 0;
unsigned char *buffer = (unsigned char*) malloc(sizeof(char)*MAX);
ifstream file;
file.open("Sql.pdf", ios::in|ios::binary);
if ( ! file )
{
cout << "An error occurred opening the file" << endl;
}
int count = 0;
const void* fileptr = NULL;
fileptr = buffer;
while(file.good())
{
char c=file.get();
buffer[count]=c;
count++;
}
file.close();
sqlite3_stmt *stmt = NULL;
char* statement = "INSERT INTO ONE( ID, NAME, LABEL, GRP, FILE ) VALUES ( NULL, 'fedfsdfss', NULL, NULL, ?);";
rc = sqlite3_prepare_v2(database, statement, 0, &stmt, NULL);
rc = sqlite3_bind_blob(stmt, 1, fileptr, sizeof(char)*count, SQLITE_TRANSIENT);
const char* result = sqlite3_errmsg(database);
rc = sqlite3_step(stmt);
result = sqlite3_errmsg(database);
sqlite3_close(database);
free(buffer);
fileptr=NULL;
return 0;
编辑:粘贴完整功能,我试图插入的字符数量约为 350K。
binb_blob 的结果始终为 21,错误代码不包含任何内容。缓冲区包含二进制文件数据,这很可能不是太大,因此是错误代码。任何提示都会受到重视。