带 SQlite 插件的 Ionic 无法创建 table

Ionic with SQlite plugin cannot create table

我正在使用 ionic Sqlite 原生插件。尝试创建 table 时,我在控制台中收到此错误:

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

我不明白,因为我认为我的代码没问题。

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());
  });
  }

有人可以帮忙吗? 提前谢谢你

ionic SQLite 的 executeSql 需要 2 个参数查询和数据。如果没有数据传递一个空数组对象。

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());
  });

如果您传递第二个参数,您的查询工作正常。