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. [Eclipse] Eclipse字体问题解决

    背景: Eclipse的字体总感觉有点问题,其中中文字体太小,不方便查看,今天网上搜索了一下,解决了问题,记录下来: 解决办法: Window --> Preferences --> Ge ...

  2. 遍历windows驱动

    驱动都存在 \\Driver 或者 \\FileSystem 目录对象里 我们只需要遍历这两个目录就可以遍历windows所有驱动 知识点 \\Driver  \\FileSystem (dt _OB ...

  3. 动态平衡二叉搜索树的简易实现,Treap 树

    http://blog.csdn.net/qichi_bj/article/details/8232048

  4. mysql一些有用的链接

    1.mysql安装:http://jingyan.baidu.com/article/f79b7cb35c0f439144023e38.html

  5. 使用Hue上传hive数据

    大概逻辑是先上传hdfs数据,然后创建hive外部表,关联到hdfs上传数据的位置. 截图比较概要,但是用起来很简单 1.创建路径和上传文件 2.创建外部表

  6. 关闭Android/iPhone浏览器自动识别数字为电话号码

    <meta name="format-detection" content="telephone=no"><meta http-equiv=& ...

  7. 把所有css放到一个css文件的格式

    @charset "utf-8"; @import url("global.css"); @import url("common.css") ...

  8. psql-02基本语法

    客户端 数据库: 创建:createdb mydb; 删除: dropdb mydb; 连接: 连接: psql mydb; 断开连接: \q 查看当前版本: select version(); 直接 ...

  9. css3 总结01

    前缀 chrome: -webkit- safari: -webkit- firefox: -moz- ie: -ms- opera: -o- 书写的时候应该先用有前缀的样式,再用无前缀的样式 颜色 ...

  10. 黑客语(Leet)

    黑客语(Leet)   Leet是从网络发展起来的一种文字书写方式.通常将英语中的字母替换为数字和特殊符号.这种方式被很多黑客组织所使用.由于具有隐密性,所以它也广泛被用于密码中.使用Leet书写的密 ...