3

i got a problem with inserting a blob into a sqlite db. I'm workin on ios so it's objective-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.

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.

The Code im Using:

i got a class where i got all db-methods in it. its called db. and i got a class where im using the db-object.

DB-Class:

-(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. but i really don't know what i have to change.

Thanks in advance for your help!

2
  • you should change the updateStmt like this. updateStmt = [NSString stringWithFormat:@"UPDATE Face SET image =?"]; Commented Oct 24, 2013 at 7:10
  • Inserting an image into SQL is not efficient and provably should be avoided. Use your local storage to store the image and use SQL to store the reference to that path / image name Commented Oct 24, 2013 at 7:52

2 Answers 2

1

i solved it.

in my db-class i wrote this method:

-(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.

Sign up to request clarification or add additional context in comments.

Comments

0

you use base64 for converting image to string and insert string in db

download base64 class

after check this link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.