var db; //global variable
$(document).ready(function () {
// This is the recommended code for a Web page to download
// an sqlite3 database:
var xhr = new XMLHttpRequest();
xhr.open('GET', '/PathTo/Database.db', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function (e) {
var uInt8Array = new Uint8Array(this.response);
db = new SQL.Database(uInt8Array);
// My code here
fNowUseDownloadedDatabase();
};
xhr.send();
}
function fNowUseDownloadedDatabase() {
// Remember, db is a global variable.
var a = db.exec("SELECT 'name', 'sql' FROM 'sqlite_master' WHERE type='table';");
var iLen = a.length;
var i = 0;
for (i = 1; i < iLen; i++) {
console.log(a[i]);
}
}
数据库没有3个表,而不是3个表。我知道数据库的路径正确,否则会收到404错误。另外,如果我将db传递到fNowUseDownloadedDatabase函数中,则它是一个空数据库。但是在服务器上它有数据。不知何故,它正在与数据一起下载,但随后被空的新数据库取代。为什么不呢?