未定义Ionic2 sqlitePlugin
如何避免此错误:VM18193:27无法打开数据库引用错误:未定义sqlitePlugin(…) 如何执行一些查询未定义Ionic2 sqlitePlugin,ionic2,Ionic2,如何避免此错误:VM18193:27无法打开数据库引用错误:未定义sqlitePlugin(…) 如何执行一些查询 if(sqlsetingsservice.openDb){ this.db=sqlsetingsservice.getDB(); 这个.db.executeSql(“如果不存在创建表的人(id INTEGER主键自动递增,firstname文本,lastname文本)”,{})。然后插件sqlLite在浏览器中不起作用,您可以使用Websql作为浏览器(据我所知,与兼容的浏览器,
if(sqlsetingsservice.openDb){
this.db=sqlsetingsservice.getDB();
这个.db.executeSql(“如果不存在创建表的人(id INTEGER主键自动递增,firstname文本,lastname文本)”,{})。然后
插件sqlLite在浏览器中不起作用,您可以使用Websql作为浏览器(据我所知,与兼容的浏览器,包括Chrome和Opera)
为Websql编写的事务与为SQLite编写的事务兼容
以下是我为管理与DB的连接所做的一项服务,无论程序是在浏览器中运行还是在真实设备上运行,它都能正常工作:
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();
}
}
要执行查询,请执行以下操作:
[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);
}
});
插件sqlLite在浏览器中不起作用,您可以将Websql用于浏览器(据我所知,兼容浏览器包括Chrome和Opera) 为Websql编写的事务与为SQLite编写的事务兼容 以下是我为管理与DB的连接所做的一项服务,无论程序是在浏览器中运行还是在真实设备上运行,它都能正常工作:
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();
}
}
要执行查询,请执行以下操作:
[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);
}
});
错误发生在浏览器或真实设备上?@nyluje,在浏览器中?错误发生在浏览器或真实设备上?@nyluje,在浏览器中?我添加了一些有关执行查询的详细信息。最后,我建议使用Promise或Observable来管理异步SQL结果。您忘记了SqlExecute中的success和error函数。请看一看关于这一点:winSer是什么?winSer.window.openDatabasewinSer是
window
对象服务(在我的回答中有详细说明)。您需要访问浏览器的窗口
对象才能使用websql命令。我添加了一些有关执行查询的详细信息。最后,我建议使用Promise或Observable来管理异步SQL结果。您忘记了SQLExecute中的success and error函数。另外,请查看:什么是winSer?winSer.window.openDatabasewinSer是窗口
对象服务(在我的回答中有详细说明)。您需要访问浏览器的窗口
对象才能使用websql命令。