Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try like this..</p> <p>In viewdidload we need to check wether table exist or not. If not we need to create db.</p> <pre><code>NSString *docsdir; NSArray *dirpaths; dirpaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); docsdir=[dirpaths objectAtIndex:0]; dabasePath=[NSString stringWithFormat:[docsdir stringByAppendingPathComponent:@"contact.db"]]; NSFileManager *filemgr= [NSFileManager defaultManager]; if ([filemgr fileExistsAtPath:dabasePath]==NO ) { const char *dbpath=[dabasePath UTF8String]; if (sqlite3_open(dbpath, &amp;contactDB)== SQLITE_OK) { char *error; const char *sql_stmt="CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, ADDRESS TEXT, NAME TEXT, PHONE TEXT, IMAGE BLOB)"; if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &amp;error)!= SQLITE_OK) { status.text=@"failed to create"; } sqlite3_close(contactDB); } } </code></pre> <p>To save data try to use the following code.</p> <pre><code>-(IBAction)saveData:(id)sender{ sqlite3_stmt *statement; const char *dbpath = [dabasePath UTF8String]; NSData *imagedata=UIImagePNGRepresentation(imageview.image); if (sqlite3_open(dbpath, &amp;contactDB) == SQLITE_OK) { NSString *insertSql =[NSString stringWithFormat:@"INSERT INTO CONTACTS (name, address, phone, image) VALUES (\"%@\", \"%@\", \"%@\", ?) ", name.text, address.text, phone.text ]; // NSString *nam=name.text; const char *insert_stmt = [insertSql UTF8String]; sqlite3_prepare_v2(contactDB, insert_stmt, -1, &amp;statement, NULL); sqlite3_bind_blob(statement, 1, [imagedata bytes], [imagedata length], NULL); if (sqlite3_step(statement) == SQLITE_DONE) { status.text=@"contact added"; [self clearClick:nil]; }else{ status.text=@"failed to added"; } sqlite3_finalize(statement); sqlite3_close(contactDB); } } </code></pre> <p>To update data try to use the following code. </p> <pre><code>-(IBAction)updateClick:(id)sender{ sqlite3_stmt *updateStmt; const char *dbpath = [dabasePath UTF8String]; if(sqlite3_open(dbpath, &amp;contactDB) == SQLITE_OK) { const char *sql = "update contacts Set address = ?, phone = ?, image = ? Where name=?"; if(sqlite3_prepare_v2(contactDB, sql, -1, &amp;updateStmt, NULL)==SQLITE_OK){ sqlite3_bind_text(updateStmt, 4, [name.text UTF8String], -1, SQLITE_TRANSIENT); sqlite3_bind_text(updateStmt, 1, [address.text UTF8String], -1, SQLITE_TRANSIENT); sqlite3_bind_text(updateStmt, 2, [phone.text UTF8String], -1, SQLITE_TRANSIENT); NSData *imagedata=UIImagePNGRepresentation(imageview.image); sqlite3_bind_blob(updateStmt, 3, [imagedata bytes], [imagedata length], NULL); } } char* errmsg; sqlite3_exec(contactDB, "COMMIT", NULL, NULL, &amp;errmsg); if(SQLITE_DONE != sqlite3_step(updateStmt)){ NSLog(@"Error while updating. %s", sqlite3_errmsg(contactDB)); } else{ [self clearClick:nil]; } sqlite3_finalize(updateStmt); sqlite3_close(contactDB); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload