Creating an Iphone application, I used to perform INSERT QUERY on different databases.
But when I tried inserting the data in the same database, it gives me assertion failure.
I am reading two different XML URL and trying to create and then insert the data in the table as it is read by creating DB. Does anyone have idea why does this show me error ?
I have the following code which does insert on DB using SQLITE:
-(void) insertProductTable:(NSMutableArray *)Dict{
[Dict retain];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"product.sql"];
NSInteger i;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char * insert_product_sql = "INSERT OR REPLACE INTO test(ID,KEY ) VALUES (?,?)" ;
if(sqlite3_prepare_v2(database, insert_product_sql, -1, &product_statement, nil) != SQLITE_OK){
NSAssert1(0, @"Error: failed to prepare insert_product_sql: '%s'.", sqlite3_errmsg(database));
}
for (i = 0; i < [self.array count]; i++) {
sqlite3_bind_text(product_statement, 1, [[Dict valueForKey:@"ID"] UTF8String] ,-1, SQLITE_TRANSIENT);
sqlite3_bind_text(product_statement, 2, [[Dict valueForKey:@"KEY"] UTF8String] ,-1, SQLITE_TRANSIENT);
}
if (sqlite3_step(product_statement) != SQLITE_DONE) {
NSAssert(0,@"Error updating table");
}
[Dict release]; Dict = 0;
sqlite3_finalize(product_statement);
sqlite3_close(database);
}
}
I have the following method which does the different insert function but different table but at this stage it doesnot allow me to insert throwing me Assertion Failure. Both the methods are called by different class.
-(void) insertTable:(NSMutableArray *)Dict{
[Dict retain];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"product.sql"];
NSInteger i;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char * insert_sql = "INSERT OR REPLACE INTO test1(Term,Value ) VALUES (?,?)" ;
if(sqlite3_prepare_v2(database, insert_sql, -1, &product_statement, nil) != SQLITE_OK){
NSAssert1(0, @"Error: failed to prepare insert_sql: '%s'.", sqlite3_errmsg(database));
}
for (i = 0; i < [self.arayList count]; i++) {
sqlite3_bind_text(product_statement, 1, [[Dict valueForKey:@"TERM"] UTF8String] ,-1, SQLITE_TRANSIENT);
sqlite3_bind_text(product_statement, 2, [[Dict valueForKey:@"VALUE"] UTF8String] ,-1, SQLITE_TRANSIENT);
}
if (sqlite3_step(product_statement) != SQLITE_DONE) {
NSAssert(0,@"Error updating table");
}
[Dict release]; Dict = 0;
sqlite3_finalize(product_statement);
sqlite3_close(database);
}
}
@lifemoveson
I used to your code to debug and found that the function
returns 1 everytime and it corresponds to your missing database at the desired location.
What I will suggest to you is make a function such as this
Which sets your class variable databasePath and ensure that before you run any database query the database is actually present in the applcation’s document directory. You can call this function in application delegate
When you make any database call use this
i would suggest you to write a separate class for all the database operation that you perform and instead call the class function to achieve the desired functionality. This will save your efforts from rewriting the code.
You can make use of FMDB wrapper for this which you can find here
Hope this helps 🙂