1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import * as SQLite from 'expo-sqlite';
- import * as FileSystem from 'expo-file-system';
- import { Asset } from 'expo-asset';
- let db1: SQLite.SQLiteDatabase | null = null;
- let db2: SQLite.SQLiteDatabase | null = null;
- async function copyDatabaseFile(dbName: string, dbAsset: Asset) {
- await dbAsset.downloadAsync();
- await FileSystem.downloadAsync(
- dbAsset.uri,
- FileSystem.documentDirectory + "SQLite/" + dbName
- );
-
- const dbUri = FileSystem.documentDirectory + `SQLite/${dbName}`;
- await FileSystem.copyAsync({
- from: dbAsset.localUri ?? '',
- to: dbUri,
- });
- return dbUri;
- }
- export async function openDatabases() {
- try {
- const sqlDir = FileSystem.documentDirectory + "SQLite";
- const fileInfo = await FileSystem.getInfoAsync(sqlDir);
- if (!fileInfo.exists) {
- await FileSystem.makeDirectoryAsync(sqlDir, { intermediates: true });
- console.log('openDatabase - Downloading databases');
- await copyDatabaseFile('nmRegions.db', Asset.fromModule(require('../../assets/db/nmRegions.db')));
- await copyDatabaseFile('darePlaces.db', Asset.fromModule(require('../../assets/db/darePlaces.db')));
- console.log('openDatabase - Databases downloaded');
- }
- const openDatabase = (dbName: string) => SQLite.openDatabase(dbName);
- db1 = openDatabase("nmRegions.db");
- db2 = openDatabase("darePlaces.db");
- } catch (error) {
- console.error('openDatabase - Error:', error);
- }
- }
- export function getFirstDatabase() {
- return db1;
- }
- export function getSecondDatabase() {
- return db2;
- }
|