使用Expo SQLite API加载预填充数据库报错:找不到预填充文件中的表
Hey, I’ve run into this exact issue before with Expo’s SQLite API—super frustrating when you’ve got the DB file in place but still get that table error. Let’s walk through the most likely fixes step by step:
Troubleshooting "no such table in pre-populated file" Error in Expo SQLite
1. Double-check your database path and openDatabase configuration
- First, make sure the
nameparameter inopenDatabasematches your actual database filename exactly, including capitalization (Android is case-sensitive for filenames, while iOS isn’t—this is a common gotcha). - If your DB is stored in the
assetsfolder, confirm it’s added toassetBundlePatternsinapp.jsonso Expo bundles it with your app:"assetBundlePatterns": [ "**/*", "assets/databases/your-db-file.db" ] - Use the correct
createFromLocationparameter for your Expo SDK version:- For SDK 48+: Use
createFromAssetwith a require statement pointing to your DB file - For older SDKs: Use
createFromLocation: 1(which tells Expo to copy from the assets folder)
Example code:
const db = SQLite.openDatabase({ name: 'your-db.db', location: 'default', // SDK 48+ syntax createFromAsset: require('./assets/your-db.db') // Older SDK syntax // createFromLocation: 1 }); - For SDK 48+: Use
2. Verify your pre-populated database file is valid
- Open your local DB file with a tool like DB Browser for SQLite to confirm:
- The table you’re querying actually exists
- The table name matches exactly what you’re using in your SQL statements (watch for typos, underscores, or capitalization differences)
- Check if the DB file is corrupted—sometimes copying or exporting the file can introduce errors. Try re-exporting a fresh version from your SQLite tool.
3. Clear Expo’s cache and rebuild the project
Expo often caches old assets, especially if you’ve updated your DB file. Try these steps:
- Close the Expo Go client completely and reopen it
- Run
expo r -cin your terminal to clear the cache and restart the dev server - For simulators: Reset iOS simulator content or clear Android app data to wipe any cached DB copies
4. Ensure the database finishes copying before running queries
If you’re executing SQL statements immediately on app launch, the pre-populated DB might not have finished copying yet. Add a safety check:
- Wrap your queries in a
try/catchblock to handle initialization delays - Add a small delay (or use a state flag) to wait for the database to open before running operations
5. Check platform-specific quirks
- Android: Make sure your DB file doesn’t have read-only permissions. Also, stick with
location: 'default'to let Expo handle the correct storage path automatically. - iOS: If distributing via TestFlight/Ad Hoc, verify the DB file is included in the IPA bundle. You can check this in Xcode by inspecting the app’s bundled resources.
内容的提问来源于stack exchange,提问作者Eric Phillips
相关产品推荐
相关产品推荐

