RSS

Sqlite Database Check , Read , Insert , Delete Functions

17 Apr

1 ) Usefull header File declarations :

Ans :
NSString *databaseName;
NSString *dbPath;
NSString *id1;
NSMutableArray *mainarray,*temparray;
NSMutableDictionary *tempdic;

2 ) Checking for database file exists or not ?
Ans :

-(void)checkdataBaseFile
{

NSArray *docPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *docDir=[docPaths objectAtIndex:0];

dbPath=[docDir stringByAppendingPathComponent:databaseName];

BOOL success;

NSFileManager *fm=[NSFileManager defaultManager];

success=[fm fileExistsAtPath:dbPath];

if(success) return;

NSString *dbPathFromApp=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

[fm copyItemAtPath:dbPathFromApp toPath:dbPath error:nil];

[fm release];

}

3 ) Insert Function

Ans :

code :

-(void)savedata

{
sqlite3 *database;

[self checkdata];

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *sqlTmp=[NSString stringWithFormat:@”insert into tbl values(NULL,’%@’,’%@’)”,[[mainarray objectAtIndex:0]valueForKey:@”catid”],[[mainarray objectAtIndex:0]valueForKey:@”image”]];

const char *sqlStmt=[sqlTmp UTF8String];

sqlite3_stmt *cmp_sqlStmt;

int returnValue = sqlite3_prepare_v2(database, sqlStmt, -1, &cmp_sqlStmt, NULL);

((returnValue==SQLITE_OK) ? NSLog(@”Success”) : NSLog(@”UnSuccess”) );

sqlite3_step(cmp_sqlStmt);

sqlite3_finalize(cmp_sqlStmt);

}

sqlite3_close(database);

}

4 ) Read From DB File
Ans:
Code :
-(void)readdata

{
[self checkdata];

sqlite3 *database;

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)

{
NSString *sql=[NSString stringWithFormat:@”select * from tbl”];

const char *sqlStmt=[sql UTF8String];

sqlite3_stmt *cmp_sqlStmt;

if(sqlite3_prepare_v2(database, sqlStmt, -1, &cmp_sqlStmt, NULL)==SQLITE_OK)

{
while(sqlite3_step(cmp_sqlStmt)==SQLITE_ROW)
{
id1=[NSString stringWithUTF8String:(char*)sqlite3_column_text(cmp_sqlStmt, 2)];

//newStu=[[student alloc]initWithNo:id1];

[temparray addObject:id1];

//[newStu release];

}
}

sqlite3_finalize(cmp_sqlStmt);

}

sqlite3_close(database);
}

5 ) Delete function .
Ans:
Code:
-(void)DeleteAll record
{
sqlite3 *database;

[self checkdata];

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)

{
NSString *str=[NSString stringWithFormat:@”delete from tbl”];

const char *sqlStmt=[str UTF8String];

sqlite3_stmt *cmp_sqlStmt;

if(sqlite3_prepare_v2(database, sqlStmt, -1, &cmp_sqlStmt, NULL)==SQLITE_OK)
{
int returnValue = sqlite3_prepare_v2(database, sqlStmt, -1, &cmp_sqlStmt, NULL);
((returnValue==SQLITE_OK) ? NSLog(@”Success”) : NSLog(@”UnSuccess”) );
sqlite3_step(cmp_sqlStmt);
}
sqlite3_finalize(cmp_sqlStmt);
}

sqlite3_close(database);
}

Advertisements
 
1 Comment

Posted by on April 17, 2012 in Uncategorized

 

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

 
%d bloggers like this: