怎么办SQL插入或更换同样的操作,在不改变主键? [英] How to do SQL Insert or Replace like operation without changing primary key?

查看:539
本文介绍了怎么办SQL插入或更换同样的操作,在不改变主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图插入一条记录到一个SQLite数据库表。该表有一栏URL和一个主键自动递增整数_id的唯一约束。

I'm trying to insert a record into a SQLite database table. The table has a Unique constraint on a column "URL" and a primary key auto increment integer "_id".

我使用的插入或更换为我解决冲突,因为我需要被持久化的新数据。但是,这种方法是递增的主键和搞乱了一些外键在其他表。

I'm using insert or replace as my conflict resolution, since I need the new data to be persisted. However, this method is incrementing the primary key and messing up some foreign keys in other tables.

什么是覆盖记录而不更新_id最好的方法是什么?

What's the best way to overwrite the record without updating the _id?

推荐答案

简单的答案是停止使用替换语法。这会导致删除旧的记录,那么一个新的加...这将增加你的索引。

The simple answer is to stop using Replace syntax. This causes the old record to be deleted then a new one added ... which would increment your index.

利用UPDATE语法来处理冲突,而不是

Utilize the UPDATE syntax to handle conflicts instead

编辑:

如果你真的偏替换语法,那么它将付出了代价。您将需要编写额外的code,更新旧索引的所有preV出现的新的。不是太辛苦,但这将纠正同步指标的问题

If you are really partial to the Replace syntax then it will come at a cost. You will need to write additional code that updates all prev occurrences of the old index to the new one. Not overly hard but this will correct the issue of synchronizing indexes

文件[上市在替换部件上没有办法下页]: http://www.sqlite.org /lang_conflict.html

Documentation [Listed under REPLACE section little ways down the page]: http://www.sqlite.org/lang_conflict.html

这篇关于怎么办SQL插入或更换同样的操作,在不改变主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