0
- init
{
    if(![super init]) return nil;

    //the database is stored in the application bundle.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *path = [documentsDirectory stringByAppendingFormat:@"/base.sqlite"];
    NSLog(@"%@",path);
    db = [FMDatabase databaseWithPath:path];
    [db setLogsErrors:YES];

    if (![db open]) { 
        NSLog(@"Could not open db."); 
        return 0; 
    } else { 
        NSLog(@"DB Open...."); 
    } 

    FMResultSet *rs = [db executeQuery:@"SELECT * FROM settings"]; 

    return self; 
}

that's the code i use for trying to access the data from settings. but this gives an error (and i can assure you that there is a settings table ! )

here is a dump from the terminal

BEGIN TRANSACTION;
CREATE TABLE About (id integer PRIMARY KEY AUTOINCREMENT,key varchar UNIQUE,value text);
DELETE FROM sqlite_sequence;
CREATE TABLE settings (version integer,updated timestamp,owner varchar);
INSERT INTO "settings" VALUES(1,'2009-05-11 14:29:07','boulevart');
COMMIT;

and the error:

2009-05-11 16:14:19.799 SummerGuide[9892:20b] /Users/andyjacobs/Library/Application Support/iPhone Simulator/User/Applications/762630F5-78CB-41A4-85C2-964316ACFE1D/Documents/base.sqlite
2009-05-11 16:14:19.801 SummerGuide[9892:20b] DB Open....
2009-05-11 16:14:19.804 SummerGuide[9892:20b] DB Error: 1 "no such table: settings"
2009-05-11 16:14:19.804 SummerGuide[9892:20b] DB Query: SELECT * FROM settings

the strange thing is if i change my path to for example /baseeeeee.sqlite (which ain't no file ) it still says "DB Open...."

I just added an existing sqlite file (base.sqlite) to my recourse folder and added the sqlite3 lib to my frameworks.

4

1 回答 1

3

要在应用程序包中获取文件的路径,请使用以下命令:

[[NSBundle mainBundle] pathForResource:@"base" ofType:@"sqlite"];

要真正打开数据库,您可能必须先将其复制到 Documents 目录,因为尝试修改应用程序包中的文件是个坏消息。

于 2009-05-11T15:04:12.870 回答