【问题标题】:REPLACE function of SQLite does not work on objective c iosSQLite 的 REPLACE 功能在目标 c ios 上不起作用
【发布时间】:2018-09-09 12:09:04
【问题描述】:

我正在使用 SQLite,但是当我替换它一次又一次添加到我的数据库中的数据时,基本上我想存储 alarm name、alarm type 和 alarm tune path,应该只有 6 rows 在NotiType 因为我在每一列中都插入了 6 个元素。但是这里的问题是当我每天更换警报名称类型和调整时的数据时,但是数据被插入到下面的每一列中,并且当表格调用替换数据函数时计数。

if([fileManager fileExistsAtPath:_dbPath] == YES) {
    const char *dbPathagain = [_dbPath UTF8String];
    if(sqlite3_open(dbPathagain, &_DB) == SQLITE_OK ) {
        NSLog(@"database is open");
        char *errorMessage;

        const char *sql_statement = "CREATE TABLE IF NOT EXISTS NotiType(Alarm TEXT, Type TEXT, TPath TEXT)";


        NSLog(@"created table success");
        // sqlite3_stmt *statement = NULL;
        //  const char *dbPathagain = [ _dbPath UTF8String];

        if([fileManager fileExistsAtPath:_dbPath] == YES) {
            sqlite3_stmt *statement = NULL;
            const char *dbPathagain = [_dbPath UTF8String];
            if(sqlite3_open(dbPathagain, &_DB) == SQLITE_OK ) {
                for(int itemIndex = 0; itemIndex < [azanst count];itemIndex++){

                    NSString *myname = [azanst objectAtIndex:itemIndex];
                    NSString *mytype = [alarmsstype objectAtIndex:itemIndex];
                    NSString *mytunepath = [alarmtune objectAtIndex:itemIndex];

                    NSString *insertSQLData = [NSString stringWithFormat:@"REPLACE INTO NotiType(Alarm, Type, TPath) VALUES (\"%@\", \"%@\",\"%@\")", myname, mytype, mytunepath];
                    NSLog(@"here is alarm names %@", myname);
                    NSLog(@"here alarm types %@", mytype);
                    NSLog(@"here alarm tune path %@", mytunepath);
                    const char *insert_statement = [insertSQLData UTF8String];

                    sqlite3_prepare_v2(_DB, insert_statement, -1, &statement, NULL);

                    if (sqlite3_step(statement) == SQLITE_DONE) {
                        NSLog(@"data added successfully");
                    }
                    else {
                        NSLog(@"could not add timings");
                    }
                }
                sqlite3_finalize(statement);
                sqlite3_close(_DB);
            }
        }
        if (sqlite3_exec(_DB, sql_statement, NULL, NULL, &errorMessage) != SQLITE_OK) {
            NSLog(@"failed to insert in table");
        }
        sqlite3_close(_DB);
    }
    else {
        NSLog(@"failed to open db or cretate table");
        NSLog(@"Database Error Message : %s", sqlite3_errmsg(_DB));
    }
}

更新

我正在做这样的事情

     for (i=0, i<= ID.count, i++) {

                    NSString *insertSQLData = [NSString stringWithFormat:@"UPDATE NotiType SET Alarm =" + myname "," + "Type =" + mytype "," + "Tpath =" mytunepath + " where ID == i"];
                    }

【问题讨论】:

  • 如果你想访问REPLACE键然后设置任何一个键作为主键
  • 哪个应该是主键??
  • 选择是你的,你的表主标识例如登录页面(用户电子邮件是主键)是主键

标签: ios objective-c sqlite replace


【解决方案1】:

您忘记提供 WHERE 语句:

NSString *insertSQLData = [NSString stringWithFormat:@"REPLACE INTO NotiType(Alarm, Type, TPath) VALUES (\"%@\", \"%@\",\"%@\")", myname, mytype, mytunepath];

您应该添加类似 WHERE _id = %@, myAlarmUniqIdentifier 的内容,以确保您只更改正在编辑的那个

编辑:想想看,我也建议使用 UPDATE,我不确定 REPLACE 是否是你想要的......

UPDATE {table} SET {column1} = {editedObject.value1}, {column2} = {editedObject.value2} WHERE {uniqueIdField} = {editedObject.identifier};

编辑 2: 在您进行编辑后,像这样更新您的 for 循环:

 NSString *myname = [azanst objectAtIndex:i];
 NSString *mytype = [alarmsstype objectAtIndex:i];
 NSString *mytunepath = [alarmtune objectAtIndex:i];
 NSNumber *myid = [ID objectAtIndex:i]; //<-- get the unique identifier for the item number ‘i‘
[NSString stringWithFormat:@"UPDATE NotiType SET Alarm=%@, Type=%@, Tpath=%@ where ID=%@", myname, mytype, mytunepath, myid];

【讨论】:

  • 每个数据都在更新,实际上每列有六个元素,每天每六个元素都在更改,行数不应超过六个
  • Replace 将替换所有行。因此,如果没有 WHERE 子句,您将使用相同的值覆盖数据库的所有字段。我认为这不是您想要的,因此您需要创建一个包含唯一标识符的字段(自动增量效果很好)并更新每一行 WHERE identifier == rowToBeEdited.identifier
  • 听起来不错,但我应该让 ID 自动递增吗??
  • 我编辑了我的答案,我认为您应该考虑使用 UPDATE 而不是更新。是的,你的 id 字段应该是自动增量的,除非你想添加一些逻辑来自己处理,只需在你的 create 语句中添加一个属性 INTEGER AUTOINCREMENT 的列
  • 但最后一个查询对我来说似乎模棱两可
猜你喜欢
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 2018-10-29
  • 1970-01-01
  • 2017-07-02
相关资源
最近更新 更多