DataBASe

File ADD

NSString *strpath = [NSHomeDirectory() stringByAppendingPathComponent:@”/Documents/Database_Sample1.sqlite”];

    NSLog(@”%@”,strpath);

    NSError *err;

    if (![[NSFileManager defaultManager]fileExistsAtPath:strpath])

    {

        NSString *str1 = [[NSBundle mainBundle]pathForResource:@”Database_Sample1″ ofType:@”sqlite”];

        [[NSFileManager defaultManager]copyItemAtPath:str1 toPath:strpath error:&err];

        

    }

    else{

        NSLog(@”Already Exist..”);

    }

.h file

#import <Foundation/Foundation.h>

#import <sqlite3.h>

@interface sqliteconnecton : NSObject

{

    NSMutableArray *data;

}

-(NSMutableArray *)selectrecord:(NSString *)dbpath SelectQuery:(NSString *)query ;

-(NSString *)IUDrecord:(NSString *)dbpath SelectQuery:(NSString *)query ;

.m file

#import “sqliteconnecton.h”

#import <sqlite3.h>

@implementation sqliteconnecton

-(NSMutableArray *)selectrecord:(NSString *)dbpath SelectQuery:(NSString *)query{

    

    data = [[NSMutableArray alloc]init];

    sqlite3 *db;

    

    

    int i;

    if (sqlite3_open([dbpath UTF8String], &db)==SQLITE_OK)

    {

        NSString *strqury = query;

        NSMutableDictionary *dict1;

        sqlite3_stmt *statementss;

        if (sqlite3_prepare_v2(db, [strqury UTF8String], –1, &statementss, nil)==SQLITE_OK)

        {

            while (sqlite3_step(statementss)==SQLITE_ROW)

            {

                dict1 =[[NSMutableDictionary alloc]init];

                for (i=0; i<sqlite3_column_count(statementss); i++)

                {

                    

                    NSString *strcolumnname = [NSString stringWithUTF8String:sqlite3_column_name(statementss, i)];

                    NSString *strcolumndata = [NSString stringWithUTF8String😦char *)sqlite3_column_text(statementss, i)];

                    [dict1 setObject:strcolumndata forKey:strcolumnname];

                }

                [data addObject:dict1];

            }

        }

        sqlite3_close(db);

    }

    return data;

}

-(NSString *)IUDrecord:(NSString *)dbpath SelectQuery:(NSString *)query{

    sqlite3 *d1;

    NSString *message;

    if (sqlite3_open([dbpath UTF8String], &d1) == SQLITE_OK)

    {

        NSLog(@”Database Open”);

        sqlite3_stmt *completedStatement;

        

        if (sqlite3_prepare_v2(d1, [query UTF8String], –1, &completedStatement, NULL) == SQLITE_OK)

        {

            if (sqlite3_step(completedStatement) == SQLITE_DONE)

            {

                message = @”Success”;

               sqlite3_close(d1);

            }

            else

            {

                message = @”error”;

            }

        }

        else {

            message =@”error”;

        }

        

    }

    return message;

}

@end

Insert update delete with demo

sqliteconnecton *sqlConnection=[[sqliteconnecton alloc] init];

     NSString *strpath = [NSHomeDirectory() stringByAppendingPathComponent:@”/Documents/Database_Sample1.sqlite”];

    /*********************** Select Query *********************/

    

    /*********************** Insert Update Delete **************/

    NSString *q1=@”insert into user_Master values(null,’ddd’,’dd’)”;

    NSString *str=[sqlConnection IUDrecord:strpath SelectQuery:q1];

    if ([str isEqualToString:@”Success”]) {

        NSLog(@”Inserted”);

    }else{

        NSLog(@”Error inserting”);

    }

    

    

    NSString *strDEl=@”delete  from user_Master where (user_id =1)”;

    NSString *str1=[sqlConnection IUDrecord:strpath SelectQuery:strDEl];

    if ([str1 isEqualToString:@”Success”]) {

        NSLog(@”Deleted”);

    }else{

        NSLog(@”Error deleting”);

    }

    NSString *q=@”select * from user_Master”;

    NSMutableArray *arr=[sqlConnection selectrecord:strpath SelectQuery:q];

    NSLog(@”%@”,arr);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s