objective c - iOS SQLite Blob data is saving NULL -
i trying insert blob data of signatureview using code when browse database there null instead of data. signature table schema given below.
create table sign(id integer primary key autoincrement, image blob,invoiceid integer);
-(void)storeimagedata:(nsdata *)imagedata withinvoiceid:(nsinteger)invoiceid{ nsstring *dbpath = [[[nsbundle mainbundle] resourcepath ]stringbyappendingpathcomponent:@"database.sqlite3"]; const char *dbpath = [dbpath utf8string]; sqlite3 *contactdb; sqlite3_stmt *statement; nslog(@"%@",dbpath); if (sqlite3_open(dbpath, &contactdb) == sqlite_ok) { int invoiceidint = (int)invoiceid; // nsstring *insertsql = [nsstring stringwithformat: @"insert sign (image,invoiceid) values (?,%d)", invoiceidint]; const char *insert_stmt = [insertsql utf8string]; sqlite3_prepare_v2(contactdb, insert_stmt, -1, &statement, null); if (sqlite3_step(statement) == sqlite_done) { sqlite3_bind_blob(statement, 2, [imagedata bytes], [imagedata length], sqlite_transient); sqlite3_step(statement); } else { const char *error = sqlite3_errmsg(database); nsstring *error = [[nsstring alloc]initwithutf8string:error]; uialertview *view = [[uialertview alloc]initwithtitle:@"error2" message:[nsstring stringwithformat:@"last inserted id: %@",error] delegate:nil cancelbuttontitle:@"cancel" otherbuttontitles:nil, nil]; [view show]; } sqlite3_finalize(statement); sqlite3_close(contactdb); } }
- always check result of
sqlite3_prepare_v2. if fails, log problem usingsqlite3_errmsg. - only call
sqlite3_finalizeifsqlite3_prepare_v2succeeds. - you
nullblob because callsqlite3_bind_blobpassing wrong column index. should1, not2since want bind first?ininsertstatement. - why inconsistency? why use
stringwithformatset valueinvoiceidcolumn , usesqlite_bind_xxximagecolumn? should bind both. never usestringwithformatbuild query. - you call
sqlite3_steptwice. call once , bind values before call it. - you appear writing database inside app's resource bundle. can't on real device. works in simulator not on real ios devices. need put database file in documents folder.
given of above, code should this:
-(void)storeimagedata:(nsdata *)imagedata withinvoiceid:(nsinteger)invoiceid{ // wrong - need update use documents folder nsstring *dbpath = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@"database.sqlite3"]; const char *dbpath = [dbpath utf8string]; nslog(@"%@",dbpath); sqlite3 *contactdb; if (sqlite3_open(dbpath, &contactdb) == sqlite_ok) { const char *insert_stmt = "insert sign (image, invoiceid) values (?, ?)"; sqlite3_stmt *statement; if (sqlite3_prepare_v2(contactdb, insert_stmt, -1, &statement, null) == sqlite_ok) { sqlite3_bind_blob(statement, 1, [imagedata bytes], [imagedata length], sqlite_transient); sqlite3_bind_int(statement, 2, (int)invoiceid); if (sqlite3_step(statement) == sqlite_done) { // row inserted } else { const char *error = sqlite3_errmsg(contactdb); nsstring *error = [[nsstring alloc] initwithutf8string:error]; uialertview *view = [[uialertview alloc] initwithtitle:@"error2" message:[nsstring stringwithformat:@"last inserted id: %@",error] delegate:nil cancelbuttontitle:@"cancel" otherbuttontitles:nil, nil]; [view show]; } sqlite3_finalize(statement); } else { nslog(@"unable prepare statement %s: %s", insert_stmt, sqlite3_errmsg(contactdb)); } sqlite3_close(contactdb); } else { nslog(@"unable open database @ path %@: %s", dbpath, sqlite3_errmsg(contactdb)); } }
Comments
Post a Comment