1

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?

2
  • Some code, including the SQL queries might be a bit more helpful. Commented Jun 27, 2011 at 6:42
  • yadav did you try debug your app? What error code is Sqlite returning? post your [runDynamicSQL: forTable:] method. Commented Jun 27, 2011 at 6:47

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

i have figured out the error i was sending device id in the parameter which sqlite is taking as number. I 'm fetching device id by the code below. Still sql query is throwing error near this. NSString *uniqueIdentifier = [[UIDevice currentDevice] uniqueIdentifier];. What should i do?
@Smriti your coding style is good. Why don't you use FMDB. It seems you are an experienced iPhone developer.
i'm sending dictionary through bluetooth in which i'm device id along with other data as nsstring only. when i executed the query in sqlite browser it showed me error near device id. i have even tried string formatting. but no luck. :(
@Rahul: The project is on the verge of completion. I don't want to implement that much change now.
@Smriti how much experience you have in iPhone and where are you working right now?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.