1

这是我用来Appdelegate.m复制数据库的代码

- (void) copyDatabaseIfNeeded {

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSError *error;
    NSString *dbPath = [self getDBPath];

    BOOL success = [fileManager fileExistsAtPath:dbPath];


    if(!success) {

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

        if (!success)
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }

}

这用于获取 DBPath

- (NSString *) getDBPath {


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    return [documentsDir stringByAppendingPathComponent:@"LocalSongs.sqlite"];


}

这是我的播放列表插入方法

-(NSString *)InsertPlaylist :(NSString *)PlaylistName
{
    NSLog(@"passed");
    NSString *status;


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *dbPath=[[NSString alloc]initWithString:[documentsDir stringByAppendingPathComponent:@"LocalSongs.sqlite"]];

    NSLog(@"Database Path %@",dbPath);

    sqlite3_stmt    *statement;


    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

        NSLog(@"open");

        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO LOCALPLAYLIST (PLAYLISTNAME,getdate()) VALUES (\"%@\")",PlaylistName];

        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            status=@"Playlist Created";
        }

        else
        {
            status=@"Error occured";
        }


        return status;

    }
}

我遇到的问题是这个 prepare_v2 总是没有完成。它总是执行 else 部分。

if (sqlite3_step(statement) == SQLITE_DONE)

这有什么问题?请帮我

4

1 回答 1

1

我认为您的查询有误:您应该在放置的位置放置一个列名getdate(),即,而不是

INSERT INTO LOCALPLAYLIST (PLAYLISTNAME,getdate()) VALUES (\"%@\")

你应该使用类似的东西

INSERT INTO LOCALPLAYLIST (PLAYLISTNAME,MYDATECOLUMN) VALUES (\"%@\",getdate())

指针后sqlite3_prepare_v2是?statementNULL

将 和 的返回值sqlite3_prepare_v2http://www.sqlite.org/c3ref/c_abort.htmlsqlite3_step中的返回值进行比较,以更好地了解问题所在

于 2013-08-30T02:10:01.540 回答