capacitor-community / sqlite

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

Error: isTableExists: No available connection for database: with Capacitor 4 and SQLite ver: 4.2.2 #344

Closed Sampath-Lokuge closed 1 year ago

Sampath-Lokuge commented 2 years ago

Hi,

sqlite-configuration.service.ts

async configureSqliteDatabase(): Promise<void> {
    const sqliteConnection = new SQLiteConnection(CapacitorSQLite);

    const checkConnectionsConsistency = await sqliteConnection.checkConnectionsConsistency();

    const isConnection = (await sqliteConnection.isConnection('TaxiMate'))?.result;

    if (checkConnectionsConsistency?.result && isConnection) {
      this.sqliteDbConnection = await sqliteConnection.retrieveConnection('TaxiMate');
    } else {
      this.sqliteDbConnection = await sqliteConnection.createConnection(
        'TaxiMate',
        false,
        'no-encryption',
        1
      );
    }

    await this.sqliteDbConnection.open();

    this.setSqliteDbConnection(this.sqliteDbConnection);
  }

Error:

 Uncaught (in promise): Error: isTableExists: No available connection for database TaxiMate
Error: isTableExists: No available connection for database TaxiMate
    at returnResult (VM3:744:32)
    at win.androidBridge.onmessage (VM3:719:21)
    at resolvePromise (zone.js:1255:1)
    at zone.js:1162:1
    at asyncGeneratorStep (asyncToGenerator.js:6:1)
    at _throw (asyncToGenerator.js:29:1)
    at ZoneDelegate.invoke (zone.js:400:1)
    at Object.onInvoke (core.mjs:26231:33)
    at ZoneDelegate.invoke (zone.js:399:1)
    at Zone.run (zone.js:160:1)
    at zone.js:1318:1
    at ZoneDelegate.invokeTask (zone.js:434:1)

package.json

{
  "name": "TaxiMate",
  "version": "1.5.0",
  "author": "TaxiMate",
  "homepage": "",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "lint": "ng lint"
  },
  "private": true,
  "dependencies": {
    "@angular/cdk": "14.2.6",
    "@angular/common": "14.2.9",
    "@angular/core": "14.2.9",
    "@angular/forms": "14.2.9",
    "@angular/platform-browser": "14.2.9",
    "@angular/platform-browser-dynamic": "14.2.9",
    "@angular/router": "14.2.9",
    "@capacitor-community/sqlite": "4.0.1",
    "@capacitor-firebase/authentication": "1.2.0",
    "@capacitor/android": "^4.3.0",
    "@capacitor/app": "^4.0.0",
    "@capacitor/camera": "^4.0.0",
    "@capacitor/core": "^4.3.0",
    "@capacitor/haptics": "^4.0.0",
    "@capacitor/ios": "^4.3.0",
    "@capacitor/keyboard": "^4.0.0",
    "@capacitor/network": "^4.0.0",
    "@capacitor/preferences": "^4.0.1",
    "@capacitor/splash-screen": "^4.0.0",
    "@capacitor/status-bar": "^4.0.0",
    "@ionic/angular": "6.3.4",
    "@sentry/angular": "7.13.0",
    "@sentry/capacitor": "0.10.1",
    "@types/lodash": "4.14.178",
    "dayjs": "1.10.7",
    "firebase": "9.6.10",
    "guid-typescript": "1.0.9",
    "lodash": "4.17.21",
    "rxjs": "6.6.0",
    "tslib": "2.2.0",
    "zone.js": "0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.2.8",
    "@angular-eslint/builder": "14.1.2",
    "@angular-eslint/eslint-plugin": "14.1.2",
    "@angular-eslint/eslint-plugin-template": "14.1.2",
    "@angular-eslint/template-parser": "14.1.2",
    "@angular/cli": "14.2.8",
    "@angular/compiler": "14.2.9",
    "@angular/compiler-cli": "14.2.9",
    "@angular/language-service": "14.2.9",
    "@capacitor/cli": "^4.0.0",
    "@ionic/angular-toolkit": "7.0.0",
    "@sentry/cli": "^2.8.1",
    "@types/node": "12.11.1",
    "@typescript-eslint/eslint-plugin": "5.42.0",
    "@typescript-eslint/parser": "5.42.0",
    "eslint": "7.6.0",
    "eslint-plugin-import": "2.22.1",
    "eslint-plugin-jsdoc": "30.7.6",
    "eslint-plugin-prefer-arrow": "1.2.2",
    "prettier": "2.5.1",
    "ts-node": "8.3.0",
    "typescript": "4.8.4"
  },
  "description": "TaxiMate"
}

The above code works fine with 3+ versions. But now it gives the above error with 4+ versions. Please let me know the issue.

I have tried with 4.2.2 too. Same error. Ionic/Angular native app.

Is this "@capacitor/core": "^4.3.0", issue? i.e. I use that version.

