List of all Iphone and Blackberry Development codes in a one click Iphone,objective C,xcode,blackberry Development,iOS Development

Saturday, November 15, 2014

Database operations on IOS

Create table-

NSString *docsDir;
    NSArray *dirPaths;
   
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = dirPaths[0];
databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"contacts.db"]];
 NSLog(@"aaa: %@",databasePath);
    NSFileManager *filemgr = [NSFileManager defaultManager];

       
    if ([filemgr fileExistsAtPath:databasePath] == NO)
    {
        const char *dbpath = [databasePath UTF8String];
      
        if (sqlite3_open(dbpath, &sql) == SQLITE_OK)
        {
            char *errMsg;
            const char *sql_stmt ="CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
           
            if (sqlite3_exec(sql, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
            {
                NSLog(@"Failed to create table");
            }
            sqlite3_close(sql);
        }
        else
        {
            NSLog(@"Failed to open/create database");
        }
    }


Save data-

    sqlite3_stmt    *statement;
    const char *dbpath = [databasePath UTF8String];
   
    if (sqlite3_open(dbpath, &sql) == SQLITE_OK)
    {
       
        NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")",tx1.text, tx2.text, tx3.text];
       
        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(sql, insert_stmt,
                           -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            NSLog(@"Contact added");
        
        }
        else
        {
            NSLog(@"Failed to add contact");
        }
        sqlite3_finalize(statement);
        sqlite3_close(sql);
    }



Search values -

  const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt    *statement;
  

    if (sqlite3_open(dbpath, &sql) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat:
                              @"SELECT address, phone FROM contacts WHERE name=\"%@\"",
                              tx1.text];
       
        const char *query_stmt = [querySQL UTF8String];
       
        if (sqlite3_prepare_v2(sql,query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            if (sqlite3_step(statement) == SQLITE_ROW)
            {
                           
                NSString *addressField = [[NSString alloc]
                                          initWithUTF8String:
                                          (const char *) sqlite3_column_text(statement, 0)];

             
                NSString *phoneField = [[NSString alloc]
                                        initWithUTF8String:(const char *)
                                        sqlite3_column_text(statement, 1)];
                tx2.text = addressField;
                tx3.text = phoneField;
              
            }
            else
            {
                NSLog(@"No data Found");
            
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(sql);
    }

No comments:

Post a Comment