-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
49 lines (43 loc) · 1.38 KB
/
example.js
File metadata and controls
49 lines (43 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import * as SQLite from 'expo-sqlite';
import PromisedSQLite from 'react-native-promised-sqlite';
// Setup
const db = SQLite.openDatabase('test.db');
const promised_db = PromisedSQLite(db);
// Usage
promised_db.sql(
'CREATE TABLE AWESOME_PACKAGES(' +
'ID INT PRIMARY KEY NOT NULL, ' +
'NAME TEXT NOT NULL, ' +
'URL TEXT NOT NULL);')
.then(([transaction, result]) => {
console.log('Table created!');
})
.catch((error) => {
console.error(error);
});
// With async/await
const with_async = async () => {
try {
const [statement, data] = await promised_db.sql(
'INSERT INTO AWESOME_PACKAGES (NAME, URL) VALUES (?, ?)',
['react-native-sqlite-promised', 'https://some-url.com']
);
console.log(`Success! Rows affected: ${data.rowsAffected}`);
} catch (error) {
console.error(error);
}
};
with_async().then(() => console.log('Async function returned'));
// Multiple statements in a transaction
promised_db.sqls([
['SELECT * FROM AWESOME_PACKAGES'],
['INSERT INTO AWESOME_PACKAGES (NAME, URL) VALUES (?, ?)', 'expo-sdk', 'https://expo.io']
])
.then(([data1, data2]) => {
console.log(`Rows returned from 1st statement: ${data1.rows.length}`);
console.log(`Rows affected by the second statement: ${data2.rowsAffected}`)
})
.catch(([error1, error2]) => {
if (error1) console.error(error1);
if (error2) console.error(error2);
});