【问题标题】:Updating an SQLite database using the Google Drive Android API使用 Google Drive Android API 更新 SQLite 数据库
【发布时间】:2017-01-03 00:18:30
【问题描述】:

使用Google Drive API for Android 和 Stack Overflow 上的一些答案,我设法在我的应用中实现了 Google 登录,并将存储在用户设备上的 SQLite 数据库备份到 Google Drive。

作为参考,这是我将数据库保存到 Google Drive 的方法(这是在名为 DriveDbHandler 的 final class 中完成的):

private static final String LOG_TAG = "DriveDbHandler";

private static final String PACKAGE_NAME = "com.package.example";

private static final String DATABASE_PATH =
        "/data/data/" + PACKAGE_NAME + "/databases/" + DbHelper.DATABASE_NAME;

private static final String FILE_NAME = DbHelper.DATABASE_NAME;
private static final String MIME_TYPE = "application/x-sqlite-3";

private DriveDbHandler() {
}


public static void tryCreatingDbOnDrive(final GoogleApiClient googleApiClient) {
    // We need to check if the database already exists on Google Drive. If so, we won't create
    // it again.

    Query query = new Query.Builder()
            .addFilter(Filters.and(
                    Filters.eq(SearchableField.TITLE, FILE_NAME),
                    Filters.eq(SearchableField.MIME_TYPE, MIME_TYPE)))
            .build();
    DriveFolder appFolder = Drive.DriveApi.getAppFolder(googleApiClient);

    appFolder.queryChildren(googleApiClient, query).setResultCallback(
            new ResultCallback<DriveApi.MetadataBufferResult>() {
                @Override
                public void onResult(@NonNull DriveApi.MetadataBufferResult metadataBufferResult) {
                    if (!metadataBufferResult.getStatus().isSuccess()) {
                        Log.e(LOG_TAG, "Query for " + FILE_NAME + " unsuccessful!");
                        return;
                    }

                    int count = metadataBufferResult.getMetadataBuffer().getCount();

                    Log.d(LOG_TAG, "Successfully ran query for " + FILE_NAME + " and found " +
                            count + " results");

                    if (count > 1) {
                        Log.e(LOG_TAG, "App folder contains more than one database file! " +
                                "Found " + count + " matching results.");
                        return;
                    }

                    // Create the database on Google Drive if it doesn't exist already
                    if (count == 0) {
                        Log.d(LOG_TAG, "No existing database found on Google Drive");
                        saveToDrive(googleApiClient);
                    }
                }
            });
}

private static void saveToDrive(final GoogleApiClient googleApiClient) {
    Log.d(LOG_TAG, "Starting to save to drive...");

    // Create content from file
    Drive.DriveApi.newDriveContents(googleApiClient).setResultCallback(
            new ResultCallback<DriveApi.DriveContentsResult>() {
                @Override
                public void onResult(@NonNull DriveApi.DriveContentsResult driveContentsResult) {
                    if (!driveContentsResult.getStatus().isSuccess()) {
                        Log.w(LOG_TAG, "Drive contents result not a success! " +
                                "Not saving data to drive.");
                        return;
                    }

                    Log.d(LOG_TAG, "Created drive contents for file");
                    createNewFile(googleApiClient, driveContentsResult.getDriveContents());
                }
            });
}

private static void createNewFile(GoogleApiClient googleApiClient, DriveContents driveContents) {
    // Write file to contents (see http://stackoverflow.com/a/33610727/4230345)
    File file = new File(DATABASE_PATH);
    OutputStream outputStream = driveContents.getOutputStream();
    try {
        InputStream inputStream = new FileInputStream(file);
        byte[] buf = new byte[4096];
        int c;
        while ((c = inputStream.read(buf, 0, buf.length)) > 0) {
            outputStream.write(buf, 0, c);
            outputStream.flush();
        }
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.d(LOG_TAG, "Written file to output stream of drive contents");

    // Create metadata
    MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
            .setTitle(FILE_NAME)
            .setMimeType(MIME_TYPE)
            .build();

    // Create the file on Google Drive
    DriveFolder folder = Drive.DriveApi.getAppFolder(googleApiClient);
    folder.createFile(googleApiClient, metadataChangeSet, driveContents).setResultCallback(
            new ResultCallback<DriveFolder.DriveFileResult>() {
        @Override
        public void onResult(@NonNull DriveFolder.DriveFileResult driveFileResult) {
            if (!driveFileResult.getStatus().isSuccess()) {
                Log.w(LOG_TAG, "File did not get created in Google Drive!");
                return;
            }

            Log.i(LOG_TAG, "Successfully created file in Google Drive");
        }
    });
}

这是我的问题:

我可以将数据库保存到 Google Drive,但如何使用本地所做的任何更改更新 Google Drive 版本?

例如,我可以从表 A 中删除 3 行,然后在本地向表 B 中添加 5 行(添加到设备的 SQLite 数据库),但是如何使用此更改更新 Google Drive 版本?

我曾考虑删除整个云端硬盘文件并重新上传,但据我了解,这会导致该文件的 DriveId 不同。

我想知道我是否能够利用 API 的修改处理(解释为 here),如果设备没有互联网连接,更改会排队等待上传。

【问题讨论】:

  • 当您将数据库用作文件时,我认为没有任何简单/优雅的解决方案来解决此问题
  • 有一个tool 虽然可以计算2个sqlite数据库之间的差异
  • @Distjubo 是的,这就是我的怀疑。你有我可以使用的解决方案(不管它有多优雅)。我将如何使用您建议的 sqldiff 工具?
  • sqldiff 仅在您将该在线文件作为数据库存在时才能工作,您可以在其中插入/更新内容。但既然你不这样做,你必须在最后一次将文件发送到 gdrive 时复制文件,计算差异并patch它
  • @Distjubo 澄清一下,我使用的是 Android API 而不是 REST API。我将如何修改数据库的 Google Drive 版本(使用 Android API)?

标签: android android-sqlite google-drive-android-api


【解决方案1】:

根据this answer,Google Drive 的 android API 已经为您处理了差异计算。所以不需要做任何复杂的事情,只需像完全重写该文件一样使用 API。

您可能还想利用 API 的透明离线同步功能。

【讨论】:

  • 感谢您的澄清 - 我很高兴这里没有什么太棘手的事情;)。重写文件时,我是否需要做任何事情来从该文件中删除内容,或者我只是写入它并且默认情况下会覆盖?
  • 更新 - 我是否应该按照 here 和 here 的概述以 WRITE_ONLY 模式打开文件?。
  • 感谢您的帮助 - 我会试一试,然后告诉您我的进展情况。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多