I didn't add any new code with the 4+ version. Do I need to do that?

Thanks!

jepiqueau commented 2 years ago

@Sampath-Lokuge try with the latest version of @capacitor-community/sqlite. I am away from any computer till end of the month. Look at the code of the angular starter app there is some call to isTableExists and they work. Have you convert your app from capacitor 3 to 4 according to the ionic recommandations

Sampath-Lokuge commented 2 years ago

Still the same error with 4.2.0. Do I need to change my code? i.e. cannot use the below code with this new version? I have used npx cap migrate for 3 to 4 and no errors there. Your thoughts?

Same above error.

package.json

{
  "name": "TaxiMate",
  "version": "1.5.0",
  "author": "TaxiMate",
  "homepage": "",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "lint": "ng lint"
  },
  "private": true,
  "dependencies": {
    "@angular/cdk": "14.0.0",
    "@angular/common": "14.0.0",
    "@angular/core": "14.0.0",
    "@angular/forms": "14.0.0",
    "@angular/platform-browser": "14.0.0",
    "@angular/platform-browser-dynamic": "14.0.0",
    "@angular/router": "14.0.0",
    "@capacitor-community/sqlite": "4.2.0",
    "@capacitor-firebase/authentication": "1.2.0",
    "@capacitor/android": "^4.2.0",
    "@capacitor/app": "^4.0.0",
    "@capacitor/camera": "^4.0.0",
    "@capacitor/core": "^4.2.0",
    "@capacitor/haptics": "^4.0.0",
    "@capacitor/ios": "^4.2.0",
    "@capacitor/keyboard": "^4.0.0",
    "@capacitor/network": "^4.0.0",
    "@capacitor/preferences": "^4.0.1",
    "@capacitor/splash-screen": "^4.0.0",
    "@capacitor/status-bar": "^4.0.0",
    "@ionic/angular": "6.3.4",
    "@sentry/angular": "7.13.0",
    "@sentry/capacitor": "0.10.1",
    "@types/lodash": "4.14.178",
    "dayjs": "1.10.7",
    "firebase": "9.6.10",
    "guid-typescript": "1.0.9",
    "lodash": "4.17.21",
    "rxjs": "6.6.0",
    "tslib": "2.2.0",
    "zone.js": "0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.0.0",
    "@angular-eslint/builder": "14.1.2",
    "@angular-eslint/eslint-plugin": "14.1.2",
    "@angular-eslint/eslint-plugin-template": "14.1.2",
    "@angular-eslint/template-parser": "14.1.2",
    "@angular/cli": "14.0.0",
    "@angular/compiler": "14.0.0",
    "@angular/compiler-cli": "14.0.0",
    "@angular/language-service": "14.0.0",
    "@capacitor/cli": "^4.2.0",
    "@ionic/angular-toolkit": "6.0.0",
    "@sentry/cli": "^2.8.1",
    "@types/node": "12.11.1",
    "@typescript-eslint/eslint-plugin": "5.42.0",
    "@typescript-eslint/parser": "5.42.0",
    "eslint": "7.6.0",
    "eslint-plugin-import": "2.22.1",
    "eslint-plugin-jsdoc": "30.7.6",
    "eslint-plugin-prefer-arrow": "1.2.2",
    "prettier": "2.5.1",
    "ts-node": "8.3.0",
    "typescript": "4.7.3"
  },
  "description": "TaxiMate"
}

code

async configureSqliteDatabase(): Promise<void> {
    const sqliteConnection = new SQLiteConnection(CapacitorSQLite);

    const checkConnectionsConsistency = await sqliteConnection.checkConnectionsConsistency();

    const isConnection = (await sqliteConnection.isConnection('TaxiMate', false))?.result;

    if (checkConnectionsConsistency?.result && isConnection) {
      this.sqliteDbConnection = await sqliteConnection.retrieveConnection('TaxiMate', false);
    } else {
      this.sqliteDbConnection = await sqliteConnection.createConnection(
        'TaxiMate',
        false,
        'no-encryption',
        1,
        false
      );
    }

    await this.sqliteDbConnection.open();

    this.setSqliteDbConnection(this.sqliteDbConnection);
  }
jepiqueau commented 2 years ago

@Sampath-Lokuge i do not see in your code where you call isTableExists. On which platform the error is returned? All of them ?

Sampath-Lokuge commented 2 years ago

I have used this const isTable = await this.sqliteDbConnection.isTable('taxiExtras');

Do I need to change that to const isTable = await this.sqliteDbConnection.isTableExists('taxiExtras');

Can I have doc for such changes?

This is a native Android app.

Sampath-Lokuge commented 2 years ago

P.S.

I have not used isTableExists with my code. only isTable and all worked nicely with this version "@capacitor-community/sqlite": "3.5.1-2",

