how to update database table in sqlite3 iphone

Posted by Ajeet Kumar Yadav on Stack Overflow See other posts from Stack Overflow or by Ajeet Kumar Yadav
Published on 2010-03-13T06:27:57Z Indexed on 2010/03/13 6:35 UTC
Read the original article Hit count: 544

Filed under:
|
|

Hi I am new in Iphone i am developing application that is use database but i am face problem.

 In My application I am using table view. I am fetch the value from database in the table view this is done after that we also insert value throw text field in that table of database and display that value in table view.I am also use another table in database table name is Alootikki the value of table alootikki is display in other table view in application and user want to add this table alootikki  value in  first table of database and display that value in table view when we do this value is display  only in the table view this is not write in the database table. when value is display and user want to add other data throw text field then only that add value is show first value is remove from table view. I am not able to solve this plz help me.



The code is given bellow for database

-(void)Data1 { //////databaseName = @"dataa.sqlite"; databaseName = @"Recipe1.sqlite";

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath =[documentsDir stringByAppendingPathComponent:databaseName];

[self checkAndCreateDatabase];

list1 = [[NSMutableArray alloc] init];  

sqlite3 *database;
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{

    if(addStmt == nil) {
        ////////////const char *sql = "insert into Dataa(item) Values(?)";


        const char *sql = " insert into Slist(Incredients ) Values(?)";



        if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
    }
    sqlite3_bind_text(addStmt, 1, [i UTF8String], -1, SQLITE_TRANSIENT);        
    //sqlite3_bind_int(addStmt,1,i);    //  sqlite3_bind_text(addStmt, 1, [coffeeName UTF8String], -1, SQLITE_TRANSIENT);
    //  sqlite3_bind_double(addStmt, 2, [price doubleValue]);

    if(SQLITE_DONE != sqlite3_step(addStmt))
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    else
        //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
        coffeeID = sqlite3_last_insert_rowid(database);

    //Reset the add statement.
    sqlite3_reset(addStmt);     

    //  sqlite3_clear_bindings(detailStmt);
    //}
}
sqlite3_finalize(addStmt);  
addStmt = nil;

sqlite3_close(database);

}

-(void)sopinglist

