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