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);     }   } 

  1. always check result of sqlite3_prepare_v2. if fails, log problem using sqlite3_errmsg.
  2. only call sqlite3_finalize if sqlite3_prepare_v2 succeeds.
  3. you null blob because call sqlite3_bind_blob passing wrong column index. should 1, not 2 since want bind first ? in insert statement.
  4. why inconsistency? why use stringwithformat set value invoiceid column , use sqlite_bind_xxx image column? should bind both. never use stringwithformat build query.
  5. you call sqlite3_step twice. call once , bind values before call it.
  6. 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

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -