Ios 带有Antair Cordova SQLitePlugin的Ionic预填充数据库[帮助请求]
____简介 大家好,首先有三点澄清:Ios 带有Antair Cordova SQLitePlugin的Ionic预填充数据库[帮助请求],ios,sqlite,cordova,ionic,Ios,Sqlite,Cordova,Ionic,____简介 大家好,首先有三点澄清: 我的英语不好,所以请您原谅我的错误 我是新手,请原谅我的错误 我以前在互联网上搜索并尝试过我找到的解决方案,但仍然无法解决嵌入预填充数据库的问题 ____目标 我想为iOS和Android开发一个应用程序,其中包含一个预填充的数据库 例如,数据库由15000条记录组成,每条记录由三个键值对(id、firstname和lastname)组成 ___我所做的 步骤: ionic start myapp blank cd myapp ionic platform
ionic start myapp blank
cd myapp
ionic platform add ios
ionic platform add android
然后我创建了一个用于测试的sqlite数据库,名为mydb.sqlite,由一个包含两个id、firstname和lastname记录的表people组成
我决定使用以下插件:
这是因为它可以用cordova工具安装
ionic plugin add https://github.com/Antair/Cordova-SQLitePlugin
(提醒:我认为网站上的说明显示了一个不正确的引用——“cordova插件添加”-指的是另一个插件)
然后,按照插件网站上的说明,我将数据库复制到myapp/www/db/以便现在可以在myapp/www/db/mydb.sqlite上找到它
我在默认app.js脚本之后修改了index.html,包括SQLite插件:
___问题
我不知道如何在控制器部分继续查询所有记录(只是一个查询示例)并在console.log中显示结果
我认为以下代码必须以某种方式完成:
angular.module('starter', ['ionic' /* What goes here? */ ])
此外,还必须完成控制器内的代码部分:
$scope.all = function(){
var query = "SELECT * FROM people";
// I don't know how to proceed
}; // $scope.all
___最后感谢
提前感谢您对我的帮助。所以这家伙的代码对封装我的DAL有很大帮助。我强烈建议你一字不差地使用他的代码 您将看到他有以下方法:
self.query = function(query, bindings) {
bindings = typeof bindings !== 'undefined' ? bindings : [];
var deferred = $q.defer();
self.db.transaction(function(transaction) {
transaction.executeSql(query, bindings, function(transaction, result) {
deferred.resolve(result);
}, function(transaction, error) {
deferred.reject(error);
});
});
return deferred.promise;
};
让我们把它分解一下。query函数获取查询字符串(query参数)和可能的绑定列表?在类似“从ID=?”的_表中选择*”的查询中。因为他的代码是一个服务,所以对于将来的所有调用,self值都指向服务本身。该函数将对db执行一个事务,但它返回一个承诺,该承诺仅在db返回后才得到满足
他的服务提供了第二个助手函数:fetchAll
self.fetchAll = function(result) {
var output = [];
for (var i = 0; i < result.rows.length; i++) {
output.push(result.rows.item(i));
}
return output;
};
这个例子有点嘈杂,因为它有一些“线程化”代码来确保如果相同的承诺被触发两次,它只对DB运行一次。一般的要点是显示DB.query返回一个承诺。查询方法后面的“then”使用DB服务获取所有数据并将其添加到本地内存空间。所有这些都由返回变量d.promise的self.findlocation进行协调
你的代表也会这样。控制器可以通过AngularJS将您的DAL服务(如my LocationService)注入其中。如果您使用的是AngularJS UI,您可以让它解析数据并将其传递到列表中
最后,我对guy代码的唯一问题是db应该来自此代码
var dbMaker = ($window.sqlitePlugin || $window);
原因是该插件在ApacheRipple中不起作用。由于该插件能够很好地镜像浏览器的Web SQL界面,因此这个简单的小改动将使Ripple能够运行您的Ionic应用程序,同时仍然允许您在真实设备中使用SQLite
我希望这有帮助
self.query = function(query, bindings) {
bindings = typeof bindings !== 'undefined' ? bindings : [];
var deferred = $q.defer();
self.db.transaction(function(transaction) {
transaction.executeSql(query, bindings, function(transaction, result) {
deferred.resolve(result);
}, function(transaction, error) {
deferred.reject(error);
});
});
return deferred.promise;
};
self.fetchAll = function(result) {
var output = [];
for (var i = 0; i < result.rows.length; i++) {
output.push(result.rows.item(i));
}
return output;
};
.service('LocationService', function ($q, DB, Util) {
'use strict';
var self = this;
self.locations = [];
self.loadLocked = false;
self.pending = [];
self.findLocations = function () {
var d = $q.defer();
if (self.locations.length > 0) {
d.resolve(self.locations);
}
else if (self.locations.length === 0 && !self.loadLocked) {
self.loadLocked = true;
DB.query("SELECT * FROM locations WHERE kind = 'active'")
.then(function (resultSet) {
var locations = DB.fetchAll(resultSet);
self.locations.
push.apply(self.locations, locations);
self.loadLocked = false;
d.resolve(self.locations);
self.pending.forEach(function (d) {
d.resolve(self.locations);
});
}, Util.handleError);
} else {
self.pending.push(d);
}
return d.promise;
};
})
var dbMaker = ($window.sqlitePlugin || $window);