0

I’m using ionic Sqlite native plugin. When trying to create table I’m getting this error in the console:

“Console MessagesError: {“rows”:{“length”:0},“rowsAffected”:0}”

which I don’t understand cause I think my code is ok.

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { SQLite, SQLiteObject } from "@ionic-native/sqlite";


@Injectable()
export class DatabaseProvider {
theConsole: string = "Console Messages";

options: any = {
name: 'db_name.db',
location: 'default'
}

private db: SQLiteObject;
private isOpen: boolean;

constructor(public http: HttpClient,private sqlite: SQLite) {
console.log('Hello DatabaseProvider Provider');
this.connectToDb();
}

private connectToDb():void {

  this.sqlite = new SQLite();
  this.sqlite.create(this.options)
    .then((db: SQLiteObject) => {
      this.db = db;
      this.isOpen = true;
      console.log('Hello DatabaseProvider connected to db');
      this.createTables();
    })
    .catch(e => {
      this.theConsole += JSON.stringify(e);
      console.log(this.getConsoleMessages());
    });

  }

  private createTables():void{
  this.createTableContatti();
   }

 private createTableContatti(): void{
 var sql = 'create table IF NOT EXISTS contatti(id_contatto INTEGER PRIMARY 
 KEY AUTOINCREMENT,nome TEXT, data_nascita TEXT)';
 this.db.executeSql(sql)
  .then(() => {
  this.theConsole += 'Executed SQL' + sql
  console.log(this.getConsoleMessages());
  })
  .catch(e => {
    this.theConsole += "Error: "  + JSON.stringify(e)
    console.log(this.getConsoleMessages());
  });
  }

Can someone help please? Thank you in advance

1 Answer 1

1

The ionic SQLite's executeSql requires 2 parameters query and data. If there is no data pass an empty array object.

var sql = 'create table IF NOT EXISTS contatti(id_contatto INTEGER PRIMARY 
 KEY AUTOINCREMENT,nome TEXT, data_nascita TEXT)';
 this.db.executeSql(sql,{})
  .then(() => {
  this.theConsole += 'Executed SQL' + sql
  console.log(this.getConsoleMessages());
  })
  .catch(e => {
    this.theConsole += "Error: "  + JSON.stringify(e)
    console.log(this.getConsoleMessages());
  });

Your query works fine if you pass the second parameter.

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

6 Comments

If I add the second parameter, I have this compile error: "Argument of type '{}' is not assignable to parameter of type 'any[]'. Property 'length' is missing in type '{}'." Because of than I removed the second parameter. Do you know why doesn't compile?
I ran your code with the second parameter and it worked. Can you show me the change you did?
private createTableContatti(): void{ var sql = 'create table IF NOT EXISTS contatti(id_contatto INTEGER PRIMARY KEY AUTOINCREMENT,nome TEXT, data_nascita TEXT)'; this.db.executeSql(sql,{}) .then(() => { this.theConsole += 'Executed SQL' + sql; console.log(this.getConsoleMessages()); }) .catch(error => { let errMsg = (error.messageText) ? error.messageText : error.status ; this.theConsole += "Error: " + errMsg + " " + JSON.stringify(error); console.log(this.getConsoleMessages()); }); }
Your query works fine. Can you try removing all extra code and then running? just log "success" or "error" in callbacks.
Also if this doesn't work try passing empty array [] instead of {}
|

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.