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的更多相关文章

  1. sqlite中的replace、insert、update之前的区别

    本文转自http://www.ithao123.cn/content-933827.html,在此感谢作者 android数据库操作,有两种方式,一种用android提供给我们的数据库操作函数inse ...

  2. MongoDB入门系列(二):Insert、Update、Delete、Drop

    概述 本章节介绍Insert.Update.Delete.Drop操作基本语法. 环境: Version:3.4 insert insert()基本语法如下: db.collection.insert ...

  3. should be mapped with insert="false" update="false

    SSH项目出现了 should be mapped with insert="false" update="false 错误,仔细检查后发现,是两个不同的属性映射了表中的 ...

  4. Oracle一个事务中的Insert和Update执行顺序

    今天碰到了一个奇怪的问题,是关于Oracle一个事务中的Insert和Update语句的执行顺序的问题. 首先详细说明下整个过程: 有三张表:A,B,C,Java代码中有一段代码是先在表A中插入一条数 ...

  5. 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete

    上篇文章<深入浅出Mybatis系列(六)---objectFactory.plugins.mappers简介与配置>简单地给mybatis的配置画上了一个句号.那么从本篇文章开始,将会介 ...

  6. 使用merge同时执行insert和update操作

    SQL点滴18—SqlServer中的merge操作,相当地风骚   今天在一个存储过程中看见了merge这个关键字,第一个想法是,这个是配置管理中的概念吗,把相邻两次的更改合并到一起.后来在tech ...

  7. MySQL数据库INSERT、UPDATE、DELETE以及REPLACE语句的用法详解

    本篇文章是对MySQL数据库INSERT.UPDATE.DELETE以及REPLACE语句的用法进行了详细的分析介绍,需要的朋友参考下   MySQL数据库insert和update语句引:用于操作数 ...

  8. oracle数据库高级应用之《自动生成指定表的insert,update,delete语句》

    /* * 多条记录连接成一条 * tableName 表名 * type 类型:可以是insert/update/select之一 */ create or replace function my_c ...

  9. 利用insert,update和delete注入获取数据

    0x00 简介 利用SQL注入获取数据库数据,利用的方法可以大致分为联合查询.报错.布尔盲注以及延时注入,通常这些方法都是基于select查询语句中的SQL注射点来实现的.那么,当我们发现了一个基于i ...

随机推荐

  1. 利用IdentityServer3在ASP.NET 5和Angular中实现OAuth2 Implicit Flow

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:之前介绍过的IdentityServer3虽然是基于Katana开发的,不过同样可以托 ...

  2. 1.ok6410移植bootloader,移植u-boot,学习u-boot命令

    ok6410移植u-boot 既然是移植u-boot当然首先需要u-boot源码,这里的u-boot代码是由国嵌提供的. 一.配置编译u-boot A. 解压 u-boot 压缩文件 B. 进入解压生 ...

  3. [Unity3D]上海某大型游戏公司的基础面试题

    一个小老乡跟我聊到去上海某大公司的基础面试题,面试结果不尽如人意,但还是分享了下面试的试题,刚刚第一次录制视频,给某人讲课,我感觉讲的还算比较耐心,但发现一些新手入门学习的弊端,可能是很普遍的现象,这 ...

  4. 通信原理实践(三)——FM调制

    一.FM调制 1.代码如下: clc,clear; fm = ; % 调制信号频率(Hz) Am = 0.5; % 调制信号幅度 fc = 5e3; % 载波频率(Hz) Ac = ; % 载波幅度 ...

  5. 对于一个负数mod正数

    鸟神说.. a/b靠零取整 然后呢..a%b定义成a-(a/b)*b c语言就是这么算的... 那么python2.6是怎么算的呢 如果最后你取模想得到一个正数.. 那么在上述取模定义不变的情况下 p ...

  6. Pig用户自定义函数(UDF)转

    原文地址:http://blog.csdn.net/zythy/article/details/18326693 我们以气温统计和词频统计为例,讲解以下三种用户自定义函数. 用户自定义函数 什么时候需 ...

  7. Liferay 6.2 改造系列之十八:修改登录Portlet配置,去除无用链接

    在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Input a list of sections that will b ...

  8. Python与Hack之window下运行带参数的Python脚本,实现一个简单的端口扫描器

    1.前提是:windows已经配置好Python的环境变量: 2.进入cmd命令行模式: **输入python命令,检测是否环境配置好:显示这样说明配置环境变量没问题 **用cd命令进入Python脚 ...

  9. express-6 请求和响应对象(1)

    URL的组成部分 协议: 协议确定如何传输请求.我们主要是处理http和https.其他常见的协议还有file和ftp. 主机名: 主机名标识服务器.运行在本地计算机(localhost)和本地网络的 ...

  10. NumPy 学习(2): 数组的操作

    1. 简单一维数组的操作 一维数组的操作类似于python自身的list类型. In [14]: arr = np.arange(10) In [15]: arr Out[15]: array([0, ...