ユーザ用ツール

サイト用ツール


reactnative:db:sqlite

差分

この文書の現在のバージョンと選択したバージョンの差分を表示します。

この比較画面にリンクする

次のリビジョン
前のリビジョン
reactnative:db:sqlite [2019/01/27 14:43]
ips 作成
reactnative:db:sqlite [2019/02/22 00:55] (現在)
ips
ライン 1: ライン 1:
 ====== SQLite ====== ====== SQLite ======
 +
 +expoに含まれるデータベース
 +[[https://​docs.expo.io/​versions/​latest/​sdk/​sqlite/​|SQLite]]
 +
 +[[https://​www.dbonline.jp/​sqlite/​|SQLite入門]]
 +
 +===== 基本 =====
 +
 +当たり前だが、パラメーター変数(?置換え)を使用する場合は、順番に気をつける必要がある。。。
 +
 +<​code>​
 +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"​)
 +    );
 +
 +</​code>​
 +
 +==== executeSql ====
 +
 +tx.executeSql(sqlStatement,​ arguments, successコールバック関数,​ errorコールバック関数)
 +sqlStatement:​SQL
 +arguments:​SQLに?​を使用すると、置換え変数にできる。(プリペアードステートメント)
 +success,​error:​それぞれ自身のトランザクションと、結果の2つのパラメータが返る。
 +<​code>​
 +  executeSql = async (sql, params = []) => {
 +    return new Promise((resolve,​ reject) => db.transaction(tx => {
 +      tx.executeSql( sql , params, (_, { rows }) => 
 +      {
 +        console.log("​sql done"​),​
 +        //​resolve(rows._array)
 +        resolve(rows.length)
 +      }
 +      , reject)
 +    }))
 +  }
 +  ​
 + async componentDidMount(e){
 +
 +    //​成功コールバック関数
 +    let suc=function(){
 +      console.log("​success"​)
 +    }
 +    ​
 +    //​エラーコールバック関数
 +    let fai=function(){
 +      console.log("​fail"​)
 +    }
 +
 +    console.log("​----- start -----"​)
 +    await this.executeSql('​select * from items',​[]).then((val)=>​console.log("​resulat="​ + val))
 +    .catch(()=>​console.log("​err"​))
 +    console.log("​----- end -----"​)
 +    ​
 +[00:43:58] ----- start -----
 +[00:43:58] sql done
 +[00:43:58] resulat=0
 +[00:43:58] ----- end -----    ​
 +</​code>​
 +
 +==== ROWID ====
 +
 +SQLiteにはデータをINSERTすると自動でROWIDがつく。
 +SELECTでは明記しないと取得できないので注意。
 +
 +[[https://​www.sqlite.org/​autoinc.html|SQLite Autoincrement]]
 +
 +[[https://​www.dbonline.jp/​sqlite/​table/​index8.html|ROWIDの利用]]
 +
 +<​code> ​
 +    db.transaction(
 +      tx => {
 +        tx.executeSql('​select *,ROWID from items',​ [], (_, { rows }) => {
 +            console.log(JSON.stringify(rows))
 +            console.log({count:​rows.length})
 +          }
 +        );
 +      },
 +      null,
 +    );
 +</​code>​
 +
 +==== await/​asycnするためには? ====
 +[[https://​stackoverflow.com/​questions/​47345000/​react-native-handling-async-calls-to-sqllite-db|React Native : Handling Async calls to sqllite db]]
 +
 +excuteはコールバックしているので、Promiseを返すようにする必要がある。
 +<​code>​
 +
 +    executeSql = async (sql, params = []) => {
 +    return new Promise((resolve,​ reject) => db.transaction(tx => {
 +      tx.executeSql('​select * from items',​ [], (_, { rows }) => 
 +      {
 +      console.log("​sql done"​),​
 +      resolve(rows._array)
 +      }
 +      , reject)
 +    }))
 +
 +
 +    console.log("​----- start -----"​)
 +    await this.executeSql(null,​null)
 +    console.log("​----- end -----"​)
 +
 +[08:05:41] ----- start -----
 +[08:05:41] sql done
 +[08:05:41] ----- end -----
 +</​code>​
 +
 +===== 以下だだのメモ =====
 +
 +
 +<​code>​
 +
 +    //​db.transaction(callback,​ error, success)
 +    console.log("​----- drop start -----"​)
 +    db.transaction((tx)=>​{console.log("​callback"​)},​ console.log("​error"​),​ console.log("​success"​))
 +    console.log("​----- drop end -----"​)
 +    ​
 +[06:56:31] ----- drop start -----
 +[06:56:31] error
 +[06:56:31] success
 +[06:56:31] ----- drop end -----
 +[06:56:31] callback ​  
 +    ​
 +</​code>​
 +   ​executeSql = async (sql, params = []) => {
 +    return new Promise((resolve,​ reject) => db.transaction(tx => {
 +      tx.executeSql('​select * from items',​ [], (_, { rows }) => 
 +      {
 +      console.log("​sql done"​),​
 +      resolve(rows._array)
 +      }
 +      , reject)
 +    }))
 +  }
 +  ​
 +  ​
 +<​code>​
 +
 +  async getDB(){
 +    await db.transaction((tx)=>​{return "​callback"​},​ console.log("​error"​),​ console.log("​success"​))
 +  }
 +  ​
 +  async componentDidMount(e){
 +
 +    // db.transaction(callback,​ error, success)
 +     ​console.log("​----- drop start -----"​)
 +    await console.log(this.getDB())
 +    console.log("​----- drop end -----"​)
 +  }
 +  ​
 +[07:12:05] ----- drop start -----
 +[07:12:05] error
 +[07:12:05] success
 +[07:12:05] Promise {
 +[07:​12:​05] ​  "​_40":​ 0,
 +[07:​12:​05] ​  "​_55":​ null,
 +[07:​12:​05] ​  "​_65":​ 0,
 +[07:​12:​05] ​  "​_72":​ null,
 +[07:12:05] }
 +[07:12:05] ----- drop end -----
 +</​code>​
  
reactnative/db/sqlite.1548567824.txt.gz · 最終更新: 2019/01/27 14:43 by ips