0

I'm trying to add some datas from NSDictionary/NSArray into my database. My project use sqlite/FMDB to do it. But I get some problem: I can't save NSData into my database.
Here is my code:

- (void)startSaveDB {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [self databasePath];
    if ([fileManager fileExistsAtPath:path] == YES) {
        FMDatabase *database = [FMDatabase databaseWithPath:path];
        if (database) {
            [database open];
            [self createTableCacheApiWithDatabase:database];
            [self saveAPIwithJsonData:jsonData withParam:params withUrl:url withMethod:method withDatabase:database];
        }
        [database close];
    }
}

Create table:

- (void)createTableCacheApiWithDatabase:(FMDatabase*)database {
    [database executeUpdate:@"create table if not exists APIENGINE_CACHE_TABLE (idx INTEGER NOT NULL DEFAULT 0 PRIMARY KEY AUTOINCREMENT, url Varchar NOT NULL, method Varchar NOT NULL, date_modified Varchar NULL, param BLOB NOT NULL, data BLOB NOT NULL);"];
}

Convert NSDictionary to NSData and save:

- (void)saveAPIwithJsonData:(NSDictionary*)jsonData
                  withParam:(NSDictionary*)params
                    withUrl:(NSString*)url
                 withMethod:(NSString*)method
               withDatabase:(FMDatabase*)database {
    NSData *dataJson = [NSKeyedArchiver archivedDataWithRootObject:jsonData];
    NSData *dataParams = [NSKeyedArchiver archivedDataWithRootObject:params];
    if ([database open]) {
        [database executeQuery:@"insert into APIENGINE_CACHE_TABLE (param, data, url, method) values (?, ?, ?, ?);", [NSData dataWithData:dataParams], [NSData dataWithData:dataJson], url, method];
    }
}

Path:

- (NSString *) databasePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex: 0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME];
    NSLog(@"path: %@", path);
    return path;
}

What is wrong with my code?

TienLe
  • 614
  • 2
  • 9
  • 33

1 Answers1

0

Its an old question, but I came by chance for searching a solution to a problem. The answer is FMDabatase binds values for NSData object. So you cannot use formatted string for NSData insertion. Rather use query with (?,?,?) format. If you really want a kind of formatting, you can use in this way,

TABLE_USER, COL_CUSTOMER_ID, COL_IMAGE_PATH, COL_EXPIRY_DATE, COL_MOBILE_NUMBER are some constant defined.

NSString * prepareStmt = [NSString stringWithFormat:
                    @"INSERT INTO %@ "
                    "(%@, %@, %@, %@) "
                    "VALUES  (?, ?, ?, ?) ",
                    TABLE_USER,
                    COL_CUSTOMER_ID, 
                    COL_IMAGE_PATH,
                    COL_EXPIRY_DATE,
                    COL_MOBILE_NUMBER];
    NSLog(@"Query: %@", prepareStmt);
    BOOL res = [self.dbConnection executeUpdate:prepareStmt, 
                userInfo.customerId, 
                userInfo.imagePath,
                userInfo.expiryDate, 
                userInfo.mobileNumber, nil];
karim
  • 15,408
  • 7
  • 58
  • 96