Ionic2 sqlitePlugin未定义 [英] Ionic2 sqlitePlugin is not defined

查看:116
本文介绍了Ionic2 sqlitePlugin未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何规避此错误:VM18193:27无法打开数据库ReferenceError:未定义sqlitePlugin(...)

How to evade this error: VM18193:27 Unable to open database ReferenceError: sqlitePlugin is not defined(…)

    setTimeout(function() {
      let db = new SQLite();
      db.openDatabase({
        name: "data.db",
        location: "default"
      }).then(() => {
        db.executeSql("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)", {}).then((data) => {
          console.log("TABLE CREATED: ", data);

        }, (error) => {
          console.error("Unable to execute sql", error);
        })
      }, (error) => {
        console.error("Unable to open database", error);
      });
    }, 2000);

我如何执行某些查询?

if(SqlSettingsService.openDb){
      this.db = SqlSettingsService.getDB();
      this.db.executeSql("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT", {}).then


而不是我得到错误。
console:

instead i get error. console:

SqlSettingsService() starts
VM21750:27 Unhandled Promise rejection: Cannot read property 'executeSql' of null ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot read property 'executeSql' of null(…) TypeError: Cannot read property 'executeSql' of null


推荐答案

插件sqlLite不起作用在浏览器中,您可以使用Websql代替浏览器(据我所知,使用兼容的浏览器,包括Chrome和Opera)。

The plugin sqlLite does not work in a browser, you can use Websql for the browser instead (with compatible browsers, including Chrome and Opera as far as I know).

为Websql编写的事务与为SQLite编写的事务。

Transactions written for Websql are compatible with transactions written for SQLite.

这是我为管理与数据库的连接所做的一项服务,无论程序是在浏览器中运行还是在在真实设备上:

Here is a service I have done to manage the Connection to the DB and make it work regardless if the program runs in a browser or a on real device:

import { Injectable } from '@angular/core';
import { SQLite } from 'ionic-native';
import { Platform } from 'ionic-angular';
import { Storage } from '@ionic/storage';

@Injectable()
export class SqlSettingsService {

    private db: any = null;
    private isOpened: boolean = false;


    constructor() { 
        console.log('SqlSettingsService() starts');
    }

    public getDB(){
        return this.db;
    }

    public openDb = (platform:Platform,winSer:any):Promise<any> => {
        console.log('SqlSettingsService() opend DB starts');
        let p:Promise<any>;
        if(!this.isOpened){
            this.isOpened = true;

            if(platform.is('core')){
                this.db = winSer.window.openDatabase("ionic2BrowserDev","1.0","",5*1024*1024);
                p = new Promise(function(resolve,reject){resolve('websql success')});
            } else {
                this.db = new SQLite();
                p = this.db.openDatabase({
                    name: 'data.db',
                    location: 'default' // the location field is required
                }).then(
                    ()=>{console.log("SqlSettingsService open db successful")},
                    (err)=>{console.error(err)}
                );
            }

        } else {
             p = new Promise(function(resolve,reject){
                resolve('db already opened');
             });
        }
        return p;

    }

    public closeDb = () => {
        this.isOpened = false;
        return this.db.close();
    }


}

winSer是另一个访问窗口的服务当我在SqlSettingsService上调用openDB()时,我在app.component.ts中使用的对象。就是这样:

winSer is another service to access the window Object that I use in my app.component.ts when I call openDB() on SqlSettingsService. It's just this:

import { Injectable } from '@angular/core';

@Injectable()
export class WindowService {
  public window = window;
}

执行查询:

    [SqlSettingsService-instance].openDb();
[SqlSettingsSevice-instance].getDB().transaction(
            function(tx){
                tx.executeSql([your sql],[bracket values you want to pass],success,error);
                function success(tx,rs){
                     console.log("success exec sql: ")
                     console.info(rs);

                }
                function error(tx,error){
                     console.log('execSqlCustom error ' + error.message + " for tx " + tx);

                }
            });

这篇关于Ionic2 sqlitePlugin未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