Sqlite更新命令无效

时间:2016-01-22 06:43:11

标签: ios objective-c sqlite

当我尝试更新表格时,给我一个

  

错误消息:附近"(":语法错误

请有人建议我纠正这个问题。

当我点击位于下方的工具栏上的保存按钮(条形按钮项目)时,我正在更新表格。

问题在于它始终是sqlite3_step的其他陈述。

我不知道这个问题的确切问题以及上面提到的错误。

任何建议都将不胜感激。

以下是我的更新方法:

   if ([databaseManager didExistDatabaseInDocumentDirectory] == YES)
    {
        const char *dbpath = [[databaseManager getDatabasePathFromDocument] UTF8String];

        sqlite3_stmt *stmt;
        if (sqlite3_open(dbpath, &scanningDB) == SQLITE_OK)
        {
            const char *update_stmt1 = "UPDATE FILES SET (documentType,orderNumber) VALUES (?,?) WHERE ID = ?";
            sqlite3_prepare_v2(scanningDB, update_stmt1, -1, &stmt, NULL);

            sqlite3_bind_text(stmt, 1, [str UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 2, [str1 UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_int(stmt, 3,[num intValue]);

            if(sqlite3_step(stmt)==SQLITE_DONE)
            {
                sqlite3_reset(stmt);
                NSLog(@"Record Updated Successfully");

            }
            else
            {
                NSLog(@"Could not Update Record");
                NSLog(@" error message %s",sqlite3_errmsg(scanningDB));
                sqlite3_finalize(stmt);
                sqlite3_close(scanningDB);
                return NO;
            }
            sqlite3_finalize(stmt);
            sqlite3_close(scanningDB);

        }
        else
            return NO;
    }
    else
        return NO;

2 个答案:

答案 0 :(得分:1)

请使用以下内容更正您的查询。

const char *update_stmt1 = "UPDATE FILES SET documentType = ? ,orderNumber = ? WHERE ID = ?

希望这会对你有所帮助。

答案 1 :(得分:0)

您的更新查询错误。

UPDATE FILES SET (documentType,orderNumber) VALUES (?,?) WHERE ID = ?

<强>语法:

UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

您的查询:

UPDATE FILES SET documentType =?, orderNumber =? where ID =?, documentTypeString, Yourordernumber, yourID

<强>示例:

   - (void) update
        {
            if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
            {
                if(updateStatement == nil)
                {
                    const char *sql = "UPDATE FILES SET documentType =?, orderNumber =? where ID =?";
                    if(sqlite3_prepare_v2(database, sql, -1, & updateStatement, NULL) != SQLITE_OK)
                        NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
                }

               sqlite3_bind_text(documentType
, 1, [comment UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(orderNumber
, 2, [categoria UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_int(ID, 3, id);


                if(SQLITE_DONE != sqlite3_step(updateStatement))
                    NSAssert1(0, @"Error while updating data. '%s'", sqlite3_errmsg(database));
                else
                    //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
                    //noteID = sqlite3_last_insert_rowid(database);

                    //Reset the update statement.
                    sqlite3_reset(updateStatement);
                sqlite3_close(database);
                deleteStatement = nil;
            }
            else
                sqlite3_close(database);
        }

基础SQLite教程: http://www.tutorialspoint.com/sqlite/index.htm