{

databaseName= @"Recipe1.sqlite";

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [documentPaths objectAtIndex:0]; databasePath =[documentsDir stringByAppendingPathComponent:databaseName];

[self checkAndCreateDatabase];

list1 = [[NSMutableArray alloc] init];

sqlite3 *database; if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

if(addStmt == nil) {
    ///////////const char *sql = "insert into Dataa(item) Values(?)";

    const char *sql = " insert into Slist(Incredients,Recipename,foodtype ) Values(?,?,?)";

/////////////   const char *sql =" Update Slist ( Incredients, Recipename,foodtype) Values(?,?,?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}



/////for( NSString * j in k)


sqlite3_bind_text(addStmt, 1, [k UTF8String], -1, SQLITE_TRANSIENT);


//sqlite3_bind_int(addStmt,1,i);    //  sqlite3_bind_text(addStmt, 1, [coffeeName UTF8String], -1, SQLITE_TRANSIENT);
//  sqlite3_bind_double(addStmt, 2, [price doubleValue]);

if(SQLITE_DONE != sqlite3_step(addStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
    coffeeID = sqlite3_last_insert_rowid(database);

//Reset the add statement.
sqlite3_reset(addStmt);     

//  sqlite3_clear_bindings(detailStmt);
//}

} sqlite3_finalize(addStmt);
addStmt = nil;

sqlite3_close(database); }

-(void)Data { ////////////////databaseName = @"dataa.sqlite";

databaseName = @"Recipe1.sqlite";

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath =[documentsDir stringByAppendingPathComponent:databaseName];

[self checkAndCreateDatabase];

list1 = [[NSMutableArray alloc] init];  

sqlite3 *database;
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
    if(detailStmt == nil)
    {
        const char *sql = "Select * from  Slist ";          
        if(sqlite3_prepare_v2(database, sql, -1, &detailStmt, NULL) == SQLITE_OK)
        {               
            //NSLog(@"Hiiiiiii");
            //sqlite3_bind_text(detailStmt, 1, [t1 UTF8String], -1, SQLITE_TRANSIENT);
            //sqlite3_bind_text(detailStmt, 2, [t2 UTF8String], -2, SQLITE_TRANSIENT);
            //sqlite3_bind_int(detailStmt, 3, t3);

            while(sqlite3_step(detailStmt) == SQLITE_ROW) 
            {
                NSLog(@"Helllloooooo");












                //NSString *item= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 0)];


                NSString *item= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 0)];


                char *str=( char*)sqlite3_column_text(detailStmt, 0);

                if( str)
                {
                    item = [ NSString stringWithUTF8String:str  ];

                }

                else
                {
                    item= @"";
                }







                //+ (NSString*)stringWithCharsIfNotNull: (char*)item
            /// {
                //  if ( item == NULL )
                    //  return nil;
                    //else
                    //  return [[NSString stringWithUTF8String: item] 
                                //stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
                //}                 









                //NSString *fame= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 1)];
                //NSString *cinemax = [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 2)];
                //NSString *big= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 3)];

                //pvr1 = pvr;
                item1=item;
                //NSLog(@"%@",item1);

                data = [[NSMutableArray alloc] init];   


                list *animal=[[list alloc] initWithName:item1];

                // Add the animal object to the animals Array
                [list1 addObject:animal];
                //[list1 addObject:item];


            }
            sqlite3_reset(detailStmt);
        }
        sqlite3_finalize(detailStmt);
        //  sqlite3_clear_bindings(detailStmt);
    }
}
detailStmt = nil;
sqlite3_close(database);

}

  • (void)recpies

{ /////////////////////databaseName = @"Data.sqlite"; databaseName = @"Recipe1.sqlite";

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath =[documentsDir stringByAppendingPathComponent:databaseName];

[self checkAndCreateDatabase];

list1 = [[NSMutableArray alloc] init];  

sqlite3 *database;
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
    if(detailStmt == nil)
    {
        //////const char *sql = "Select * from Dataa";

        const char *sql ="select *from alootikki";
        if(sqlite3_prepare_v2(database, sql, -1, &detailStmt, NULL) == SQLITE_OK)
        {               
            //NSLog(@"Hiiiiiii");
            //sqlite3_bind_text(detailStmt, 1, [t1 UTF8String], -1, SQLITE_TRANSIENT);
            //sqlite3_bind_text(detailStmt, 2, [t2 UTF8String], -2, SQLITE_TRANSIENT);
            //sqlite3_bind_int(detailStmt, 3, t3);

            while(sqlite3_step(detailStmt) == SQLITE_ROW) 
            {
                //NSLog(@"Helllloooooo");



                //NSString *item= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 0)];


                NSString *item= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 0)];


                char *str=( char*)sqlite3_column_text(detailStmt, 0);

                if( str)
                {
                    item = [ NSString stringWithUTF8String:str  ];

                }

                else
                {
                    item= @"";
                }




                //NSString *fame= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 1)];
                //NSString *cinemax = [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 2)];
                //NSString *big= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 3)];

                //pvr1 = pvr;
                item1=item;
                //NSLog(@"%@",item1);

                data = [[NSMutableArray alloc] init];   


                list *animal=[[list alloc] initWithName:item1];

                // Add the animal object to the animals Array
                [list1 addObject:animal];
                //[list1 addObject:item];


            }
            sqlite3_reset(detailStmt);
        }
        sqlite3_finalize(detailStmt);
        //  sqlite3_clear_bindings(detailStmt);
    }
}
detailStmt = nil;
sqlite3_close(database);

}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c