Iphone SQlite - 从文本字段填充表

时间:2010-02-21 00:29:26

标签: objective-c iphone sqlite

我正在尝试从文本文件中预填充我的SQlite表 - 尽管它编译得很好,但不会插入任何行:

NSLog(@"Insert Table for English");


    char *errorMsg;

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath ] stringByAppendingPathComponent:@"english.sql"];

    NSLog(@"DefaultPath: %@", defaultDBPath);

    NSString *sql = [[[NSString alloc] initWithContentsOfFile:defaultDBPath  
                                           encoding:NSUTF8StringEncoding 
                                              error:NULL] autorelease];

    if (sqlite3_exec(database,[sql UTF8String],NULL,NULL, &errorMsg) != SQLITE_OK) {
        NSAssert1(0, @"Error loading update file: %s", errorMsg);
    }

    NSLog(@"I should have written something");

这就是我的文本文件的样子:

BEGIN TRANSACTION;
INSERT INTO PARTNER(branche) VALUES('Choose music for the Ceremony');
COMMIT;

我在这里创建数据库:

-(void)createDatabase {

NSUserDefaults *userSETTINGS = [NSUserDefaults standardUserDefaults];

NSLog(@"HUHU - schau ma mal: Database = %d", [userSETTINGS integerForKey:@"Database"]);


if ([userSETTINGS integerForKey:@"Database"] == 0) {


    if (sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK) {
        sqlite3_close(database);
        NSAssert(0, @"Failed to open database");
    }

    char *errorMsg;
    //sqlite3_stmt *statement;
    NSString *createSQL = @"CREATE TABLE IF NOT EXISTS PARTNER (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, branche TEXT, company TEXT, name TEXT, phone TEXT, email TEXT, price INT, notes TEXT, done TEXT);";
    if (sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) {
        sqlite3_close(database);
        NSAssert1(0, @"Error createing table: %s", errorMsg);
    }

    NSUserDefaults *userSETTINGS = [NSUserDefaults standardUserDefaults];
    [userSETTINGS setInteger:1 forKey:@"Database"];

    NSLog(@"HUHU - schau ma mal: Database = %d", [userSETTINGS integerForKey:@"Database"]);

[self fillDatabase];

} else {

    NSLog(@"Database wurde schon angelegt");
    [self readDatabase];

}

}

日志文件告诉我,defaultDBPath是:

  

DefaultPath:/ Users / sl / Library / Application Support / iPhone Simulator / User / Applications / 0BE2EDC6-F070-43BE-9666-310257B495B9 / WeddingPlanner.app / english.sql

我错过了什么?数据库是正确的,路径是正常的,但没有任何内容可写......

感谢您的回答,帮助,建议......: - )

BR,

的Stefan


再次感谢您的不断帮助......

我改变如下:

if (sqlite3_open_v2([[self dataFilePath] UTF8String], &database, SQLITE_OPEN_READWRITE, nil) != SQLITE_OK) {
        sqlite3_close(database);
        NSAssert(0, @"Failed to open database");
    }

    NSLog(@"Insert Table for English");


    char *errorMsg;

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath ] stringByAppendingPathComponent:@"e.sqlite"];

    NSLog(@"DefaultPath: %@", defaultDBPath);

    NSString *sql = [[[NSString alloc] initWithContentsOfFile:defaultDBPath  
                                           encoding:NSUTF8StringEncoding 
                                              error:NULL] autorelease];

    if (sqlite3_exec(database,[sql UTF8String],NULL,NULL, &errorMsg) != SQLITE_OK) {
        NSAssert1(0, @"Error loading update file: %s", errorMsg);
    }

    NSLog(@"Hier hätte ich schreiben sollen");

我的日志看起来像:

  

2010-02-21 20:37:41.666 WeddingPlanner [61189:207]没有开始   2010-02-21 20:37:41.668 WeddingPlanner [61189:207] HUHU - schau ma mal:   数据库= 1 2010-02-21 20:37:41.669 WeddingPlanner [61189:207]   数据库wurde schon angelegt 2010-02-21 20:37:41.670   WeddingPlanner [61189:207] Abfrage:SELECT SUM(price)FROM PARTNER   2010-02-21 20:37:41.671 WeddingPlanner [61189:207] Nil开始   2010-02-21 20:37:41.672 WeddingPlanner [61189:207] Die gesetzte   Sprache :( null)2010-02-21 20:37:41.682 WeddingPlanner [61189:207]   Abfrage:SELECT SUM(price)FROM PARTNER 2010-02-21 20:37:41.683   WeddingPlanner [61189:207] Nil on start 2010-02-21 20:37:41.685   WeddingPlanner [61189:207] HUHU - schau ma mal:(null)2010-02-21   20:37:41.685 WeddingPlanner [61189:207] calculateDateDifference   2010-02-21 20:37:41.691 WeddingPlanner [61189:207] Das Datum DAY =   -3338 wait_fences:未收到回复:10004003 2010-02-21 20:37:56.597 WeddingPlanner [61189:207] English 2010-02-21 20:37:56.599   WeddingPlanner [61189:207] Die gespeicherte Sprache:EN 2010-02-21   20:37:56.600 WeddingPlanner [61189:207]插入英文表格   2010-02-21 20:37:56.602 WeddingPlanner [61189:207] DefaultPath:   / Users / sl / Library / Application Support / iPhone   模拟器/用户/应用/ D79C5BB0-B4E1-4AD7-8B9B-E4493D82F64D / WeddingPlanner.app / e.sqlite

所以外观看起来很好 - 将创建DB,可以打开DB(因为没有故障问题),我能够将(查看GUI)数据插入表“伙伴”,但我无法插入数据通过文件......

1 个答案:

答案 0 :(得分:0)