I have two classes A and B using SQLite database.Class A is using TableA and classB is using tableB. I’m able to insert data in class A but not in class B. However, select query is working properly in class B. I have used same function of insert as in Class A. I have gone through my code several times still not able to figure this out.
Code where I’m inserting data into database.
[app.sk insertDictionary:dictFrndDetail forTable:@”tblAllProfiles”];
Function i’m using for insertion:
- (void)insertDictionary:(NSDictionary *)dbData forTable:(NSString *)table {
NSMutableString *sql = [NSMutableString stringWithCapacity:16];
[sql appendFormat:@"INSERT INTO %@ (",table];
NSArray *dataKeys = [dbData allKeys];
for (int i = 0 ; i < [dataKeys count] ; i++) {
[sql appendFormat:@"%@",[dataKeys objectAtIndex:i]];
if (i + 1 < [dbData count]) {
[sql appendFormat:@", "];
}
}
[sql appendFormat:@") VALUES("];
for (int i = 0 ; i < [dataKeys count] ; i++) {
if ([[dbData objectForKey:[dataKeys objectAtIndex:i]] intValue]) {
[sql appendFormat:@"%@",[dbData objectForKey:[dataKeys objectAtIndex:i]]];
} else {
[sql appendFormat:@"'%@'",[dbData objectForKey:[dataKeys objectAtIndex:i]]];
}
if (i + 1 < [dbData count]) {
[sql appendFormat:@", "];
}
}
[sql appendFormat:@")"];
[self runDynamicSQL:sql forTable:table];
}
Code for runDynamicSQL:
- (BOOL)runDynamicSQL:(NSString *)sql forTable:(NSString *)table {
int result;
NSAssert1(self.dynamic == 1,[NSString stringWithString:@"Tried to use a dynamic function on a static database"],NULL);
sqlite3_stmt *statement;
if (statement = [self prepare:sql]) {
result = sqlite3_step(statement);
}
sqlite3_finalize(statement);
if (result) {
if (self.delegate != NULL && [self.delegate respondsToSelector:@selector(databaseTableWasUpdated:)]) {
[delegate databaseTableWasUpdated:table];
}
return YES;
} else {
return NO;
}
}
But This same code is working in one class but not in this class. How to figure out the error?
Use FMDB for your SQLITE purpose it easier to manage and easier to implement then sqlite. Search on google for FMDB tutorials you will find it a lot easier than your current implementation.