准备要远程加载的 SQLite 数据库以进行移动开发

发布于 2024-11-26 09:25:12 字数 284 浏览 2 评论 0原文

我的移动 Titan 应用程序正在从远程 URL 加载我生成的 xml 文件形式的数据。这个想法是应用程序正在下载该 xml 并将其处理到 SQLlite 数据库中。但我在想,我也可以用包含所有数据的现成 sqlite 文件替换服务器端生成的 xml。这样,我不需要在客户端做任何处理,这节省了用户的一些时间。

这是个好主意吗?如果是,我将如何“伪造”生成 sqlite 文件?我没有需要传递的标头,所以这不是问题,但当我用 coda 打开 .sqlite 文件时,我注意到有一些奇怪的字符,这意味着编码必须不同。

谢谢!

My mobile titanium app is loading data from a remote url, in the form of a xml file, that I have generated. The idea is that the app is downloading that xml and processes it into a SQLlite db. But I was thinking, I could also replace the server side generated xml with a ready sqlite file with all the data in it. That way, I don't need to do any processing client side, which saves some of the user's time.

Is this a good idea? If yes, how would I "fake" generate a sqlite file? There are no headers that I need to pass on, so that's not the issue, but what I noticed when I opened an .sqlite file with coda, that there are weird characters meaning the encoding must be different.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫雪独思 2024-12-03 09:25:12

据我所知,在任何浏览器中都没有任何机制允许您传输 sqlite 数据库的二进制图像(即使假设字节排序和字符编码不是问题)。

当然,以比原子 XML 更接近一组 CREATE 和 INSERT 语句的格式维护/传输数据可能会提高性能。

There is no mechanism in any browsers I am aware of which would allow you to transfer a binary image of a sqlite database (even assuming byte ordering and character coding were not an issue).

Certainly, it may improve performance to maintain / transfer the data in a format closer to a set of CREATE and INSERT statements than atomic XML.

芯好空 2024-12-03 09:25:12

您可以在许多程序中创建 SQLite 数据库 - 我使用 MesaSQLLite。它只是一个二进制文件。以下是在 iOS 上下载和安装的示例。我还没有在安卓上测试过。

var win = Ti.UI.createWindow({
    backgroundColor: '#eee'
});

var button1 = Ti.UI.createButton({
    width: '60%',
    height: 30,
    top: 5,
    title: '1) Get Database'
});

var button2 = Ti.UI.createButton({
    width: '60%',
    height: 30,
    top: 40,
    title: '2) Install Database'
});

var button3 = Ti.UI.createButton({
    width: '60%',
    height: 30,
    top: 75,
    title: '3) Query Database'
});

var msg = Ti.UI.createLabel({
    top: 110,
    width: '80%',
    height: 120,
    text: 'click the buttons in order',
    font: {
        fontFamily: 'Courier',
        fontSize: 12
    },
    textAlign: 'center'
});

win.add(button1);
win.add(button2);
win.add(button3);
win.add(msg);

function localFile() {
    var localDbFile = 'testdb.db';
    return Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, localDbFile);
}

button1.addEventListener('click',
function() {
    msg.text = "Getting Database from Server";
    var xhr = Ti.Network.createHTTPClient();
    xhr.open("GET", "http://www.titaniumdevelopment.com.au/testdb.db");
    xhr.onload = function() {
        if (xhr.status == 200) {
            msg.text = "saving database";
            var file = localFile();
            file.write(this.responseData);
            msg.text = "database saved as " + file.nativePath;
        } else {
            msg.text = "Unable to get database. Response code was " + xhr.status;
        }
    };
    xhr.onerror = function() {
        msg.text = "Error fetching database";
    };
    xhr.send();
});

button2.addEventListener('click',
function() {
    var file = localFile();
    if (file.exists()) {
        msg.text = "installing database";
        Ti.Database.install(file.nativePath, 'test');
        msg.text = "database installed";
    } else {
        msg.text = "unable to find database";
    }
});

button3.addEventListener('click',
function() {
    try {
        var db = Titanium.Database.open('test');
        var rows = db.execute('SELECT * FROM TIPS');
        msg.text = 'ROW COUNT = ' + rows.getRowCount();
        rows.close();
        db.close();
    } catch(e){
        msg.text = "unable to open database";
    }
});

win.open();

注意:我不确定应用商店是否会批准这一点,因为他们不太热衷于远程获取数据。

You can create a SQLite database in many programs - I use MesaSQLLite. It is just a binary file. Here is an example of downloading and installing on iOS. I have not tested it on Android.

var win = Ti.UI.createWindow({
    backgroundColor: '#eee'
});

var button1 = Ti.UI.createButton({
    width: '60%',
    height: 30,
    top: 5,
    title: '1) Get Database'
});

var button2 = Ti.UI.createButton({
    width: '60%',
    height: 30,
    top: 40,
    title: '2) Install Database'
});

var button3 = Ti.UI.createButton({
    width: '60%',
    height: 30,
    top: 75,
    title: '3) Query Database'
});

var msg = Ti.UI.createLabel({
    top: 110,
    width: '80%',
    height: 120,
    text: 'click the buttons in order',
    font: {
        fontFamily: 'Courier',
        fontSize: 12
    },
    textAlign: 'center'
});

win.add(button1);
win.add(button2);
win.add(button3);
win.add(msg);

function localFile() {
    var localDbFile = 'testdb.db';
    return Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, localDbFile);
}

button1.addEventListener('click',
function() {
    msg.text = "Getting Database from Server";
    var xhr = Ti.Network.createHTTPClient();
    xhr.open("GET", "http://www.titaniumdevelopment.com.au/testdb.db");
    xhr.onload = function() {
        if (xhr.status == 200) {
            msg.text = "saving database";
            var file = localFile();
            file.write(this.responseData);
            msg.text = "database saved as " + file.nativePath;
        } else {
            msg.text = "Unable to get database. Response code was " + xhr.status;
        }
    };
    xhr.onerror = function() {
        msg.text = "Error fetching database";
    };
    xhr.send();
});

button2.addEventListener('click',
function() {
    var file = localFile();
    if (file.exists()) {
        msg.text = "installing database";
        Ti.Database.install(file.nativePath, 'test');
        msg.text = "database installed";
    } else {
        msg.text = "unable to find database";
    }
});

button3.addEventListener('click',
function() {
    try {
        var db = Titanium.Database.open('test');
        var rows = db.execute('SELECT * FROM TIPS');
        msg.text = 'ROW COUNT = ' + rows.getRowCount();
        rows.close();
        db.close();
    } catch(e){
        msg.text = "unable to open database";
    }
});

win.open();

Note: I'm not sure the App Store would approve this as they are not too keen on fetching data remotely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文