Regarding Sqlite Datbase connectivity in iphone
- by Prash.......
hi..
I am developing an application in iphone in which i have to do the database connectivity using sqlite.
I have made a "clsDBManage" class which contains 4 functions open_database, close_database, is_database_open, getdatabase_connection, which are globally defined ,i have a screen called "user details" in which i am filling the user details such as name , mobileno , email-id , password, i want that when user feeds the info it should get connected with database and store the details entered and should authenticate the user the next time he logs in.
(Just as we have yahoo login,gmail login screen)
clsDBManage.h
+(int) openDBConnection;
+(int) closeDBConnection;
+(BOOL) IsDatabaseOpen;
+(sqlite3 *)getDBConnection;
clsDBManage.m
import "clsDBManage.h"
import "Types.h"
sqlite3 *DBConnection=nil;
@implementation clsDBManage
+(int) openDBConnection
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"iphone.sqlite"];
//Before
if ([self IsDatabaseOpen] == YES)
[self closeDBConnection];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &DBConnection ) == SQLITE_OK)
{
ifdef _DEBUG
NSLog(@"Database Successfully Opened :)");
endif
return CON_RET_SUCCESSFUL;
}
else
{
ifdef _DEBUG
NSLog(@"Error in opening database :(");
endif
return CON_RET_ERROR;
}
}
+(BOOL) IsDatabaseOpen
{
if(DBConnection != nil)
{
//add if condition to check database is in open state or close state
return YES;
}
else
{
return NO;
}
}
+(int) closeDBConnection
{
@try
{
if(DBConnection != nil)
{
sqlite3_close(DBConnection);
DBConnection = nil;
return CON_RET_SUCCESSFUL;
}
else
{
return CON_RET_ERROR;
}
}
@catch (NSException * e) {
NSLog([e reason]);
}
}
+(sqlite3 *)getDBConnection
{
if ([self openDBConnection] == 1)
return DBConnection;
else
return nil;
}
@end
//classDBManage file is my handler class where i have written code to open database
//my addprofile functions
-(NSInteger)addProfile:(NSString *)mobileno:(NSString *)country:(NSString *)username:(NSString *)screenname:(NSString *)emailid:(NSString *)password:(NSString *)retypepassword
{
@try
{
sqlite3 *db =[clsDBManage getDBConnection];
if (db != nil)
{
sqlite3_stmt *statement =nil;
const char *sql = "insert into SPCaccountdetails(MobileNo , Country , UserName , ScreenName, EmailId, Password, RetypePawssword) Values(?,?,?,?,?,?,?)";
if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL) != SQLITE_OK)
{
//NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(db));
}
sqlite3_bind_text(statement, 1, [mobileno UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [country UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [username UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 4, [screenname UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 5, [emailid UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 6, [password UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 7, [retypepassword UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(statement))
{
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(db));
}
sqlite3_finalize(statement);
}
}
@catch(NSException *e)
{
//[clsMessageBox ShowMessageOK:@CON_MESSAGE_TITLE_MESSAGE_USER_PROFILE :[e reason]];
}
return CON_RET_SUCCESSFUL;
}
//button click event where i am calling addprofile function;_
[self addProfile:txtMobile :txtCountry :txtName :txtScreenname :txtemailid :txtpassword :txtretypepassword];