无法在ios中更新数据库表

时间:2013-10-17 06:40:32

标签: ios database sqlite

我需要更新我的sqlite表格。所以我写了一个这样的查询

 NSString *updateSQL = [NSString stringWithFormat: @"UPDATE LOCALPLAYLISTSONGS SET SONGNAME=\"%@\",SONGPATH=\"%@\" WHERE PLAYLISTNAME=\"%@\"",SongTitle,songPath,playlistName];

    const char *update_stmt = [updateSQL UTF8String];
    sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL);
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        status=@"song Added";
    }
    else
    {
        status=@"Error occured";
    }

问题是这总是显示状态为Error Occured。我的查询有什么问题。请告诉我

由于

更新

    -(NSString *)UpdateLocalPlaylist :(NSString *)playlistName :(NSString *)SongTitle :  

    (NSString *)songPath
     {
         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 *updateSQL = [NSString stringWithFormat: @"UPDATE LOCALPLAYLISTSONGS SET SONGNAME=%@,SONGPATH=%@ WHERE PLAYLISTNAME=%@",SongTitle,songPath,playlistName];

        const char *update_stmt = [updateSQL UTF8String];
        sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            status=@"song Added";
        }

        else
        {
            status=@"Error occured";
        }

        return status;

    }

    else
        return @"Error Occured";

}

4 个答案:

答案 0 :(得分:1)

SQL对字符串使用单引号('),而不是double(“)。双引号仅用于对象名称(表,列......)。

答案 1 :(得分:0)

试试这个......

NSString *updateSQL = [NSString stringWithFormat: @"UPDATE LOCALPLAYLISTSONGS SET
 SONGNAME='%@',SONGPATH='%@' WHERE PLAYLISTNAME='%@'",SongTitle,songPath,playlistName];

答案 2 :(得分:0)

你试试这个:

update_stmt = nil;
    NSString *updateSQL = [NSString stringWithFormat: @"UPDATE LOCALPLAYLISTSONGS SET SONGNAME=%@,SONGPATH=%@ WHERE PLAYLISTNAME=%@",SongTitle,songPath,playlistName];

答案 3 :(得分:0)

试试这个也让我知道

以这种格式声明您的查询(如前所述)

NSString *updateSQL = [NSString stringWithFormat: @"UPDATE LOCALPLAYLISTSONGS SET SONGNAME=\"%@\",SONGPATH=\"%@\" WHERE PLAYLISTNAME=\"%@\"",SongTitle,songPath,playlistName];

并添加了这样的条件来检查

if (sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL)) == SQLITE_OK)
            {
                if (sqlite3_step(statement) == SQLITE_DONE)
                {
                    status=@"song Added";
                }

                else
                {
                    status=@"Error occured";
                }
            }