Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where is the iOS sqlite .db file located? #242

Open
garbit opened this issue Sep 11, 2021 · 1 comment
Open

Where is the iOS sqlite .db file located? #242

garbit opened this issue Sep 11, 2021 · 1 comment

Comments

@garbit
Copy link

garbit commented Sep 11, 2021

Hi there,

I'm trying to use sqlite on iOS to persist data on the iOS device however I can't seem to locate the .db file.

Where can I find the .db sqlite database file on iOS?

I'm using the following code that seems to be adding/retrieving the relevant data and Safari debugger doesn't show the database in the "Storage" section whilst running an Ionic/Vue app on an iPad.

Installation steps:

npm install @ionic/storage
npm install cordova-sqlite-storage
npm install localforage-cordovasqlitedriver

Code to interact with DB (which is successful, from what I can tell but I can't find the file)

import { Drivers, Storage } from '@ionic/storage';
import * as CordovaSQLiteDriver from 'localforage-cordovasqlitedriver';

const sqliteStorage = new Storage({
      name: '__mydb',
      driverOrder: [CordovaSQLiteDriver._driver, Drivers.IndexedDB, Drivers.LocalStorage]
});
    
await sqliteStorage.defineDriver(CordovaSQLiteDriver)
await sqliteStorage.create();
await sqliteStorage.set(media.name, JSON.stringify(media)) 
@garbit
Copy link
Author

garbit commented Sep 12, 2021

Looking more closely at this problem. Ionic Storage doesn't support location as a property of the StorageConfig. If it was possible to add this and you were using localForage-cordovaSQLiteDriver you could then pass in a location id as found in the underlaying cordova-sqlite-storage package (see below):

To work out what options to add as a location I looked in the cordova-sqlite-storage repo to understand how this errors and also the location options you can pass for iOS (permalink):

In cordova-sqlite-storage there's two lists of iOS specific keys that can be passed to store the db:

dblocations = ["docs", "libs", "nosync"];

  iosLocationMap = {
    'default': 'nosync',
    'Documents': 'docs',
    'Library': 'libs'
  };

In ionic/storage I've modified the StorageConfig object to allow for the location ID to be passed to cordova-sqlite-storage:

export interface StorageConfig {
    name?: string;
    version?: number;
    size?: number;
    storeName?: string;
    description?: string;
    driverOrder?: Driver[];
    dbKey?: string;
    location?: number; <- I've added this
}

I then can use the following in my Ionic app (passing in the location index that denotes Documents on iOS):

const ionicStorage = new Storage({
    name: '__mydb.db',
    driverOrder: [CordovaSQLiteDriver._driver, Drivers.IndexedDB, Drivers.LocalStorage],
    location: 0 <- this is passed in by editing the StorageConfig in ionic/storage (see above) - the 0 refers to the index position of capacitor-sqlite-storage package [here](https://github.com/storesafe/cordova-sqlite-storage/blob/c256701caf8affbe0779e8ee95abaa746ecc051f/www/SQLitePlugin.js#L543)
});

location should really be available to be passed to specify the location output of the database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant