使用Ionic 2中的Cordova Sqlite插件 [英] Working with Cordova Sqlite plugin in ionic 2

查看:74
本文介绍了使用Ionic 2中的Cordova Sqlite插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ionic 2开发的新手(也没有与ionic-1一起工作).

I'm new to ionic 2 development (haven't worked with ionic-1 either).

我正在尝试在应用程序中使用cordova s​​qlite插件.我遵循了在Ionic中使用SQLite2 链接.能够使用以下命令将插件添加到项目中.

I'm trying to use cordova sqlite plugin in the application. I followed Use SQLite in Ionic 2 link. Was able to add the plugin into to project using following command.

$ ionic插件添加cordova-sqlite-storage

$ ionic plugin add cordova-sqlite-storage

在真实设备上部署应用程序后,创建并打开了data.db,并且成功创建了 people 表.以下是来自Xcode的日志:

After deploying the app on real device data.db was created and opened and also the peopletable was created successfully. Following is the log from the Xcode :

2016-10-17 16:29:16.435992 SqlProject [238:3982]-[SQLitePlugin pluginInitialize] [67行]路径上没有云同步:/var/mobile/Containers/Data/Application/DFDADD59-D48E-4D4C-B8F0-23EEDE169471/库/LocalDatabase2016-10-17 16:29:16.436138 SqlProject [238:4034]-[SQLitePlugin openNow:] [第137行]打开完整的数据库路径:/var/mobile/Containers/Data/Application/DFDADD59-D48E-4D4C-B8F0-23EEDE169471/Library/LocalDatabase/data.db2016-10-17 16:29:16.443765 SqlProject [238:4034]-[SQLitePlugin openNow:] [第163行]好消息:SQLite是线程安全的!2016-10-17 16:29:16.448487 SqlProject [238:3982] 打开数据库:data.db-确定2016-10-17 16:29:16.463667 SqlProject [238:3982] 创建的表: [对象对象]

2016-10-17 16:29:16.435992 SqlProject[238:3982] -[SQLitePlugin pluginInitialize] [Line 67] no cloud sync at path: /var/mobile/Containers/Data/Application/DFDADD59-D48E-4D4C-B8F0-23EEDE169471/Library/LocalDatabase 2016-10-17 16:29:16.436138 SqlProject[238:4034] -[SQLitePlugin openNow:] [Line 137] open full db path: /var/mobile/Containers/Data/Application/DFDADD59-D48E-4D4C-B8F0-23EEDE169471/Library/LocalDatabase/data.db 2016-10-17 16:29:16.443765 SqlProject[238:4034] -[SQLitePlugin openNow:] [Line 163] Good news: SQLite is thread safe! 2016-10-17 16:29:16.448487 SqlProject[238:3982] OPEN database: data.db - OK 2016-10-17 16:29:16.463667 SqlProject[238:3982] TABLE CREATED: [object Object]

打开数据库的代码:

从"ionic-native"导入{SQLite};

import { SQLite } from 'ionic-native';

 constructor(platform: Platform) {
   platform.ready().then(() => {
   StatusBar.styleDefault();

   let database = new SQLite();
        database.openDatabase({
            name: "data.db",
            location: "default"
        }).then(() => {
            database.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 create table", error);

            })
        }, (error) => {

            console.error("Unable to open database", error);
        });
   });
}

但是,现在插入从数据库中获取记录时出现错误.

But, now I'm getting error while Inserting and Fetching the records from the db.

 public add() {

 console.log("Inside the add function- ");

 this.database.executeSql("INSERT INTO people (firstname, lastname) VALUES (?, ?)", ['mahesh', 'bhalerao']).then((data) => {

   console.log("INSERTED: " + JSON.stringify(data));

 }, (error) => {
   console.log("ERROR: " + JSON);
 });
}

 public fetchRecords() {

    this.database.executeSql("SELECT * FROM people", []).then((data) => {
               alert("executeSql refresh function success");

        this.people = [];

        if(data.rows.length > 0) {
            for(var i = 0; i < data.rows.length; i++) {
                this.people.push({firstname: data.rows.item(i).firstname, lastname: data.rows.item(i).lastname});
            }
        }
    }, (error) => {
        console.log("ERROR: " + JSON.stringify(error.err));
    });
}

我收到未定义的错误.

错误:未定义

不知道出了什么问题.

任何帮助将不胜感激.谢谢!

Any help would be appreciated. Thanks!

推荐答案

第一行,第五行:您有 let database = new SQLite(); ;您将数据库创建到磁盘上(如果尚不存在),然后从构造器中退出.

First snippet, fifth line: you have let database = new SQLite();; you create the database onto the disk (if it doesn't exists yet) and then you quit from contructor.

由于 let 的作用域,一旦您从构造函数退出,则 database 变量不存在,将其添加到 add fetch 方法从未存在过.

Due to the scoping of let, once you exit from the costructor, the database variable doesn't exists, to the add and fetch methods it has never existed.

您应该将 database 放入属性(即方法在其中查找的位置)中,而不是 let ,然后一切正常.

Instead of let you should put database into an attribute (that is where the methods are looking for it) and then everything should work fine.

换句话说,您应该具有类似的代码:

In other words you should have a similar code:

public database: SQLite //here the attribute

constructor(platform: Platform) {
   platform.ready().then(() => {
   StatusBar.styleDefault();

   this.database = new SQLite(); //here you initialize the attribute instead of a volative variable
   this.database.openDatabase({ //in this block you open an then populate the attribute
            name: "data.db",
            location: "default"
        }).then(() => {
            database.executeSql("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)").then((data) => {
                console.log("TABLE CREATED: ", 
            }, (error) => {
                console.error("Unable to create table", error);
            });
        }, (error) => {
            console.error("Unable to open database", error);
        });
   });
}

这篇关于使用Ionic 2中的Cordova Sqlite插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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