【问题标题】:Error while executing transactions in Ionic 2 app using Sqlite from Ionic Native使用 Ionic Native 的 Sqlite 在 Ionic 2 应用程序中执行事务时出错
【发布时间】:2016-12-30 05:22:21
【问题描述】:

我有一个项目,其中我有一个设置和查询本地 sqlite 数据库的服务。我正在使用来自 Ionic Native 的 cordova-sqlite-plugin 和 Sqlite。

这是打开数据库后我必须打开数据库并创建表的代码。

import {Injectable} from '@angular/core';
import {SQLite} from "ionic-native";

@Injectable()
export class LocalDbService {

sqlDb: SQLite;

constructor() {
    this.sqlDb = new SQLite();
}

openDatabase() {
    return this.sqlDb.openDatabase({
        name: 'app.db',
        location: 'default'
    }).then(() => {
        return this.sqlDb.transaction((tx) => {
            tx.executeSql(`create table if not exists groups (
              id nvarchar(50) primary key not null,
              name nvarchar(50) unique not null,
              createdOn datetime not null default current_timestamp,
              updatedOn datetime not null default current_timestamp,
              deleted boolean not null default 0
            )`, [])
        })
    }).catch((err) => {
      console.error('Error while creating tables', err);
    })
}
}

数据库正确打开。但是,问题是创建表的事务总是抛出错误。在我记录错误的捕获阶段,它始终是未定义的。

如果我执行一个简单的 executeSql 方法而不是事务,它运行良好。我目前正在基于 Android 6.0 的设备上运行代码。

我已经阅读了 Cordova s​​qlite 插件中sql transactions 的以下文档以及 Ionic native 中 Sqlite 插件的文档。

在 Ionic 2 中使用 Sqlite 事务的正确方法是什么?

【问题讨论】:

    标签: sqlite cordova angular transactions ionic2


    【解决方案1】:

    我认为您遇到了错误,因为您无法在 then 中访问 this.sqlDb,如果 openDatabase 参考您打开的数据库返回承诺,您可以尝试以下操作:

    this.sqlDb.openDatabase({
        name: 'app.db',
        location: 'default'
    }).then((db) => {
        return db.transaction((tx) => {
            tx.executeSql(`create table if not exists groups (
              id nvarchar(50) primary key not null,
              name nvarchar(50) unique not null,
              createdOn datetime not null default current_timestamp,
              updatedOn datetime not null default current_timestamp,
              deleted boolean not null default 0
            )`, [])
        })
    }).catch((err) => {
      console.error('Error while creating tables', err);
    });
    

    或者如果openDatabase 没有返回承诺,那么您必须在创建数据库后通过检查数据库是否打开来创建事务:

    this.sqlDb.openDatabase({
        name: 'app.db',
        location: 'default'
    });
    if(this.sqlDb) { 
        this.sqlDb.transaction((tx) => {
            tx.executeSql(`create table if not exists groups (
                id nvarchar(50) primary key not null,
                name nvarchar(50) unique not null,
                createdOn datetime not null default current_timestamp,
                updatedOn datetime not null default current_timestamp,
                deleted boolean not null default 0
            )`, [])
        });
    }
    

    【讨论】:

    • this.sqlDb 绝对可以在 promise-> then 部分中使用,因为我正在使用箭头函数。我什至通过注销来检查它。它正确运行除事务之外的方法。当我调用事务方法时,它只会引发错误。这种行为不仅存在于 openDatabase 承诺中。在我使用它的任何地方,使用事务时都会引发错误。感谢您的帮助。
    • 我的项目中的行为相同。不知何故,交易无法正常运作。
    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2017-05-10
    • 2019-02-25
    • 2018-12-01
    • 2017-08-09
    • 2016-09-04
    • 2018-01-27
    相关资源
    最近更新 更多