Sampath-Lokuge commented 2 years ago

P.S. 2

Sorry, we cannot use this const isTable = await this.sqliteDbConnection.isTableExists('taxiExtras'); there too.

Any thoughts, please? Why is that error with 4.2.0?

jepiqueau commented 2 years ago

@Sampath-Lokuge it is isTable to be used with this.sqliteDbConnection

Sampath-Lokuge commented 2 years ago

Yes private sqliteDbConnection: SQLiteDBConnection;

Sampath-Lokuge commented 2 years ago

P.S.

I use this CLI: ionic cap run android -l --host=0.0.0.0

Sampath-Lokuge commented 2 years ago

P.S.2

It shows like so: Is this correct? I think the problem is there no?

1

Actuely the same error with "@capacitor-community/sqlite": "3.5.1-2",

Any reason why this happened suddenly? I have not touched this app for a few months now and no changes too.

Sampath-Lokuge commented 2 years ago

Any feedback here, please?

Sampath-Lokuge commented 2 years ago

That error shows when it executes this line:

const isTable = await this.sqliteDbConnection.isTable('taxiExtras');

Sampath-Lokuge commented 1 year ago

Please see this video too: https://youtu.be/8oWWLAYzxkE

Any clue, please?

jepiqueau commented 1 year ago

@Sampath-Lokuge are you using @capacitor-community/http if it is the case change it with the http plugin @capacitor/http. This may solve the issue. At least it did for one developer otherwise you will have to share your code when i am back and i will have a look

Sampath-Lokuge commented 1 year ago

No. I use this import { HttpClientModule } from '@angular/common/http';

package.json

{
  "name": "taximate-mobile",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/cdk": "^14.0.0",
    "@angular/common": "^14.0.0",
    "@angular/core": "^14.0.0",
    "@angular/forms": "^14.0.0",
    "@angular/platform-browser": "^14.0.0",
    "@angular/platform-browser-dynamic": "^14.0.0",
    "@angular/router": "^14.0.0",
    "@capacitor-community/sqlite": "^4.2.2",
    "@capacitor/android": "^4.4.0",
    "@capacitor/app": "4.1.0",
    "@capacitor/camera": "^4.1.3",
    "@capacitor/core": "4.4.0",
    "@capacitor/haptics": "4.0.1",
    "@capacitor/keyboard": "4.0.1",
    "@capacitor/network": "^4.0.1",
    "@capacitor/preferences": "^4.0.1",
    "@capacitor/splash-screen": "^4.1.1",
    "@capacitor/status-bar": "4.0.1",
    "@ionic/angular": "^6.1.9",
    "@sentry/angular": "^7.13.0",
    "@sentry/capacitor": "^0.10.1",
    "dayjs": "^1.11.6",
    "guid-typescript": "^1.0.9",
    "ionicons": "^6.0.3",
    "rxjs": "~6.6.0",
    "tslib": "^2.2.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.0.0",
    "@angular-eslint/builder": "~13.0.1",
    "@angular-eslint/eslint-plugin": "~13.0.1",
    "@angular-eslint/eslint-plugin-template": "~13.0.1",
    "@angular-eslint/template-parser": "~13.0.1",
    "@angular/cli": "^14.0.0",
    "@angular/compiler": "^14.0.0",
    "@angular/compiler-cli": "^14.0.0",
    "@angular/language-service": "^14.0.0",
    "@capacitor/cli": "4.4.0",
    "@ionic/angular-toolkit": "^6.0.0",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "@typescript-eslint/eslint-plugin": "5.3.0",
    "@typescript-eslint/parser": "5.3.0",
    "eslint": "^7.6.0",
    "eslint-plugin-import": "2.22.1",
    "eslint-plugin-jsdoc": "30.7.6",
    "eslint-plugin-prefer-arrow": "1.2.2",
    "jasmine-core": "~3.8.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~6.3.2",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "prettier": "^2.7.1",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "typescript": "~4.7.3"
  },
  "description": "An Ionic project"
}
Sampath-Lokuge commented 1 year ago

I have sent you the source code. Please confirm it. Thanks!

Sampath-Lokuge commented 1 year ago

I have sent you a Git invitation for the source code. Please see that. Thanks!

jepiqueau commented 1 year ago

@Sampath-Lokuge i do not have receive any invitation

Sampath-Lokuge commented 1 year ago

Sent it again. Please see it in the spam folder too.

j

jepiqueau commented 1 year ago

@Sampath-Lokuge can you close the issue if everything is solved on your side

Sampath-Lokuge commented 1 year ago

Your this feedback gave me a hint. https://github.com/capacitor-community/sqlite/issues/338#issuecomment-1288136413

i.e. I have opened 2 connections for old db and new db. So it must be a Singleton one. So I have fixed that and all are working fine with Capacitor 4 and your new plugin version. Thanks a lot for your support.