expo-sqlite
expo-sqlite
オープンベータ版ドキュメント:本テクニカルドキュメントは、リリース前のオープンベータ版の一部としてAmazonから提供されるものです。ここで説明されている機能は、Amazonがフィードバックを受け取り、機能の開発を繰り返す過程で変更される可能性があります。最新の機能の情報については、リリースノートを参照してください。
@amazon-devices/expo-sqliteは、アプリにデータベースへのアクセスを提供します。WebSQLと同様のAPIを通じて、データベースにクエリを実行できます。データベースはアプリの再起動後も保持されます。
インストール
package.jsonファイルにJavaScriptライブラリの依存関係を追加します。dependencies: { ... "@amazon-devices/expo-sqlite": "~2.0.0", "@amazon-devices/keplerscript-turbomodule-api": "~1.0.0", "expo": "~50.0.0", ... }npm installコマンドを使用して依存関係を再度インストールし、kepler buildコマンドを使用してアプリを再度ビルドします。
例
以下の例は、典型的なアプリにおけるデータベース操作フロー全体を示す包括的な例です。
import {openDatabase, SQLResultSet} from '@amazon-devices/expo-sqlite';
import React, {useEffect, useState} from 'react';
import {Button, StyleSheet, Text, View} from 'react-native';
let db = openDatabase('database.db');
export const App = () => {
const [rows, setRows] = useState<any[][]>([]);
const [dbClosed, setDbClosed] = useState(false);
// データベースを作成します
useEffect(() => {
db.transaction((tx) => {
tx.executeSql(
'create table if not exists items (id integer primary key not null, done int, value text, floating double, int integer);',
);
});
}, []);
const handleSelectResult = (result: SQLResultSet) => {
if (result.rows.length > 0) {
const columnNames = Object.keys(result.rows._array[0]);
const allRows = result.rows._array.map(Object.values);
setRows([columnNames, ...allRows]);
} else {
setRows([]);
}
};
// データベースを開きます
const openDb = () => {
db = openDatabase('database.db');
db.transaction((tx) => {
tx.executeSql(
'create table if not exists items (id integer primary key not null, done int, value text, floating double, int integer);',
);
});
setDbClosed(false);
};
// データベースを同期的に閉じます
const closeDbSync = () => {
db.closeSync();
setDbClosed(true);
};
// データベースを非同期で閉じます
const closeDbAsync = async () => {
await db.closeAsync();
setDbClosed(true);
};
// 挿入操作を同期的に実行します
const transactionSync = () => {
db.transaction((tx) => {
const done = Math.round(Math.random());
const value = 'text-' + Date.now().toString().slice(-4);
const float = Number((Math.random() * 10).toFixed(2));
const int = Math.round(Math.random() * 1000);
tx.executeSql(
'insert into items (done, value, floating, int) values (?, ?, ?, ?)',
[done, value, float, int],
() => {},
);
});
};
// 削除操作を非同期で実行します
const transactionAsync = async () => {
await db.transactionAsync(async (tx) => {
await tx.executeSqlAsync(
'delete from items where id = (select max(id) from items)',
);
});
};
// 選択トランザクションを同期的に実行します
const readTransactionSync = () => {
db.readTransaction((tx) => {
tx.executeSql('select * from items', [], (_, result) =>
handleSelectResult(result),
);
});
};
// データベースを削除する
const deleteDb = () => {
db.deleteAsync();
};
return (
<View style={styles.container}>
<View style={styles.sideColumn}>
<Button title="open" onPress={openDb} disabled={!dbClosed} />
<Button title="close sync" onPress={closeDbSync} disabled={dbClosed} />
<Button
title="close async"
onPress={closeDbAsync}
disabled={dbClosed}
/>
<Button
title="transaction sync (insert)"
onPress={transactionSync}
disabled={dbClosed}
/>
<Button
title="transaction async (delete)"
onPress={transactionAsync}
disabled={dbClosed}
/>
<Button
title="同期的な読み取りトランザクション(選択)"
onPress={readTransactionSync}
disabled={dbClosed}
/>
<Button
title="データベースを削除"
onPress={deleteDb}
disabled={!dbClosed}
/>
</View>
<View style={styles.mainColumn}>
<Text style={styles.label}>Database version: {db.version}</Text>
<Text style={styles.label}>Database open: {String(!dbClosed)}</Text>
<View style={styles.table}>
{rows.map((row) => (
<View style={styles.row}>
{row.map((col) => (
<View style={styles.cell}>
<Text style={styles.cellText}>{String(col)}</Text>
</View>
))}
</View>
))}
</View>
</View>
</View>
);
};
const styles = StyleSheet.create({
scrollView: {
flex: 1,
},
container: {
flex: 1,
backgroundColor: 'white',
flexDirection: 'row',
padding: 20,
},
sideColumn: {
flex: 1,
flexDirection: 'column',
padding: 10,
},
mainColumn: {
flex: 2,
flexDirection: 'column',
padding: 10,
},
label: {
color: 'black',
fontSize: 32,
flexDirection: 'column',
},
table: {
alignItems: 'center',
justifyContent: 'center',
},
row: {
flexDirection: 'row',
height: 40,
},
cell: {
flex: 1,
borderColor: 'black',
borderWidth: 1,
alignItems: 'center',
justifyContent: 'center',
},
cellText: {
color: 'black',
fontSize: 20,
},
});
APIリファレンス
Expoの公式ドキュメント(英語のみ)を参照してください。
クラス
| クラス | 説明 |
|---|---|
ExpoSQLTransactionAsync |
非同期トランザクションAPIの内部データ構造。 |
SQLError |
データベース操作に関連するエラーを処理するために使用されるクラス。 |
SQLiteDatabase |
openDatabase()によって返されるデータベース。 |
SQLErrorのプロパティ
| プロパティ | 説明 |
|---|---|
code |
エラーコード。 |
message |
エラーメッセージ。 |
CONSTRAINT_ERR |
制約違反を示します。 |
DATABASE_ERR |
データベースに関連する一般的なエラーを示します。 |
SYNTAX_ERR |
SQLステートメントの構文エラーを示します。 |
TOO_LARGE_ERR |
処理中のデータが大きすぎて処理できないことを示します。 |
UNKNOWN_ERR |
不明なエラーまたは予期しないエラーが発生したことを示します。 |
VERSION_ERR |
SQLiteデータベースのバージョンと@amazon-devices/expo-sqliteが想定するバージョンとの間に互換性の問題があることを示します。 |
SQLiteDatabaseのプロパティ
| プロパティ | 説明 |
|---|---|
close |
データベースを閉じるために使用できるメソッド。警告: このメソッドは非推奨です。代わりにcloseAsync()を使用してください。 |
version |
データベースの現在のバージョン。 |
SQLiteDatabaseのメソッド
| メソッド | 説明 |
|---|---|
closeAsync |
データベースを閉じます。 |
deleteAsync |
データベースファイルを削除します。削除する前にデータベースを閉じる必要があります。 |
exec |
SQLステートメントを実行し、その結果で解決されるコールバックを返します。 |
execAsync |
SQLステートメントを実行し、その結果で解決されるPromiseを返します。 |
readTransaction |
データベースを読み取り専用としてロックしてトランザクションを実行します。 |
transaction |
データベーストランザクションを実行します。 |
transactionAsync |
Promiseをサポートして新しいトランザクションを作成します。 |
メソッド
| メソッド | 説明 |
|---|---|
openDatabase |
データベースを開き、Databaseオブジェクトを返します。データベースが存在しない場合は作成されます。ディスク上では、アプリのドキュメントディレクトリ(${FileSystem.documentDirectory}/SQLite/${name})にデータベースが作成されます。 |
サポートされているバージョン
| パッケージのバージョン | ベース | @amazon-devices/react-native-kepler version |
|---|---|---|
| 2.0.x | 11.8.0 | 2.0.x |
関連リソース
その他のライブラリについては、サポート対象のサードパーティのライブラリとサービスを参照してください。
Last updated: 2025年9月30日

