简体   繁体   English

与Android的外部SQLite数据库交互。

[英]Interacting with external SQLite databases from Android.

I'm trying to work with a web service (that I have no control over) that returns a SQLite database when you query it. 我正在尝试使用一个Web服务(我无法控制),它在您查询时返回一个SQLite数据库。 Is there any way to do this? 有没有办法做到这一点?

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. 我请求更新数据库,并根据所需的更新量,发送一个全新的数据库或一系列SQL语句。 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. 我强烈建议将数据库保留在SD卡上(如果可用),特别是如果您下载的数据库大小可变且超过几千字节。

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.) 我正在执行以下操作来获取文件(因为您可能需要在SOAP响应中处理XML,所以也要修改它们,也忽略了进度。)

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();

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. 由于这些数据库也用于iPhone应用程序,因此它们没有特定于Android的表,因此访问数据库需要SQLiteDatabase.NO_LOCALIZED_COLLATORS标志。 Note that my Database class extends the SQLiteOpenHelper class: 请注意,我的Database类扩展了SQLiteOpenHelper类:

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();
    }

}

If you have no option to the format of the result, then a solution for you is to save the database file into the database directory of your app in /data/data/package_name/databases, and after each request load the database file and query for the results. 如果您无法选择结果的格式,那么您的解决方案是将数据库文件保存到/ data / data / package_name / databases中的应用程序的数据库目录中,并在每次请求后加载数据库文件和查询结果。

Here are a couple of pages on databases. 这是关于数据库的几页。

  1. http://developer.android.com/guide/topics/data/data-storage.html http://developer.android.com/guide/topics/data/data-storage.html
  2. http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM