0

I am new to sQLite and created a sqlite database in the mac's terminal for my app. I then added the database too my app and ran fine. Recently I needed another table, so i downloaded sqlitemanager(a firefox plugin with a gui for sqlite databases, kinda didnt want to write script for everysingle thing). From since then, I have been getting the error "Table not found". However browsing my database i see the table, but if i look at the database in the ios simulator i dont see the tables(database is zero kb so i guess the error is justified). However, if I just copy and paste the database directly into that folder the app works fine. However this is not a good fix...but is this occuring because i used this manager to edit the database? If yes what manager can i use or if no what is the problem.

Thank you

+ (NSString *) getDBPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"tourism.sql"];


}

+ (void) copyDatabaseIfNeeded {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [DBAccess getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath];

    if(!success) {

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"tourism.sql"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success)
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}
Sleep Paralysis
  • 449
  • 7
  • 22
  • 1
    You are being unclear. Remember that the DB that the application uses is not in the bundle, but in the read/write file system. You first must get your DB to included in the bundle, then have code that copies it from the bundle to read/write store on first startup. (There are [dozens of examples](http://stackoverflow.com/questions/15491238/how-to-copy-sqlite-database-when-application-is-launched-in-ios) of this code to study.) – Hot Licks Apr 09 '14 at 02:47
  • @HotLicks I have this code, that worked fine for me. What I am saying now is, I add my db too the project like adding a file, then when i run it it copies into the budle. (Correct me if im wrong) However when i run the program. I get the "Table doesnt exist" error, then when i look at the db in the bundle it is 0 kb an has no tables but the db in my program is 12kb and has my tables as expected. in experimenting i copied that db from my project folder and into the bundle folder then the program worked...no table error – Sleep Paralysis Apr 09 '14 at 03:28
  • not sure why it creates the database but not the tables – Sleep Paralysis Apr 09 '14 at 03:30
  • 1
    I also use firefox plugin for sqlite database modifications. While adding the db into app bundle select the option 'copy items into destinations group folder'. Once you add the DB into bundle you can't modify the db hence you can copy the db to 'Documents' directory and do the modifications. – user2071152 Apr 09 '14 at 03:34
  • @user2071152 Just tried it again. I deleted the database in the simulator so it would make a new one. I copy my 12kb database into my program (has two tables in it) and i add it too target and also copy items into dest group folder. Then i run the program, program runs but tables are not found. I open my simulator folder , the database is created but it is empty, size zero. – Sleep Paralysis Apr 09 '14 at 03:44
  • You can copy the DB by right click on any folder in XCode project and add by selecting the option. Once you add the file in app bundle, you can access the db using NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"dbname"];. – user2071152 Apr 09 '14 at 03:51
  • @user2071152 I added the code I use to create my database please tell me if it is valid – Sleep Paralysis Apr 09 '14 at 03:56
  • Could you check whether the extension of db is .sql? The above code is valid for first time but if you change any thing in DB and want to update the same in Document directory then you should delete the db from Documents directory and run the application again. – user2071152 Apr 09 '14 at 04:03
  • Yes it is .sql extension. funny thou i use it with a .sqlite database and it works perfectly. any idea why? – Sleep Paralysis Apr 09 '14 at 04:14
  • **Read what I said again.** You do not (or should not) access your DB from the bundle. The bundle is read-only. It **must** be copied to read/write space. (And when you open a file in SQLite, if the file does not exist then SQLite will create it -- empty.) – Hot Licks Apr 09 '14 at 11:00
  • @user2071152 - The file extension makes no difference -- it could be .java and SQLite would still access it as a SQLite DB. – Hot Licks Apr 09 '14 at 11:02
  • @HotLicks - I meant to just check the extension of DB as while saving from firefox plugin we can do it without giving an extension also. So just wanted to verify. – user2071152 Apr 09 '14 at 11:09

0 Answers0