在cordova中使用现有的sqlite数据库 [英] Use existing sqlite database in cordova

查看:678
本文介绍了在cordova中使用现有的sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在创建一个离子框架和cordova应用程序,并希望使用一个sqlite数据库来存储数据。

Hello I'm creating a application with ionic framework and cordova and want to use a sqlite database to store the data.

那么如何使用现有的sqlite数据库为此目的?
我在哪里必须保存数据库,以便cordova可以找到它,并使用它,如果我编译一个平台?

So how can I use a existing sqlite database for this purpose? Where do I have to save the database so that cordova can find it and uses it if I compile for a platform?

如果我尝试以推荐的方式打开数据库,我得到以下错误:

I get the following error if I try to open the db in the recommend way:

[Error] TypeError: undefined is not an object (evaluating 'n.sqlitePlugin.openDatabase')
openDB (ng-cordova.min.js, line 9)
(anonyme Funktion) (app.js, line 19)
(anonyme Funktion) (ionic.bundle.js, line 44243)
onPlatformReady (ionic.bundle.js, line 2396)
onWindowLoad (ionic.bundle.js, line 2375)

这是我的index.html的头部:

Here is the head section of my index.html:

<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>

<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>


<!-- cordova sqlite extenseion -->
<script src="js/ng-cordova.min.js"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>


<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
</head>

我的app.js如下所示:

My app.js looks like this:

//Database variable
var db = null;

angular.module('starter', ['ionic', 'starter.controllers', 'ngCordova'])

.run(function($ionicPlatform, $cordovaSQLite) {
  $ionicPlatform.ready(function() {

    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }

    db = $cordovaSQLite.openDB({ name: "db.spareParts" });

    var query = "SELECT cars.name FROM cars";
    $cordovaSQLite.execute(db, query).then(function(res) {
        if(res.rows.length > 0) {
            console.log("SELECTED -> " + res.rows.item(0).name);
        } else {
            console.log("No results found");
        }
    }, function (err) {
        console.error(err);
    });


  });
})

数据库db.spareParts存储在根www目录中, sqlite3数据库。

The database db.spareParts is stored in the root www directory and is a sqlite3 database.

推荐答案

那么如何使用现有的sqlite数据库来实现这个目的? >
您可以通过使用内置的WebSQL来使用现有的sqlite数据库。您不要需要安装任何插件。有关详情,请参阅此处的链接。

请注意,使用标准方法的存储量有限制(我认为大约为5MB)。

如果要存储超过允许的标准存储量你需要使用sqlite插件。链接此处了解详情。

So how can I use a existing sqlite database for this purpose?
You can use the existing sqlite database by using the built-in WebSQL. You do not need to install any plugin. Please see the link here for more details.
Note that there is limitation to the amount of storage using the standard approach.(I think it is about 5MB).
If you want to store more than the allowed standard storage than you need to use the sqlite plugin. Link here for details.

我需要保存数据库,以便cordova可以找到它,并在编译平台时使用它?

您不需要指定存储数据库的位置路径。数据库打开函数调用将在方便的位置自动创建和存储

编辑1:

需要为您的应用程序使用预填充的 sqlite3数据库。

将数据库复制到www目录将工作。您需要将预填充的数据库复制到应用程序将为应用程序使用的默认位置。不同平台(iOS,Android,Windows)的位置不同。在平台内,db存储的位置路径也不同。例如Android Jelly bean的位置路径不同于Android KitKat中的位置路径。因此据我所知,你不会在网上找到一个完整的解决方案,将数据库复制到应用程序的默认位置。

这是一个完全不同的命题。

您需要在stackoverflow中搜索使用cordova预填充的sqlite 以获取有用的答案。

我已回答的是从头开始在应用程序中创建新的数据库。

也与您的错误有关。

您必须检查 deviceready 事件是否已触发,然后再致电任何 cordova / phonegap 相关代码。

What I understand from your edited question is that you require a pre-populated sqlite3 db to be used for your application.
Copying your db to the www directory will not work. You need to copy the pre-populated db to the default location where the app will use it for your application. The location is different for different platforms (iOS, Android, Windows). Also within a platform the location path for the db storage is different. E.g for Android Jelly bean the location path is different to the one in Android KitKat. Hence as far as I know you will not a find a complete solution on the Web for copying the db to the default location of the app.
This is a completely a different proposition.
You need to search stackoverflow for pre populated sqlite with cordova to get useful answers.
What I have answered is to create a new db in your application from scratch.
Also relating to your error.
You need to check that the deviceready event is fired before calling any cordova/phonegap related code.

这篇关于在cordova中使用现有的sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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