How ON CONFLICT Works in SQLite

SQLite has the ON CONFLICT clause that allows you to specify how to handle constraint conflicts. It applies to UNIQUE, NOT NULL, CHECK, and PRIMARY KEY constraints (but not FOREIGN KEY constraints).

There are five possible options you can use with this clause:

  • ABORT
  • FAIL
  • IGNORE
  • REPLACE
  • ROLLBACK

This article provides examples and an explanation of each of these options.

The ON CONFLICT clause is used in CREATE TABLE statements, but it can also be used when inserting or updating data by replacing ON CONFLICT with OR.

Read more

How to Skip Rows that Violate Constraints When Inserting Data in SQLite

In SQLite, when you try to insert multiple rows into a table, and any of those rows violates a constraint on that table, the operation will fail.

This is to be expected, after all, that’s what the constraint is for.

But what if you just want to ignore any rows that violate constraints? In other words, if a row violates a constraint, you want SQLite to skip that row, then carry on processing the next row, and so on.

Fortunately, there’s an easy way to do this in SQLite.

Read more

3 Ways to Backup an SQLite Database

When it comes to backing up your databases in SQLite, you have a few options as to how to go about it.

In particular, you can use one of the following methods:

  • Use the .backup command to back up a specified database
  • Use the .dump command to export the database to a .sql file
  • Use the .clone command to clone the database

Read more

How SQLite Char() Works

In SQLite, the char() function returns a string based on the unicode code points provided as arguments.

You can provide one or more arguments, each of which is a unicode code point. The function then returns a string based on those code points.

Read more