当たり前だが、パラメーター変数(?置換え)を使用する場合は、順番に気をつける必要がある。。。
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") );
tx.executeSql(sqlStatement, arguments, successコールバック関数, errorコールバック関数)
sqlStatement:SQL
arguments:SQLに?を使用すると、置換え変数にできる。(プリペアードステートメント)
success,error:それぞれ自身のトランザクションと、結果の2つのパラメータが返る。
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 -----
SQLiteにはデータをINSERTすると自動でROWIDがつく。
SELECTでは明記しないと取得できないので注意。
db.transaction( tx => { tx.executeSql('select *,ROWID from items', [], (_, { rows }) => { console.log(JSON.stringify(rows)) console.log({count:rows.length}) } ); }, null, );
React Native : Handling Async calls to sqllite db
excuteはコールバックしているので、Promiseを返すようにする必要がある。
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 -----
//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
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) })) }
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 -----