2

INSERT OR REPLACE INTO如果该行已经存在,SQLite 语句将替换该行。但是当我一次又一次地运行相同的语句时,它会继续插入而不是替换。

REPLACE 在什么情况下实际发生?

INSERT OR REPLACE INTO names (rollno, name) VALUES (1, "Adam")
4

3 回答 3

6

如果要插入的行与表中的另一行具有相同的 PRIMARY KEY,则会发生替换。您可能忘记定义主键。

于 2012-08-30T07:44:01.870 回答
3

Values will be replaced in some row if a UNIQUE violation occurs, as is explained in the documentation:

When a UNIQUE constraint violation occurs, the REPLACE algorithm deletes pre-existing rows that are causing the constraint violation prior to inserting or updating the current row and the command continues executing normally. If a NOT NULL constraint violation occurs, the REPLACE conflict resolution replaces the NULL value with he default value for that column, or if the column has no default value, then the ABORT algorithm is used. If a CHECK constraint violation occurs, the REPLACE conflict resolution algorithm always works like ABORT.

I presume that the column that you would to make UNIQUE is rollno. You can make it unique by making it the PRIMARY KEY of the table, or by simply creating a UNIQUE constrain on it.

于 2012-08-30T10:22:46.263 回答
1

哈哈哈,哦,伙计,我从哪里开始?

当我经历这个 SQL 地狱时,我正在诅​​咒计算机。

谢天谢地,我设法让它工作!:D

我从 INSERT OR REPLACE INTO 语句中学到的一件事是你需要做一件超级关键的事情:

Mark one column of your table as "UNIQUE" during SQL table creation.

这是一个示例(滚动到右端):

NSString *strEvents = @"CREATE TABLE Events (eventid INTEGER PRIMARY KEY, nodeID INTEGER, title VARCHAR(255), description TEXT, eventTime VARCHAR(255), eventDate VARCHAR(255), eventLocation VARCHAR(255), imagePaths TEXT, bookingURL TEXT, UNIQUE(nodeID));";

在上面的示例中,我已将“nodeID”列标记为唯一。

一旦你这样做了,你可以像这样使用它:

-(void)batchSaveEvents:(NSMutableArray *)paramList
{
    @synchronized(self)
    {
        if(self.isConnected == YES)
        {
            const char *sql = "INSERT OR REPLACE INTO Events (nodeID,title,description,eventTime,eventDate,eventLocation,imagePaths,bookingURL) VALUES (?,?,?,?,?,?,?,?);";

            sqlite3_stmt *stmtSave;

            if(sqlite3_prepare(objDatabase, sql, -1, &stmtSave, NULL) != SQLITE_OK)
            {
                //NSLog(@"FAILED %s", sqlite3_errmsg(objDatabase));

                return;
            }

            int listIndex = 0;        

            sqlite3_exec(objDatabase, "BEGIN IMMEDIATE TRANSACTION", 0, 0, 0); // Begin Trasaction

            for(listIndex = 0; listIndex < [paramList count]; listIndex++)
            {
                sqlite3_bind_int(stmtSave, 1, [[(Event *)[paramList objectAtIndex:listIndex] nodeID] intValue]);
                sqlite3_bind_text(stmtSave, 2, [[(Event *)[paramList objectAtIndex:listIndex] title] UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmtSave, 3, [[(Event *)[paramList objectAtIndex:listIndex] description] UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmtSave, 4, [[(Event *)[paramList objectAtIndex:listIndex] eventTime] UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmtSave, 5, [[(Event *)[paramList objectAtIndex:listIndex] eventDate] UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmtSave, 6, [[(Event *)[paramList objectAtIndex:listIndex] eventLocation] UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_bind_text(stmtSave, 7, [[(Event *)[paramList objectAtIndex:listIndex] imagePaths] UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_bind_text(stmtSave, 8, [[(Event *)[paramList objectAtIndex:listIndex] bookingURL] UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_step(stmtSave);
                sqlite3_reset(stmtSave);
            }

            sqlite3_exec(objDatabase, "COMMIT", 0, 0, 0); // Commit Transaction

            sqlite3_finalize(stmtSave);
        }
    }
}

祝布鲁达好运!……或者姐姐!

于 2012-08-30T09:00:00.447 回答