lbremen
lbremen

Reputation: 53

insert blob in sqlite db (objective-c)

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!

Upvotes: 3

Views: 2964

Answers (2)

lbremen
lbremen

Reputation: 53

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.

Upvotes: 1

kirti Chavda
kirti Chavda

Reputation: 3015

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

download base64 class

after check this link

Upvotes: 0

Related Questions