简体   繁体   English

在sqlite db中插入blob(objective-c)

[英]insert blob in sqlite db (objective-c)

i got a problem with inserting a blob into a sqlite db. 我遇到了将blob插入sqlite db的问题。 I'm workin on ios so it's objective-c. 我正在研究ios,所以它的目标是c。

the plan: 计划:

i'm pressing a button, a photo picker opens, i'm choosing a photo, im touching another button, the photo is saved in the db as a blob. 我按下一个按钮,一个照片选择器打开,我正在选择一张照片,我正在触摸另一个按钮,照片作为一个blob保存在数据库中。

everything is working fine only the inserting not. 一切都工作正常只插入不。 Im allready inserting int and strings into the db and it works fine, but the blob makes me crazy. 我已经将int和字符串插入数据库并且它工作正常,但blob让我发疯。

The Code im Using: 代码即时使用:

i got a class where i got all db-methods in it. 我得到了一个类,我在其中获得了所有db-methods。 its called db. 它叫做db。 and i got a class where im using the db-object. 我得到了一个使用db-object的类。

DB-Class: DB-类:

-(void)insertblob:(NSData *)blob stmt:(NSString *)blobStmnt {

const char *sql = [blobStmnt UTF8String];

sqlite3_prepare_v2(database, sql, 1, &statement, NULL);

if (SQLITE_DONE != sqlite3_step(statement)) {
     NSLog(@"Error while inserting data. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_blob(statement, 1, [blob bytes], [blob length], SQLITE_TRANSIENT);

}

Using-Class: 使用级:

[database openDB:@"Content"]
updateStmt = [NSString stringWithFormat:@"UPDATE Face SET image =%@", image];
[database insertblob:imageData stmt:updateStmt];
[database closeDB];

So What am i missing? 那我错过了什么? I'm getting the error that the inserting allready fails at the "U" from my update statement. 我从更新语句中收到“U”插入已经失败的错误。 but i really don't know what i have to change. 但我真的不知道我要改变什么。

Thanks in advance for your help! 在此先感谢您的帮助!

i solved it. 我解决了

in my db-class i wrote this method: 在我的db-class中我编写了这个方法:

-(void)insertblob:(NSData *)blob stmt:(NSString *)blobStmnt {

const char *sql = [blobStmnt UTF8String];
sqlite3_prepare_v2(database, sql, -1, &statement, NULL);

int returnValue = sqlite3_bind_blob(statement, 1, [blob bytes], [blob length], SQLITE_TRANSIENT);
sqlite3_step(statement);
sqlite3_finalize(statement);

if (returnValue != SQLITE_OK) {
    NSLog(@"NOT OK");
}
NSLog(@"Saved!");
}

hope that helps somebody. 希望能帮到别人。

you use base64 for converting image to string and insert string in db 您使用base64将图像转换为字符串并在db中插入字符串

download base64 class 下载base64

after check this link 检查此链接后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM