I am using these lines of code to insert data. But yet it is not workable for me. I used the same technique you told in this post. But acquiring same result means nothing. I am trying to explain you in code as well that where i am facing difficulties.
- (void)dataInsertion {
NSString *query;
NSString *querySelect;
NSString *queryInsert;
NSArray *asdasd;
NSArray *asd;
Sqlite *sqliteDatabase =[[Sqlite alloc] init];
/***************************Database Enterance is working here for temporary base*************************/
/******After Applying these lines of code, i am able to get the result in console not in my database. But as soon as i restart my application, I have lost the data in console as well.******/
myDB=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Test.sqlite"];
Sqlite *sqliteDatabase =[[Sqlite alloc] init];
[sqliteDatabase open:myDB];
queryInsert=[NSString stringWithFormat:@"INSERT INTO Test VALUES('value1','Value2','Value3');"];
NSLog(@"Query is , %@",queryInsert);
[sqliteDatabase executeNonQuery:queryInsert];
query=[NSString stringWithFormat:@"Select * FROM Test"];
NSLog(@"Query is , %@",query);
asdasd=[sqliteDatabase executeQuery:query];
NSLog(@"Array contains %@",asdasd);
[asdasd release];
/*****************************************************/
/***************Other Way*************/
/**************By Applying these line, My Application get crashed on lines [sqliteDatabase executeNonQuery:queryInsert];*******/
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"SohaibDb.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL success;
success = [fileManager fileExistsAtPath:path];
if (success)
{
NSLog(@"Writable database already exists.");
/***** My control is coming here****/
}
else
{
/******** Control is not coming here*********/
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Test.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:path error:&error];
if (!success)
{
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
/******* Here it is giving me warning that Argument2 is from incompatible pointer type*****/
if (sqlite3_open([path UTF8String], &sqliteDatabase) == SQLITE_OK)
{
/***** My control is coming here ******/
queryInsert=[NSString stringWithFormat:@"INSERT INTO Test VALUES('value1','Value2','Value3');"];
NSLog(@"Query is , %@",queryInsert);
[sqliteDatabase executeNonQuery:queryInsert]; //On this line Application is Crashing without giving an error
query=[NSString stringWithFormat:@"Select * FROM Test"];
NSLog(@"Query is , %@",query);
asdasd=[sqliteDatabase executeQuery:query];
NSLog(@"Array contains %@",asdasd);
[asdasd release];
}
else
{
/********** Control is not coming here ********/
NSLog(@"Database is not Opening.");
}
/*******************************************/
[sqliteDatabase commit];
[sqliteDatabase close];
}