2016-08-23 34 views
1

ローカルのsqliteデータベースを設定してクエリするプロジェクトがあります。私はcordonic-sqlite-pluginとSqliteをIonic Nativeから使用しています。Ionic 2のアプリケーションでトランザクションを実行しているときに、Ionic Nativeの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ベースのデバイスでコードを実行しています。

私はsql transactionsのドキュメントをCordova s​​qliteプラグインとIonicネイティブのSqliteプラグインのドキュメントで読んだことがあります。

Ionic 2でSqliteトランザクションを使用する正しい方法は何ですか?

答えて

0

私はあなたがエラーを取得していると思いますがthenthis.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 
     )`, []) 
    }); 
} 
+0

this.sqlDbが、その後の部分、私は矢印のfuを使用しているよう>プロミスの内側間違いなく入手可能です:あなたはDBが好きで開かれているかどうかチェックすることで、DBを作成した後、トランザクションを作成する必要があり、その後約束nctions。私はそれをログアウトしてチェックしました。トランザクションとは別にメソッドを実行します。私はトランザクションメソッドを呼び出すときにだけエラーをスローします。この動作は、openDatabaseの約束の中だけではありません。どこでも私はそれを使用して、トランザクションを使用すると、エラーをスローします。助けてくれてありがとう。 –

+0

私のプロジェクトでは同じ動作をしています。どういうわけかトランザクションは彼らがどのようにすべきかを動かさない。 –

関連する問題