以前のリビジョンの文書です
当たり前だが、パラメーター変数(?置換え)を使用する場合は、順番に気をつける必要がある。。。
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つのパラメータが返る。
tx.executeSql('select * from items where name=?', ["safa"], (tran, { rows } ) => console.log(JSON.stringify(rows)) );
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, );
//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
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 -----