· 7 years ago · Dec 16, 2018, 12:00 AM
1import { SQLite } from 'expo'
2
3async function main() {
4 const db = SQLite.openDatabase('mydb')
5 console.debug('nn')
6 await exec_sql({ db, sql: `DROP TABLE IF EXISTS table_name;` })
7 await exec_sql({ db, sql: `CREATE TABLE IF NOT EXISTS table_name (id INTEGER PRIMARY KEY, created_at INTEGER NOT NULL);` })
8 await exec_sql({ db, sql: `INSERT INTO table_name (created_at) VALUES (?);`, parameters: [Math.pow(2, (8 * 4) - 1) - 1] })
9 await exec_sql({ db, sql: `INSERT INTO table_name (created_at) VALUES (?);`, parameters: [Math.pow(2, (8 * 4) - 1)] })
10
11 await exec_sql({ db, sql: `INSERT INTO table_name (created_at) VALUES (9223372036854775000);`, parameters: [] })
12 // set to 9223372036854775000 as javascript Number.MAX_SAFE_INTEGER means `Math.pow(2, (8 * 8) - 1) - 1`
13 // is rounded higher to 9223372036854776000
14 await exec_sql({ db, sql: `INSERT INTO table_name (created_at) VALUES (?);`, parameters: [9223372036854775000] })
15
16// This will error with
17 // Error in callNativeModules()
18 // Over flow during conversion: 9223372036854776000 (rounding up due to javascript's Number.MAX_SAFE_INTEGER)
19 // await exec_sql({ db, sql: `INSERT INTO table_name (created_at) VALUES (?);`, parameters: [Math.pow(2, (8 * 8) - 1)] })
20 await exec_sql({ db, sql: `SELECT * FROM table_name;` })
21}
22
23main()
24
25function exec_sql ({ db, sql, parameters = [] }) {
26 return new Promise((resolve, reject) => {
27 db.transaction(tx => {
28 console.debug(`Executing ${sql} with parameters: ${parameters}`)
29 tx.executeSql(sql, parameters,
30 (_, result) => {
31 console.debug(`Have result: ${JSON.stringify(result)}`)
32 if (result && result.rows && result.rows._array) {
33 resolve({ items: result.rows._array })
34 } else {
35 resolve()
36 }
37 },
38 (_, err) => {
39 console.error(`Error during executing sql: `, err)
40 reject(err)
41 }
42 )
43 })
44 })
45}
46
47Executing CREATE TABLE IF NOT EXISTS table_name (id INTEGER PRIMARY KEY, created_at UNSIGNED INTEGER NOT NULL); with parameters:
48Executing INSERT INTO table_name (created_at) VALUES (?); with parameters: 2147483647
49Executing INSERT INTO table_name (created_at) VALUES (?); with parameters: 2147483648
50Executing INSERT INTO table_name (created_at) VALUES (9223372036854775000); with parameters:
51Executing INSERT INTO table_name (created_at) VALUES (?); with parameters: 9223372036854775000
52Executing SELECT * FROM table_name; with parameters:
53Have result: [{"id": 1, "created_at": 2147483647}, {"id": 2, "created_at": -2147483648}, {"id": 3, "created_at": -808},{"id": 4, "created_at": -1024}]