I would like to implement a SQLite database in my Expo application. While following the official documentation, I encountered this error:
→ Caused by: Error code: no such table: <tablename>]
ERROR [Error: Call to function 'NativeDatabase.prepareAsync' has been rejected.
So I modified my code, which solved the problem at first:
import { Suspense, useEffect } from "react";
import { Text } from "react-native";
import { Stack } from "expo-router";
import { Asset } from "expo-asset";
import { SQLiteProvider } from "expo-sqlite";
import * as FileSystem from "expo-file-system";
export default function RootLayout() {
const configDatabase = async () => {
const dbName = "database.db";
const dbAsset = require("../assets/data/database.db");
const dbUri = Asset.fromModule(dbAsset).uri;
const dbFilePath = `${FileSystem.documentDirectory}SQLite/${dbName}`;
const fileInfo = await FileSystem.getInfoAsync(dbFilePath);
if (!fileInfo.exists) {
await FileSystem.makeDirectoryAsync(
`${FileSystem.documentDirectory}SQLite/`,
// eslint-disable-next-line prettier/prettier
{ intermediates: true }
);
await FileSystem.downloadAsync(dbUri, dbFilePath);
}
};
useEffect(() => {
const fetchDatabase = async () => {
try {
await configDatabase();
} catch (err) {
console.error(err);
}
};
fetchDatabase();
}, []);
return (
<Suspense fallback={<Text>Loading...</Text>}>
<SQLiteProvider databaseName="database.db" useSuspense={true}>
<Stack>
<Stack.Screen
name="index"
options={{
headerShown: false,
}}
/>
</Stack>
</SQLiteProvider>
</Suspense>
);
}
After a while, the error occurred again. I then tried my basic code, but the error remained unchanged:
export default function RootLayout() {
return (
<Suspense fallback={<Text>Loading...</Text>}>
<SQLiteProvider
databaseName="database.db"
useSuspense={true}
assetSource={{ assetId: require("../assets/data/database.db") }}
>
<Stack>
<Stack.Screen
name="index"
options={{
headerShown: false,
}}
/>
</Stack>
</SQLiteProvider>
</Suspense>
);
}
In the meantime, I only updated my emulator on Android Studio, nothing else. After that, I updated my packages, but the error is still there, even on my android phone. If anyone can give me a solution, I would be very grateful.
Thanks in advance
package.json:
"dependencies": {
"@expo-google-fonts/changa": "^0.2.3",
"@expo-google-fonts/kanit": "^0.2.3",
"@expo-google-fonts/maven-pro": "^0.2.3",
"@expo/vector-icons": "^14.0.2",
"expo": "~51.0.17",
"expo-asset": "~10.0.10",
"expo-constants": "~16.0.2",
"expo-file-system": "~17.0.1",
"expo-font": "~12.0.7",
"expo-linking": "~6.3.1",
"expo-router": "~3.5.17",
"expo-sqlite": "~14.0.4",
"expo-status-bar": "~1.12.1",
"react": "18.2.0",
"react-native": "0.74.2",
"react-native-safe-area-context": "4.10.1",
"react-native-screens": "3.31.1"
},```