capacitor-community / sqlite

⚡Capacitor plugin for native & electron SQLite databases.
MIT License
495 stars 118 forks source link

No available connection for database test-db #85

Closed logisnap closed 3 years ago

logisnap commented 3 years ago

First of all thanks for writing a really cool SQlite plugin. It looks like it simplifies all the issues i have had with mobil SQLite in the past.

After successfully importing json schema and trying to open connection to the database i am getting: "No available connection for database test-db"

I have no data in the tables as some special logic are running to preseed the DB on the device.

If i run get.DatabaseList() i am getting: "test-dbSQLite.db"

I have spent 2 days trawling the net for a possible solution. I am using Simon Grimm's article as inspiration. https://devdactic.com/sqlite-ionic-app-with-capacitor/

I have tested on multiple android devices. And are getting the same result.

I hope you guys can help me in the right direction.

jepiqueau commented 3 years ago

@logisnap No problem to help you and thanks to looking at the plugin. First of All which release of the plugin are you using ?, which Framework (Ionic/Angular, Ionic/React or Ionic/Vue) ? On which device are you testing and get the error? Did you look at the different applications starter which are referenced in the documentation. Can you share the code where you import the json Object till you get the error

jepiqueau commented 3 years ago

@logisnap The article that you mentioned was using the old version of the plugin

logisnap commented 3 years ago

HI @jepiqueau

Thank you for getting back to me.

I am using version "@capacitor-community/sqlite": "^2.9.12", on ionic/angular.

My test devices are Oneplus and levono tab M7. Both behaves the same way. I am running with external server through android studio and chrome device debugging.

Here are my init db ` async init(): Promise { const info = await Device.getInfo();

if (info.platform === 'android') {
  try {
    const sqlite = CapacitorSQLite as any;
    let result = await sqlite.requestPermissions();
    console.log("################# Premissions...", result);
    this.setupDatabase();
  } catch (e) {
    const alert = await this.alertCtrl.create({
      header: 'No DB access',
      message: 'This app can\'t work without Database access.',
      buttons: ['OK']
    });
    await alert.present();
  }
} else {
  this.setupDatabase();
}

}

private async setupDatabase(downloadNew:number = 0) { //this.deleteDatabase();

const dbSetupDone = await Storage.get({ key: DB_SETUP_KEY });
if (!dbSetupDone.value) {
    console.log("*************************** DOWNLOADING database ***************************");
    this.downloadDatabase();
} else {
    console.log("*************************** INITIATING database ***************************");
    this.dbName = (await Storage.get({ key: DB_NAME_KEY })).value;
    await CapacitorSQLite.open({ database: this.dbName });
    this.dbReady.next(true);
}

}

`

downloadDatabase code works and accepts the json and seeds the database.. As far as i can see.

I also tried to implement this code: https://github.com/jepiqueau/angular-sqlite-app-starter/blob/master/src/app/services/sqlite.service.ts But running on devices i am getting "not implemented in Web Platform"

logisnap commented 3 years ago

Here are my downloadDatabase code: ` private downloadDatabase(update = false) { try { this.http.get(environment.APIlogiURL+'database/getSchema').subscribe(async (jsonExport: JsonSQLite) => { console.log("Downloaded json:", jsonExport); const jsonstring = JSON.stringify(jsonExport); const isValid = await CapacitorSQLite.isJsonValid({ jsonstring });

          console.log("Json valid result", isValid);
          if (isValid.result) {
            console.log("********************** JSON OK ***************************");
            this.dbName = jsonExport.database;
            await Storage.set({ key: DB_NAME_KEY, value: this.dbName });
            await CapacitorSQLite.importFromJson({ jsonstring });
            await Storage.set({ key: DB_SETUP_KEY, value: '1' });

            // //Your potential logic to detect offline changes later
            // if (!update) {
            //   await CapacitorSQLite.createSyncTable();
            // } else {
            //   await CapacitorSQLite.setSyncDate({ syncdate: '' + new Date().getTime() })
            // }
            this.dbReady.next(true);
          } else {
            console.log("****************************************************************");
            console.log("*********************** JSON INVALID ***************************");
            console.log("****************************************************************");
          }
        });
      } catch (err) {
        console.log("**** downloadDatabase FAILED!!!!" + err.name + ': "' + err.message);
      }    

    }

`

logisnap commented 3 years ago

The Starter app works on both platforms.

jepiqueau commented 3 years ago

@logisnap You are mixing the code between the 2.4.x and the 2.9.x releases. In 2.9.12 the request for permissions has been removed. so look carefully at https://github.com/jepiqueau/angular-sqlite-app-starter/tree/refactorto understand how the service works. again take care of the version as it seems that you are using the service from the master which is the 3.0.0-beta version. take the one of the 2.9.x. the fact that get But running on devices i am getting "not implemented in Web Platform" is coming as you do not have defined the CapacitorSQLite class in the MainActivity.java file. Here again look at the doc it is clearly explained https://github.com/capacitor-community/sqlite/blob/2.9.x/README.md After this it should normally works.

logisnap commented 3 years ago

THANK YOU!

for pointing me in the right direction. I have it running on 2.9

I must say that this is a really cool integration. Thumbs up.

I am closing the ticket.

jepiqueau commented 3 years ago

@logisnap I am glad that you solved it. Good luck and happy development