expo-sqlite
gives your app access to a database that can be queried through a WebSQL-like API. The database is persisted across restarts of your app.Android Device | Android Emulator | iOS Device | iOS Simulator | Web |
---|---|---|---|---|
expo install expo-sqlite
If you're installing this in a bare React Native app, you should also follow these additional installation instructions.
import * as SQLite from 'expo-sqlite';
Database
object. On disk, the database will be created under the app's documents directory, i.e. ${FileSystem.documentDirectory}/SQLite/${name}
.version
, description
and size
arguments are ignored, but are accepted by the function for compatibility with the WebSQL specification.Database
object, described below.Database
objects are returned by calls to SQLite.openDatabase()
. Such an object represents a connection to a database on your device. They support one method:db.transaction(callback, error, success)
Transaction
(see below) as its only parameter, on which it can add SQL statements to execute.Transaction
object is passed in as a parameter to the callback
parameter for the db.transaction()
method on a Database
(see above). It allows enqueuing SQL statements to perform in a database transaction. It supports one method:tx.executeSql(sqlStatement, arguments, success, error)
?
placeholder feature of the method to avoid against SQL injection attacks, and to never construct SQL statements on the fly.?
placeholders, with values to be substituted listed in the arguments
parameter.?
placeholders in the SQL statement.ResultSet
object (see below) with the results of the query.ResultSet
objects are returned through second parameter of the success
callback for the tx.executeSql()
method on a Transaction
(see above). They have the following form:{ insertId, rowsAffected, rows: { length, item(), _array, }, }
rows.item(index)
returns the row with the given index
. If there is no such row, returns null
.rows.item()
..db
file you already have, you need to do three things:expo install expo-file-system expo-asset
metro.config.js
file in the root of your project with the following contents (curious why? read here):const { getDefaultConfig } = require('expo/metro-config'); const defaultConfig = getDefaultConfig(__dirname); module.exports = { resolver: { assetExts: [...defaultConfig.resolver.assetExts, 'db'], }, };
async function openDatabase(pathToDatabaseFile: string): SQLite.WebSQLDatabase { if (!(await FileSystem.getInfoAsync(FileSystem.documentDirectory + 'SQLite')).exists) { await FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + 'SQLite'); } await FileSystem.downloadAsync( Asset.fromModule(require(pathToDatabaseFile)).uri, FileSystem.documentDirectory + 'SQLite/myDatabaseName.db' ); return SQLite.openDatabase('myDatabaseName.db'); }
Please note that you should use this kind of execution only when it is necessary. For instance, when code is a no-op within transactions (like eg.PRAGMA foreign_keys = ON;
).
const db = SQLite.openDatabase('dbName', version); db.exec([{ sql: 'PRAGMA foreign_keys = ON;', args: [] }], false, () => console.log('Foreign keys turned on') );