Trouble accessing the Ionic SQLite database: Unable to open

I encountered some errors while attempting to connect my ionic app to a database. I am currently running the app on an android device using Google Chrome DevTools to troubleshoot the issue.

Check out the createDatabase() function and the specific error that was generated.

createDatabase() {
    this.plt.ready().then(() => {
      if (this.plt.is('cordova')) {
        this.sqlite = new SQLite();
        this.sqlite.create({
          name: 'ocp_database_fuits.db',
          location: 'default'
        }).then((db: SQLiteObject) => {
          this.database = db;
          this.createTables();
        }).catch(e => {
          console.log(e);
        });
      }
    });
  }

OPEN database: ocp_database_fuits.db

SQLitePlugin.js:197 OPEN database: ocp_database_fuits.db

FAILED, aborting any pending transactions main.js:686 Error: Could not open database

  at newSQLError (SQLitePlugin.js:26)
  at SQLitePlugin.js:199
  at Object.callbackFromNative (cordova.js:290)
  at <anonymous>:1:9

Answer №1

Have you tried removing and then reinstalling the plugin? I tested your code and it seems to be working fine for me.

Uninstall

ionic cordova plugin rm cordova-sqlite-storage
npm uninstall @ionic-native/sqlite

Install

ionic cordova plugin add cordova-sqlite-storage
npm install @ionic-native/sqlite

Answer №2

To address the issue you're facing, please carefully follow these steps:

npm update
npm run build
ionic cordova plugin rm cordova-plugin-sqlite-2

Next, make the following adjustments in your database file:

createDatabase(): void {
this.plt.ready().then(() => {
  if (this.plt.is('cordova')) {
    this.sqlite = new SQLite();
    this.sqlite.create({
      name: 'ocp_database_fuits.db',
      location: 'default'
    }).then((db: SQLiteObject) => {
      this.database = db;
      this.createUserTable(this.database);
      this.createPathTable(this.database);
    }).catch(e => {
      console.log(e);
    });
  }
});
}

Additionally, ensure to execute the following:

createUserTable(db): void {
    // const d: SQLiteObject;
    db.executeSql('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, pass TEXT)', [])
    .then(() => {
      console.log('table users created');
    }).catch(e => {
      console.log('error in createUserTable function');
      console.log(e);
    });
  }

