【问题标题】:Ionic project with database带数据库的离子项目
【发布时间】:2017-08-18 19:54:01
【问题描述】:

我正在尝试创建一个移动项目,这是我在 Ionic 平台上的第一个项目。 同样,我必须学习 Angular 和 Ionic。所以我决定做一个简单的移动项目。 我在移动技术中搜索了太多数据库,所以我得到了很多数据库,比如 MongoDb、SQLite、Firebase 等。所以我对在 ionic 移动项目中应该使用哪个数据库感到有点困惑? 是否有任何数据库的初学者文档可以帮助我在我的 ionic 项目中实现数据库?

非常感谢。

【问题讨论】:

  • 使用 firebase 尤其是在您尝试使用它时,例如 firebase.googleblog.com/2015/05/… 它需要几秒钟才能在数据库中获取数据,您可以花更多时间学习 ionic 框架。

标签: angularjs mongodb sqlite ionic-framework firebase-realtime-database


【解决方案1】:

为了培训的目的,我建议不要一次全部搞定,我建议只从 Angular 本身开始。然后,在第二个项目中,尝试 Ionic。

角度的一些好的起点是:
https://www.codecademy.com/pt-BR/learn/learn-angularjs
http://www.learn-angular.org/

然后,对于 Ionic,我使用了: https://thinkster.io/ionic-framework-tutorial

现在,特别是关于数据库
Ionic 与 cordova 一起工作,它在 Web 开发和移动原生功能之间建立了联系。它通过插件实现。

Android 和 IOS 原生支持 SQLite。所以,如果你想尽可能地使用原生资源,我认为 SQLite 是最好的选择。

最好的插件(恕我直言)是https://github.com/litehelpers/Cordova-sqlite-storage

这个插件很容易使用:

在 Ionic cordova 项目上,运行

cordova plugin add cordova-sqlite-storage

然后,在您的代码中,使用

var db = window.sqlitePlugin.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);

然后,只需运行您的 SQL:

db.executeSql("select length('tenletters') as stringlength", [], function (res) {
      var stringlength = res.rows.item(0).stringlength;
      console.log('got stringlength: ' + stringlength);
      document.getElementById('deviceready').querySelector('.received').innerHTML = 'stringlength: ' + stringlength;
   });

在插件网站上,有更多完整的例子。

但是,我再次建议先学习和平。

编辑

回复评论:

添加关于数据库的信息很简单,就像上面的 SELECT 示例一样。只需在 executeSQL() 的 2º 参数上将数据作为数组传递。像这样:

db.executeSql('INSERT INTO MyTable VALUES (?)', ['test-value'], function (resultSet) {
  console.log('resultSet.insertId: ' + resultSet.insertId);
  console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
}, function(error) {
  console.log('SELECT error: ' + error.message);
});

查看文档,那里还有其他示例。

【讨论】:

  • 感谢您提供重要信息。我还有一个问题,如何通过js文件的软件实现或向SQLite数据库添加数据?
  • @PIrodotou,我编辑了答案以添加插入示例。看一看。如果它回答了您的问题,请点赞并标记为已接受
【解决方案2】:

我一直在浏览一篇又一篇“不是我正在寻找的”答案的文章。我找到了一个法语视频。这里是:Ionic 3 Store Data

话虽如此,这里是如何设置您的代码以使 ionic 3 cordova s​​qlite 可用。

1) 通过在 npm 或 cmd 提示符下运行这 2 个命令来导入您的原生 sqlite。

  • ionic cordova plugin add cordova-sqlite-storage
  • npm install --save @ionic-native/sqlite

2) 导入你的 app.module.ts

  • import { SQLite} from '@ionic-native/sqlite';

3) 在你的 app.module.ts 中添加为提供者

providers: [
   ...
    SQLite,
   ...
  ]

4) 创建一个新文件夹(如果你想让它成为一个不同的组件)并制作一个数据库 ts 文件。为了方便起见,我调用了我的 database.ts

5) 添加以下代码(请注意,这不是我使用的真实代码。只是一个示例。用户名和密码不应以这种方式存储):

import { Injectable } from '@angular/core';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';


 @Injectable()
export class Database {

    theConsole: string = "Console Messages";

    options: any = {
        name: data.db,
        location: 'default',
        createFromLocation: 1
    }

    private db: SQLiteObject;

    constructor(private sqlite: SQLite) {

        this.connectToDb();
    }

    private connectToDb():void {

        this.sqlite.create(this.options)
            .then((db: SQLiteObject) => {

                this.db = db;
                var sql = 'create table IF NOT EXISTS `user` (username VARCHAR(255), password VARCHAR(255))';
                //IF you move the below statment out of here then the db variable will not be initialized
                //before you can use it to execute the SQL. 
                this.db.executeSql(sql, {})
                .then(() => this.theConsole += 'Executed SQL' + sql)
                .catch(e => this.theConsole += "Error: " + JSON.stringify(e));
            })
            .catch(e => this.theConsole += JSON.stringify(e));

    }

     addUser(username, password):void {

        var sql = "INSERT INTO `user` (username,password) VALUES ('"+username+"','"+ password+"')";

        this.db.executeSql(sql,{})
        .then(() => this.theConsole += "\n" + 'Executed SQL' + sql)
        .catch(e => this.theConsole += "Error: " + JSON.stringify(e));


    }
    getDealer() {
        var sql = "SELECT * FROM user";

        this.db.executeSql(sql, {})
            .then((result) => {
                this.theConsole += JSON.stringify(result);
                if (result.rows.length > 0) {
                    this.theConsole += 'Result' + result.rows.item(0);
                }
                this.theConsole += "\n" + result.rows.item(0).username+ result.rows.item(0).password;
                this.theConsole +=  "\n" +'Rows' + result.rows.length;

            })

            .catch(e => this.theConsole += JSON.stringify(e));
    }

    getConsoleMessages() {
        return this.theConsole;
    }
}

然后,您只需将数据库组件(类)导入您的一个页面,您就可以通过运行这些函数或创建您自己的 RunSQL 函数来访问数据库,您可以将任何您想要的东西放入其中。

真正让我在 ionic 的网站上感到困惑的部分是,他们展示的是 SQLiteObject 的创建而不是重用。 通过添加:

private db: SQLiteObject;

在类变量的声明和db对象的初始化中我的代码:

 ...
 this.sqlite.create(this.options)
            .then((db: SQLiteObject) => {

                this.db = db;
 ...

我能够重复使用 db 变量,而不必一遍又一遍地打开 db 连接。

6) 将组件类导入页面

import { Database } from '../../data/database';

我使用这个网站:ionic native sqlite 主要了解如何设置它以及前面提到的法语视频。我希望我会发现我希望可以帮助其他人碰到同样的 sqlite 墙。我希望我能早点找到我今天发现的东西。我希望可以帮助其他人碰到同样的 sqlite 墙。

【讨论】:

    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    相关资源
    最近更新 更多