You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用SQLite.openDatabaseAsync时遇NativeDatabase不是构造函数错误

解决 TypeError: _ExpoSQLiteNext.default.NativeDatabase is not a constructor 错误

核心修复步骤

1. 补全异步调用的 await

SQLite.openDatabaseAsync 是异步函数,直接赋值会得到 Promise 对象而非数据库实例,调用transaction必然触发类型错误。必须添加await获取实际的数据库对象:

修正后的完整代码:

import * as SQLite from 'expo-sqlite'

export const DbConnection = async () => {
  // 用 await 获取真实的 Database 实例
  const db = await SQLite.openDatabaseAsync('db.testDb') 

  // 推荐使用异步版 API 替代回调式写法
  await db.transactionAsync(async tx => {
    await tx.executeSqlAsync(
      'CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT, count INT)'
    )
  });

  await db.transactionAsync(async (tx) => {
    const resultSet = await tx.executeSqlAsync("SELECT * FROM items");
    console.log(resultSet.rows._array);
  }).catch(error => console.log(error));
}

2. 清理混淆的依赖包

错误信息中出现_ExpoSQLiteNext,说明可能误装了第三方的expo-sqlite-next包,而非官方expo-sqlite。执行以下命令替换依赖:

# npm 环境
npm uninstall expo-sqlite-next
npm install expo-sqlite

# yarn 环境
yarn remove expo-sqlite-next
yarn add expo-sqlite

3. 清除开发缓存

如果以上操作无效,清除Expo开发缓存并重启服务器:

npx expo start -c

内容的提问来源于stack exchange,提问作者Parag Pathari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 14:12:05