0

I'm tring to use native @ionic-native/sqlite module with ionic3. If I create a SQLite db in the default location the database gets created.

  constructor( public sqlite: SQLite) {
    this.initDatabase();

  }


  initDatabase(){

    config = {
      name: 'offline.db',
      location: 'default'
    }
    this.sqlite.create(this.config).then(async  (db: SQLiteObject) => {
      await db.executeSql(`
      CREATE TABLE IF NOT EXISTS cond_pag (
          id_codizione_pag varchar(4),
          descrizione varchar(150),
          CONSTRAINT cond_pag_pk PRIMARY KEY (id_codizione_pag)  
       )`,{});

      })
      .catch(e => {
        console.log(e);
      });    

  }


}

but if I try to use the sd card insetad

config = {
      name: 'offline.db',
      location: '/sdcard'
}   

ionic throws an error

Error: Valid iOS database location could not be determined in openDatabase call
    at newSQLError (SQLitePlugin.js:26)
    at Object.<anonymous> (SQLitePlugin.js:581)
    at Object.openDatabase (SQLitePlugin.js:59)
    at index.js:199
    at new t (polyfills.js:3)
    at SQLite.create (index.js:198)
    at SQLite.value [as create] (decorators.js:49)
    at DatabaseProvider.webpackJsonp.200.DatabaseProvider.initDatabase (database.ts:28)
    at new DatabaseProvider (database.ts:20)
    at _createClass (core.js:10933) 

what's missing?

2
  • First you need permission to write files into sdcard(get permissions using ionic diagnostic plugin), next i think you should specify iosDatabaseLocation follow link Commented Jun 20, 2018 at 11:54
  • Thanks NRaghavendra . I tryed to add user permission for Android Platform but I get the same message. I found another way out. I configured ionic to compile into assets directory of a Android studio project. I created an app based on WebView component. Now I can access native function from TypeSpript and Java-Android does the dirty job. My customer wants only the Android version. Commented Jun 21, 2018 at 10:29

1 Answer 1

1

The following code works for me

initDatabase(){
        config = {
          name: '/sdcard/offline.db', // file:///sdcard/offline.db
          location: 'default'
        }
        this.sqlite.create(this.config).then(async  (db: SQLiteObject) => {
          await db.executeSql(`
          CREATE TABLE IF NOT EXISTS cond_pag (
              id_codizione_pag varchar(4),
              descrizione varchar(150),
              CONSTRAINT cond_pag_pk PRIMARY KEY (id_codizione_pag)  
           )`,{});

          })
          .catch(e => {
            console.log(e);
          });    

      }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.