首页 > 解决方案 > 删除表时,Ionic SQLite 无法读取未定义的属性“executeSql”

问题描述

我是 Angular 的新手,在将数据同步到服务器并面临这个问题之前,我使用 SQLite 将数据存储在本地。

在此处输入图像描述

我想在生成新表之前删除我的 SQLite DB 中的表,但是当我运行删除查询时会发生错误。这是我的代码:

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

@Injectable()
export class LocalProvider {

  public db: SQLiteObject;

  constructor(
    public http: HttpClient,
    public sql: SQLite
  ) {
    console.log('Hello LocalProvider Provider');
  }

  GenerateDB() {
    return new Promise((resolve, reject) => {
      if (this.db != null) {
        resolve(this.db);
      } else {
        this.sql.create({
          name: 'localpriok.sql',
          location: 'default'
        }).then((db: SQLiteObject) => {
          this.db = db;
          this.C_equipment_parrent();
          this.C_equipment();
          this.C_equipment_parameter();
          this.C_master_parameter();
          this.C_master_area();
          this.C_log_inspeksi();
          resolve(this.db);
        }).catch((e) => {
          console.log(JSON.stringify(e));
          reject(e);
        });
      }
    });
  }

  // error happens here
  async Truncate(TABLE) {
    // let query = 'DELETE FROM ' + TABLE;
    return new Promise((resolve, reject) => {
      this.db.executeSql('DELETE FROM ' + TABLE, []).then((data) => {
        resolve(data);
        // alert('Execute SQL, ' + data);
      }, (error) => {
        reject(error);
        // alert(JSON.stringify(error));
      })
    });
  }

  async Insert_multi_to(TABLE, DATA) {
    this.Truncate(TABLE).then(data => {
      alert(JSON.stringify(data));
    });
    await this.GenerateDB().then((db: SQLiteObject) => {
      let query = this.GenerateInsertMultiData(TABLE, DATA);
      console.log(query);
      db.executeSql(query, [])
        .then(() => console.log('Executed SQL'))
        .catch(e => console.log('YANG ERROR : ', query + 'ERRORNYA : ' + JSON.stringify(e)));
    });
  }

有没有其他人遇到过这种情况,请帮助我。谢谢。

标签: angularsqliteionic-framework

解决方案


推荐阅读