Perform UPSERT / INSERT OR UPDATE against a SQLite Database
Option 1: You can afford deleting the row
In other words, you don't have foreign key, or if you have them, your SQLite engine is configured so that there no are integrity exceptions. The way to go is INSERT OR REPLACE. If you are trying to insert/update a player whose ID already exists, the SQLite engine will delete that row and insert the data you are providing. Now the question comes: what to do to keep the old ID associated?
Let's say we want to UPSERT with the data user_name="steven" and age=32.
Look at this code:
INSERT INTO players (id, name, age)
VALUES(coalesce((select id from players where user_name="steven"),(select max(id)from drawings)+1),32)
The trick is in coalesce. It returns the id of the user "steven" if any, and otherwise, it returns a new fresh id.
Option 2: You cannot afford deleting the row
After monkeying around with the previous solution, I realized that in my case that could end up destroying data, since this ID works as a foreign key for other table. Besides, I created the table with the clause ON DELETE CASCADE, which would mean that it'd delete data silently. Dangerous.
So, I first thought of a IF clause, but SQLite only has CASE. And this CASE can't be used (or at least I did not manage it) to perform one UPDATE query if EXISTS(select id from players where user_name="steven"), and INSERT if it didn't. No go.
And then, finally I used the brute force, with success. The logic is, for each UPSERT that you want to perform, first execute a INSERT OR IGNORE to make sure there is a row with our user, and then execute an UPDATE query with exactly the same data you tried to insert.
Same data as before: user_name="steven" and age=32.
-- make sure it exists --
INSERTOR IGNORE INTO players (user_name, age)
VALUES("steven",32); -- make sure it has the right data --
UPDATE players
SET user_name="steven", age=32WHERE user_name="steven";
Perform UPSERT / INSERT OR UPDATE against a SQLite Database的更多相关文章
- sqlite中的replace、insert、update之前的区别
本文转自http://www.ithao123.cn/content-933827.html,在此感谢作者 android数据库操作,有两种方式,一种用android提供给我们的数据库操作函数inse ...
- MongoDB入门系列(二):Insert、Update、Delete、Drop
概述 本章节介绍Insert.Update.Delete.Drop操作基本语法. 环境: Version:3.4 insert insert()基本语法如下: db.collection.insert ...
- should be mapped with insert="false" update="false
SSH项目出现了 should be mapped with insert="false" update="false 错误,仔细检查后发现,是两个不同的属性映射了表中的 ...
- Oracle一个事务中的Insert和Update执行顺序
今天碰到了一个奇怪的问题,是关于Oracle一个事务中的Insert和Update语句的执行顺序的问题. 首先详细说明下整个过程: 有三张表:A,B,C,Java代码中有一段代码是先在表A中插入一条数 ...
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
上篇文章<深入浅出Mybatis系列(六)---objectFactory.plugins.mappers简介与配置>简单地给mybatis的配置画上了一个句号.那么从本篇文章开始,将会介 ...
- 使用merge同时执行insert和update操作
SQL点滴18—SqlServer中的merge操作,相当地风骚 今天在一个存储过程中看见了merge这个关键字,第一个想法是,这个是配置管理中的概念吗,把相邻两次的更改合并到一起.后来在tech ...
- MySQL数据库INSERT、UPDATE、DELETE以及REPLACE语句的用法详解
本篇文章是对MySQL数据库INSERT.UPDATE.DELETE以及REPLACE语句的用法进行了详细的分析介绍,需要的朋友参考下 MySQL数据库insert和update语句引:用于操作数 ...
- oracle数据库高级应用之《自动生成指定表的insert,update,delete语句》
/* * 多条记录连接成一条 * tableName 表名 * type 类型:可以是insert/update/select之一 */ create or replace function my_c ...
- 利用insert,update和delete注入获取数据
0x00 简介 利用SQL注入获取数据库数据,利用的方法可以大致分为联合查询.报错.布尔盲注以及延时注入,通常这些方法都是基于select查询语句中的SQL注射点来实现的.那么,当我们发现了一个基于i ...
随机推荐
- PMP 第十二章 项目采购管理
1规划采购 2实施采购 3管理采购 4结束采购 1.合同的不同叫法?卖方的不同叫法? 2.规划采购管理的目的是什么?合同类型有哪些?不同的特点是什么?何种情况下应选择何种合同?自制或外购分析计算和合同 ...
- Ubuntu14.04LTS系统输入法的安装
由于安装的时候选择的是英文版,所以一进入系统问题就来了:无法输入中文. 我记得自己直接选的输入法是pinyin那个 在网上看到别人到blog,直接转过来吧,只为自己收藏下,如有需要请联系原作者. 转载 ...
- Debian下安装vim
问题描述:安装完系统以后,刚要打算开始写程序,发现,vim还没有装,用su -切换到root后 直接运行apt-get install vim,提示插入disc源,然后回车,陷入无法解决的状态. 上网 ...
- Linux下配置OpenCV1.0环境
自己一直嚷嚷着打算学学图像识别,识别个简单的,车牌号,验证码之类的,之前查过资料,OpenCV可以实现.昨天花了一个下午终于配置好环境了,今天写下总结. OpenCV这一名称包含了Open和Compu ...
- JQuery Event属性说明
JQuery事件中的Event属性是经常性的被忽略的.大多数时间你的确不怎么用它,但有些时候它还是有作用的. 如获知触发时用户的环境(是否按了shift etc).每个浏览器对event都有不同的地方 ...
- button hot key 热键
<Button x:Name="ScanIDButton" Margin="11,0,0,0" IsEnabled="{Binding Elem ...
- ios 音乐播放
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...
- Android 开发工具之Codota
Codota 的特性 的确,正如codota 官方所说,的确是精品,他的搜索源,不仅只有Github,而且还有知名博客和开发者网站,让你搜索一个东西,不用在找上半天: 除了搜索功能,首页的下方还罗列比 ...
- c++11 正则表达式基本使用
c++ 11 正则表达式 常用的方法 regex_match regex_search regex_replace 等. regex_match 要求正则表达式必须与模式串完全匹配,例如: strin ...
- 保利威视Polyv点播集成
demo和文档下载地址http://dev.polyv.net/2014/08/sdk/ 1.demo是eclipse的,所以导入android studio有几个要注意的地方 导入方式 在app的b ...