交互与Android的外部SQLite数据库。 [英] Interacting with external SQLite databases from Android.

查看:252
本文介绍了交互与Android的外部SQLite数据库。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与Web服务一起使用(过我有没有控制)返回SQLite数据库,当你查询。有没有办法做到这一点?

I'm trying to work with a web service (that I have no control over) that returns a SQLite database when you query it. Is there any way to do this?

推荐答案

我工作的一个项目,在做类似的事情。我请求更新数据库,并根据更新所需的量,或者发送下一个全新的数据库或一系列的SQL语句。我强烈建议保持数据库上的SD卡(如果可用),特别是如果你下载了数据库的大小是可变的,不仅仅是几千字节大小。

I am working on a project that's doing something similar. I request updates to a database and depending on the volume of updates required, it either sends down an entirely new database or a series of SQL statements. I would strongly suggest keeping the databases on the sdcard if it is available, especially if the size of the databases you're downloading are variable and beyond just a few kilobytes in size.

我做以下操作来获取文件(修改味,因为你可能有XML处理的SOAP响应,也忽视了进步的东西。)

I'm doing the following to get the file (modify to taste since you may have XML to deal with in the SOAP response, also ignore the progress stuff.)

fileStream = new FileOutputStream(new File(I-build-my-filename-stuff-above-here));

/* fileData is a byte[8192] */
while((i = httpResponse.read(fileData)) > -1) {
    fileStream.write(fileData, 0, i);

    position += i;

    if (item.getUpdateType() == UpdateItem.FULL_FILE) {
        progress = position / item.getSize();
    } else {
        progress = position / (item.getSize() * 2);
    }

    item.setProgress(progress);
} // end loop through getting http response stream

fileStream.close();
httpResponse.close();

然后我执行以下操作来访问数据库。由于这些数据库也可在iPhone上的应用程序,他们没有一个Android特定表,因此 SQLiteDatabase.NO_LOCALIZED_COLLATORS 标志需要访问数据库。请注意,我的数据库类扩展SQLiteOpenHelper类:

Then I do the following to access the database. Since these databases are also used on an iPhone app, they do not have an Android-specific table and therefore the SQLiteDatabase.NO_LOCALIZED_COLLATORS flag is needed to access the DB. Note that my Database class extends the SQLiteOpenHelper class:

public Database(Context context, String fileName, String title) {
    super(context, fileName, null, 1);
    this.context = context;
    this.title = title;
    this.fileName = fileName;

    if (fileName != null && fileName.contains("/sdcard")) {
        db = SQLiteDatabase.openDatabase(fileName, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);            
    } else {
        db = getWritableDatabase();
    }

}

这篇关于交互与Android的外部SQLite数据库。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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