React Native导入react-native-sqlite-storage库为空对象问题求助
问题描述
按照react-native-sqlite-storage的iOS预填充数据库配置说明完成Xcode配置后,在JS文件中导入库时得到空对象,无法调用openDatabase方法:
import SQLite from 'react-native-sqlite-storage'; console.log(SQLite) // {}
使用的版本信息:
"react-native-sqlite-storage": "^6.0.1", "react": "18.2.0", "react-native": "0.71.7"
解决方法
- 修正导入方式
该库新版本的导出方式可能已变更,尝试使用命名导入直接获取openDatabase:
import { openDatabase } from 'react-native-sqlite-storage'; // 直接使用openDatabase方法 const db = openDatabase({ name: 'your-db-name.db' });
若仍需导入整个模块,可尝试:
import * as SQLite from 'react-native-sqlite-storage';
- 重新安装并清理缓存
- 卸载并重新安装依赖:
npm uninstall react-native-sqlite-storage npm install react-native-sqlite-storage --save - 清理React Native缓存:
npx react-native start --reset-cache - 重新安装iOS Pods并构建:
cd ios pod install cd .. npx react-native run-ios - 在Xcode中执行
Clean Build Folder(快捷键Cmd+Shift+K)后重新编译运行。
- 检查版本兼容性
react-native-sqlite-storage 6.x版本可能与React Native 0.71.x存在兼容性问题,尝试降级到5.x版本:
npm install react-native-sqlite-storage@5.0.0 --save
之后重复上述清理缓存和重新构建的步骤。
- 确认iOS配置细节
虽然你认为Xcode配置无误,但仍需核对:
- 预填充的数据库文件已添加到Xcode项目中,且勾选了
Copy items if needed。 - 数据库文件的
Target Membership已勾选当前项目目标。
内容的提问来源于stack exchange,提问作者Expert wanna be
相关产品推荐
相关产品推荐

