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

Expo React Native中使用Drizzle ORM无法找到SQLite数据库文件

问题分析与解决方法

你的drizzle.config.ts配置本身没有问题,找不到.sqlite文件是因为Expo对本地文件系统的存储路径做了隔离,并非Drizzle的功能问题。

1. Expo中SQLite文件的默认存储逻辑

Expo的SQLite模块会将数据库文件存放在应用的私有沙盒目录中,这个目录在设备上默认是隐藏状态,无法直接通过普通文件管理器访问。不同平台的具体路径如下:

  • iOS模拟器:Library/Developer/CoreSimulator/Devices/[设备ID]/data/Containers/Data/Application/[应用ID]/Documents/
  • Android设备:真机需ROOT权限才能访问,模拟器可通过Android Studio的Device File Explorer进入/data/data/[应用包名]/files/目录查看

2. 导出数据库到可访问位置的两种方法

方法一:通过代码复制到公共目录

使用Expo的FileSystem模块,将私有目录中的数据库文件复制到设备的公共文档目录,这样就能在系统文件管理器中找到:

import * as FileSystem from 'expo-file-system';
import { openDatabase } from 'expo-sqlite';

// Drizzle + Expo默认使用的数据库名为default.db
const db = openDatabase('default.db');

async function exportDbToPublic() {
  // 私有目录中的数据库路径
  const privateDbPath = FileSystem.documentDirectory + 'SQLite/default.db';
  // 公共文档目录路径
  const publicDir = FileSystem.documentDirectory + 'Documents/';

  // 创建公共目录(如果不存在)
  await FileSystem.makeDirectoryAsync(publicDir, { intermediates: true });
  // 复制数据库文件
  await FileSystem.copyAsync({
    from: privateDbPath,
    to: publicDir + 'my-app-db.sqlite'
  });

  console.log('数据库已导出至:', publicDir + 'my-app-db.sqlite');
}

// 调用函数执行导出
exportDbToPublic();

执行后,在iOS的「文件」应用或Android的「文档」目录中就能找到导出的my-app-db.sqlite文件。

方法二:通过Expo Go开发者菜单导出

  1. 打开Expo Go的开发者菜单(iOS摇一摇设备,Android按物理菜单键)
  2. 选择「Database」选项
  3. 找到目标数据库,点击「Download」即可将文件导出到本地电脑

3. 验证Drizzle数据库的有效性

如果想确认Drizzle确实在正常操作数据库,可以添加简单查询代码验证:

import { drizzle } from 'drizzle-orm/expo-sqlite';
import { openDatabase } from 'expo-sqlite';
import { yourTable } from './db/schema';

const expoDb = openDatabase('default.db');
const db = drizzle(expoDb);

async function testDbConnection() {
  const tableData = await db.select().from(yourTable);
  console.log('表内数据:', tableData);
}

testDbConnection();

如果控制台能打印出表内数据,说明数据库运行正常,只是文件处于私有目录无法直接查看。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 09:57:05