ユーザ用ツール

サイト用ツール


サイドバー

reactnative:db:sqlite

以前のリビジョンの文書です


SQLite

expoに含まれるデータベース
SQLite

SQLite入門

基本

import { Constants, SQLite } from 'expo';

const db = SQLite.openDatabase('SampleDatabase.db');

・・・
 componentDidMount(e){

    db.transaction(tx => {
      tx.executeSql(
        'create table if not exists items (id integer primary key not null, done int, value text);'
      );
    });

    db.transaction(
      tx => {
        tx.executeSql('insert into items (done, value) values (0, ?)', ["safa"]);
        tx.executeSql('select * from items', [], (_, { rows }) =>
          console.log(JSON.stringify(rows))
        );
      },
      null,
      console.log("test")
    );

executeSql

tx.executeSql(sqlStatement, arguments, success, error)
sqlStatement:SQL
arguments:SQLに?を使用すると、置換え変数にできる。(プリペアードステートメント)
success,error:それぞれ自身のトランザクションと、結果の2つのパラメータが返る。

tx.executeSql('select * from items where name=?', 
          ["safa"], 
          (tran, { rows }
          ) => console.log(JSON.stringify(rows))
        );

ROWID

SQLiteにはデータをINSERTすると自動でROWIDがつく。
SELECTでは明記しないと取得できないので注意。

SQLite Autoincrement

ROWIDの利用

 
    db.transaction(
      tx => {
        tx.executeSql('select *,ROWID from items', [], (_, { rows }) => {
            console.log(JSON.stringify(rows))
            console.log({count:rows.length})
          }
        );
      },
      null,
    );
reactnative/db/sqlite.1549783396.txt.gz · 最終更新: 2019/02/10 16:23 by ips