【问题标题】:Ionic Pre-populated Database Sqlite离子预填充数据库 Sqlite
【发布时间】:2017-07-15 10:36:37
【问题描述】:

所以我试图让一个预先填充的数据库在我的 ionic 应用程序中运行。

我正在使用Cordova-sqlite-storagefor sqlite 和cordova-plugin-dbcopy 将数据库复制到手机的内部存储中,但它不起作用。

正在(或似乎正在)工作:

  • window.plugins.sqlDB.copy("Test.db", 0, pass, fail) 第一次返回 pass 函数,第二次按预期失败(由于它已经存在)
  • db = window.sqlitePlugin.openDatabase({name: "Test.db"}); 我知道有效,因为以下查询有效:(下一个项目符号)

  • $cordovaSQLite.execute(db, "SELECT * FROM sqlite_master WHERE type='table'") 有效,但只返回表 android_metadata 而不是我的测试表 names(我确定它在 Test.db 文件中)

什么是不起作用

  • 如上所述,我只从查询中获取起始表,而不是应该存在的表

  • 结果,$cordovaSQLite.execute(db, "INSERT into names VALUES(2, 'hello')")给了我Error: no such table: names (code1):, while...

也许我在正确的位置没有 Test.db 文件。我把它放在 www 文件夹、platforms > android > assets 文件夹和 www > js 文件夹中,但没有成功(它是现在都在其中)。

这是我的代码:

"use strict";

angular.module('starter').controller('AboutController', function(
  $scope,
  $cordovaSQLite
) {


  $scope.$on('$ionicView.beforeEnter', function() {
    var db = null;

    if (window.sqlitePlugin) {

      // copy the database
      window.plugins.sqlDB.copy("Test.db", 0,

        function() {
          // copy success, if not yet been copied
          console.log("Copy Success");
          db = window.sqlitePlugin.openDatabase({
            name: "Test.db"
          });

          execute(db)
        },
        function(err) {
          // copy error, if it has already been copied

          db = window.sqlitePlugin.openDatabase({
            name: "Test.db"
          });
          console.log("Copy Fail: " + JSON.stringify(err));

          execute(db)
        })
    }
  });


  function execute(db) {
    $scope.dbData += "in execute;  ";


    $cordovaSQLite.execute(db, "INSERT into names VALUES(2, 'hello')")
      .then(function(res) {
          console.log("sql1: " + res.rows.length);
        },
        function(err) {
          console.log("sql1: " + err);
        });


    $cordovaSQLite.execute(db, "SELECT * FROM sqlite_master WHERE type='table'")
      .then(function(res) {

          for (var i = 0; i < res.rows.length; i++)
            console.log(res.rows.item(i).name);
        },
        function(err) {
          //$scope.showAlert("failed2?", err);
          console.log("sql2: " + err);

        });
  }




})

任何帮助将不胜感激。谢谢你。

【问题讨论】:

    标签: android sqlite cordova plugins ionic-framework


    【解决方案1】:

    发现问题。在尝试让它运行时,我下载了许多 sqlite 插件(例如 sqlite2 和 sqlite-ext)。其中之一与基本的 sqlite 插件有某种冲突。我只是删除了所有 sqlite 插件,只安装了

    https://github.com/litehelpers/Cordova-sqlite-storage

    现在它正在工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-02
      • 2013-03-11
      • 1970-01-01
      • 2021-12-13
      • 2015-11-24
      • 2016-10-24
      • 1970-01-01
      • 2011-04-15
      相关资源
      最近更新 更多