createPathTable(db): void {
    db.executeSql('CREATE TABLE IF NOT EXISTS paths (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, type TEXT, points TEXT', [])
    .then(() => {
      console.log('table paths created');
    }).catch(e => {
      console.log('error in createPathTable function');
      console.log(e);
    });
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enumerate all interdependencies among 2 directories or libraries in an Angular application

I am currently working on a large Angular project and need to refactor some code by identifying dependencies between two specific folders/libs (using nx). Here is an example of the file structure: /apps /lib-1 a.service.ts b.component.t ...

The source 'http://localhost:3000' is not authorized to access the Twitter API

I am working on a web application that utilizes the Twitter API REST. On one of my pages, I have a list of Twitter accounts and a button to add a new account. When this button is clicked, it triggers a function in the Angular controller: // Function to ...

Exploring the TypeScript Type System: Challenges with Arrays Generated and Constant Assertions

I am currently grappling with a core comprehension issue regarding TypeScript, which is highlighted in the code snippet below. I am seeking clarification on why a generated array does not function as expected and if there is a potential solution to this pr ...

Is it possible for a JavaScript environment to restore function normally after modifying the [[Prototype]] of an object?

After thoroughly reviewing the MDN disclaimers and warnings, as well as an enlightening discussion in a popular online forum, I still find myself seeking answers. This question arose from a previous exchange, which can be found here. Imagine if I were to ...

Strategies for avoiding asynchronous behavior in AJAX with JQuery

My questions are best explained through code rather than words. Take a look at the snippet below and try to understand it: $('#all').on('change', function(){ //ONCHANGE RADIOBUTTON, THERE ARE #bdo, #cash also. $.ajax({ type:"GET", ...

Converting a JavaScript array to a PHP array using POST request

In my JavaScript script, I have the following code: cats = []; cats.push(cat1); cats.push(cat2); $.post( URL+"/edit-article.php", { id: artId, title: "PZujF0 wxKLCW", content: "ILEn3o oU9Ft6oU5", author: author, cat_id: cats } ).done(function( data2 ) ...

JavaScript will continue to run uninterrupted even after refreshing the webpage

Has anyone else encountered the issue of a javascript on a page continuing to run even after the page is refreshed? From what I understand, javascript is single-threaded and should stop running when the page is refreshed. Just to provide some background, ...

Obtain personalized results for implementing in Convase JS from a PHP server

I have a table on my WordPress site with the following output: { { [line1]=> array(3) {{'x'=>'5' , 'y'=>'8},{'x'=>'5' , 'y'=>'8},{'x'=>'5' , &apos ...

What is the best way to ensure the search box remains fixed in the top navigation bar while maintaining a fluid and responsive design?

I've been struggling as a novice programmer, and even with the help of more experienced programmers, we haven't been able to figure it out. I'm trying to integrate a search box into the top navigation that adjusts responsively to different ...

Utilizing the power of Datatables through AJAX calls with HTML Forms

Hello, I am currently working on incorporating djangorestframework-datatables with the datatables' JQuery plugin. My task involves loading a large table (consisting of approximately 15000 entries, paginated) with the serverSide option enabled. Enabli ...

How can I display the output from Geocoder in a text box using the ArcGIS JavaScript API?

I am trying to customize the Geocoder text box in the ArcGIS JavaScript API by overriding the default search result. Although I have written some code for this purpose, I am not satisfied with the results. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...

angular5: The ngFor directive will only function properly after the second button click

Here is my current situation: 1) When the user inputs a keyword in a text field and clicks on the search icon, it triggers an HTTP request to retrieve the data. 2) The retrieved data is then rendered in HTML using ngFor. The issue I am facing is that up ...

Creating a Vue component that leverages the computed property from a mixin

My situation involves a straightforward component that utilizes a mixin shared across various components that have similar functionalities. However, upon running it, an issue arises: The error message "Property or method 'activeClass' is not ...

Angular 13: A guide on pulling data from an Excel spreadsheet

I have been encountering issues while trying to display data from a CSV file on a web platform using Angular 13. The errors I am facing are related to binding 'ngModel' and type mismatches in the code. errors Error: src/app/app.component.html:24 ...

Which option offers superior performance: using attributes through HTML or jQuery?

At times, I find myself needing to include special classes, divs, and attributes in my HTML code for jQuery scripts to function properly (the site's "no-JavaScript" version doesn't require these elements). You probably understand what I mean. I& ...

Adding a JavaScript variable into a Django template tag

This particular situation has been presenting a challenge for me. So far, I have been using query parameters instead of a variable within the {% url %} tag. However, I can't help but wonder if there is a way to achieve this: I am interested in includ ...

Trouble accessing setState within an axios call in ReactJs

I've encountered an issue while attempting to set the state of the variable isCorrectAnswer within an axios call. The error message Cannot read properties of undefined (reading 'setState') is showing up in the console log. What mistake am I ...

What is the purpose of adding "/dist" to the library import statement?

Currently, I am in the process of developing a react component library using vite as my primary build tool. After successfully compiling the project and deploying it to the npm registry, I encountered an issue when importing it into my client app. Specifi ...

Collaborating and monitoring data between controllers

A unique challenge has arisen as we implement a tree-style navigation element that must communicate with other directives/controllers. The main objectives are: Keep track of the current selection, Detect when the row selection changes. The task at hand ...

Add an unidentified element into a sealed directive's scope

Is it possible to inject a value into a directive's close scope? I couldn't find any information about this in the angularjs documentation, so I decided to experiment. Does anyone know of a more elegant solution to this issue? Here is my current ...