我的头撞到了这堵墙一段时间,所以希望蜂巢的头脑可以帮助我。
我正在尝试从远程位置下载并附加sqlite数据库('database.db'),虽然我可以正常下载,但我很难使用window.sqlitePlugin.openDatabase连接到它({name:databaseName });
任何想法或有没有人能够在phonegapbuild中下载并附加sqlite数据库?
var _urlContactsSqlite = 'https://www.dropbox.com/s/370lyi2w21pxgrs/database.db?dl=1';
function init() {
var databaseName = 'database.db';
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function requestFileSystemSuccess(fs) { //fs = filesystem
console.log('requestFileSystemSuccess');
fs.root.getDirectory('databases', { create: true }, //create the directory if it doesnt exist
function getDirectorySuccess(dataEntry) {
console.log('getDirectorySuccess');
fs.root.getFile('dummy.html', {create: true, exclusive: false},
function getFileSuccess(fileEntry) {
console.log('getFileSuccess');
var sPath = fileEntry.nativeURL.replace('dummy.html','') + 'databases/';
console.log('sPath: ' + sPath);
var ft = new FileTransfer();
ft.download(
encodeURI(_urlContactsSqlite),
sPath + databaseName,
function downloadSuccess(entry) {
console.log('downloadSuccess - what now smarty pants');
console.log('now we have the database in a location, we should try and open it.');
$rootScope._db = window.sqlitePlugin.openDatabase({
name : databaseName
});
console.log('attempting to get a count of the contacts table');
var promise = dbQuery.contacts_Count();
promise
.then(function(data) {
console.log(data);
alert(data);
})
.catch(function(error) {
console.log('error occurred inside promise : ' + error );
});
},
function downloadFail(e) {
console.log('downloadFail', e);
console.log('error source ' + e.source);
console.log('error target ' + e.target);
console.log('error code ' + e.code);
},
false, null); //end fileTransfer
},
function getFileFail(e) {
console.log('getFileFail',e);
}
);
},
function getDirectoryFail(e)
{
console.log('getDirectoryFail',e);
}
); //end requestFileSystem
},
function requestFileSystemFail(){
console.log('requestFileSystemFail');
}
); //end requestFileSystem
} // end init()
dbQuery.contacts_Count();
function contacts_Count() {
var deferred = $q.defer();
setTimeout(function() {
$rootScope._db.transaction(function(tx) {
var sql = 'SELECT COUNT(*) AS COUNT FROM contacts;';
console.log(sql);
tx.executeSql(sql, [], function(tx, results) {
var rowCount = results.rows.item(0).COUNT;
console.log('rowcount: ' + rowCount);
deferred.resolve(rowCount);
}, null);
});
},1);
return deferred.promise;
}
答案 0 :(得分:0)
我猜这是因为SqlitePlugin正在寻找您的数据库。在您的代码中,您将其下载到Databases目录,而插件只在www子目录中查找。根据预填充数据库的documentation:
适用于Android& iOS(仅限):将数据库文件放在www目录中并打开数据库,如:
var db = window.sqlitePlugin.openDatabase({name: "my.db", createFromLocation: 1});
----编辑如下:
考虑在_db.transaction
函数中添加一些错误检查,如下所示:
$rootScope._db.transaction(function(tx) {
var sql = 'SELECT COUNT(*) AS COUNT FROM contacts;';
console.log(sql);
tx.executeSql(sql, [], function(tx, results) {
var rowCount = results.rows.item(0).COUNT;
console.log('rowcount: ' + rowCount);
deferred.resolve(rowCount);
}, null);
},
function(error) {
console.log('db error: ' + error.message);
});