当尝试插入一个等于4字节-1位的有符号整数(即2^31,即2147483648(时,我得到一个整数溢出条件(即它被转换为-2147483648(。
sqlite文档指出,INTEGER类型最多支持8字节的带符号整数:https://www.sqlite.org/datatype3.html另外,超过8字节-1位-1的值会导致异常。因此,这导致了它是一个expo/react原生bug的想法。这是Expo/react native中已知的bug吗?我找不到ExponentSQLite
是从哪里来的。还要注意,2147483648
远低于javascript MAX_SAFE_INTEGER,因此可能在android代码中,参数被转换为4字节带符号整数。
import { SQLite } from 'expo'
async function main() {
const db = SQLite.openDatabase('mydb')
await exec_sql({ db, sql: `DROP TABLE IF EXISTS table_name;` })
await exec_sql({ db, sql: `CREATE TABLE table_name (id INTEGER PRIMARY KEY, created_at INTEGER NOT NULL);` })
await exec_sql({ db, sql: `INSERT INTO table_name (created_at) VALUES (?);`, parameters: [Math.pow(2, (8 * 4) - 1) - 1] })
await exec_sql({ db, sql: `INSERT INTO table_name (created_at) VALUES (?);`, parameters: [Math.pow(2, (8 * 4) - 1)] })
await exec_sql({ db, sql: `INSERT INTO table_name (created_at) VALUES (9223372036854775000);`, parameters: [] })
// set to 9223372036854775000 as javascript Number.MAX_SAFE_INTEGER means `Math.pow(2, (8 * 8) - 1) - 1`
// is rounded higher to 9223372036854776000
await exec_sql({ db, sql: `INSERT INTO table_name (created_at) VALUES (?);`, parameters: [9223372036854775000] })
// This will error with
// Error in callNativeModules()
// Over flow during conversion: 9223372036854776000 (rounding up due to javascript's Number.MAX_SAFE_INTEGER)
// await exec_sql({ db, sql: `INSERT INTO table_name (created_at) VALUES (?);`, parameters: [Math.pow(2, (8 * 8) - 1)] })
await exec_sql({ db, sql: `SELECT * FROM table_name;` })
}
main()
function exec_sql ({ db, sql, parameters = [] }) {
return new Promise((resolve, reject) => {
db.transaction(tx => {
console.debug(`Executing ${sql} with parameters: ${parameters}`)
tx.executeSql(sql, parameters,
(_, result) => {
console.debug(`Have result: ${JSON.stringify(result)}`)
if (result && result.rows && result.rows._array) {
resolve({ items: result.rows._array })
} else {
resolve()
}
},
(_, err) => {
console.error(`Error during executing sql: `, err)
reject(err)
}
)
})
})
}
这将导致(为简洁起见进行编辑(:
Executing CREATE TABLE IF NOT EXISTS table_name (id INTEGER PRIMARY KEY, created_at INTEGER NOT NULL); with parameters:
Executing INSERT INTO table_name (created_at) VALUES (?); with parameters: 2147483647
Executing INSERT INTO table_name (created_at) VALUES (?); with parameters: 2147483648
Executing INSERT INTO table_name (created_at) VALUES (9223372036854775000); with parameters:
Executing INSERT INTO table_name (created_at) VALUES (?); with parameters: 9223372036854775000
Executing SELECT * FROM table_name; with parameters:
Have result: [{"id": 1, "created_at": 2147483647}, {"id": 2, "created_at": -2147483648}, {"id": 3, "created_at": -808},{"id": 4, "created_at": -1024}]
PRAGMA用户版本->0PRAGMA schema_version->46expo版本->31.0.2
*编辑1*
我已经提交了一个错误,让我们看看响应是什么:https://github.com/expo/expo/issues/3000
这是一个错误。已修复为:https://github.com/expo/expo/pull/3005