当前位置: 首页 > 知识库问答 >
问题:

驱动API-下载/上载sql数据库

梁季
2023-03-14

我现在正在下载我用驱动器API上传的sql数据库。有一个备份是肯定的,但当我尝试打开文件时,我得到一个文件无法打开的错误。

编辑:我解决了上面的问题。现在我想最后上传和下载sql数据库。

我的代码:

public static void restoreDriveBackup() {
    Query query = new Query.Builder()
            .addFilter(Filters.eq(SearchableField.TITLE, GOOGLE_DRIVE_FILE_NAME))
            .build();

    Drive.DriveApi.query(api, query).setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
        @Override
        public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
            int count = metadataBufferResult.getMetadataBuffer().getCount() - 1;
            debug("Count: " + count);
            driveId = metadataBufferResult.getMetadataBuffer().get(count).getDriveId();
            debug("driveId: " + driveId);
            metadataBufferResult.getMetadataBuffer().release();

            mfile = Drive.DriveApi.getFile(api, driveId);

            mfile.open(api, DriveFile.MODE_READ_ONLY, new DriveFile.DownloadProgressListener() {
                @Override
                public void onProgress(long l, long l1) {
                    debug("Downloading... ("+l+")");
                }
            })
                    .setResultCallback(restoreContentsCallback);
        }
    });
}

public static void doDriveBackup() { Drive.DriveApi.newContents(api).setResultCallback(backupContentsCallback); }

static final private ResultCallback<DriveApi.DriveContentsResult> restoreContentsCallback =
        new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(DriveApi.DriveContentsResult result) {
                if (!result.getStatus().isSuccess()) {
                    mToast("File cant be opened");
                    return;
                }

                utilsM.dbClose();

                DriveContents contents = result.getDriveContents();
                File db_file;

                InputStreamReader is = new InputStreamReader(contents.getInputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream());

                FileOutputStream fos; 
                String line;

                db_file = getDbPath();

                if (db_file != null) {
                    db_file.delete();
                }

                try {
                    fos = new FileOutputStream(act.getDatabasePath(DATABASE_NAME));

                    while ((line = reader.readLine()) != null) { fos.write(line.getBytes()); }
                    fos.flush();
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                utilsM.dbOpen();
                mRecyclerView.invalidate();
                mAdapter.notifyDataSetChanged();
                contents.discard(api);

            }
        };




static final private ResultCallback<DriveApi.DriveContentsResult> backupContentsCallback = new ResultCallback<DriveApi.DriveContentsResult>() {

    @Override
    public void onResult(DriveApi.DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            mToast("Error while trying to create new file contents");
            return;
        }

        String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType("db");
        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                .setTitle(GOOGLE_DRIVE_FILE_NAME) // Google Drive File name
                .setMimeType(mimeType)
                .setStarred(true).build();
        // create a file on root folder
        Drive.DriveApi.getRootFolder(api)
                .createFile(api, changeSet, result.getDriveContents())
                .setResultCallback(backupFileCallback);
    }
};
static final private ResultCallback<DriveFolder.DriveFileResult> backupFileCallback = new ResultCallback<DriveFolder.DriveFileResult>() {
    @Override
    public void onResult(DriveFolder.DriveFileResult result) {
        if (!result.getStatus().isSuccess()) {
            mToast("Error while trying to create the file");
            return;
        }
        mfile = result.getDriveFile();
        mfile.open(api, DriveFile.MODE_WRITE_ONLY, null).setResultCallback(backupContentsOpenedCallback);
    }
};
static final private ResultCallback<DriveApi.DriveContentsResult> backupContentsOpenedCallback = new ResultCallback<DriveApi.DriveContentsResult>() {
    @Override
    public void onResult(DriveApi.DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            mToast("Error opening file");
            return;
        }
        try {
            mToast("Uploading db...");

            DriveContents content = result.getDriveContents();
            FileInputStream is = new FileInputStream(act.getDatabasePath(DATABASE_NAME));

            BufferedInputStream in = new BufferedInputStream(is);
            BufferedOutputStream out = new BufferedOutputStream(content.getOutputStream());

            byte[] buffer = new byte[4 * 1024];
            int n = 0;
            int off = 0;

            while( ( n = in.read(buffer) ) > 0) {
                out.write(buffer, 0, n);
                off += n;
                debug("upload_buffer: " + buffer[0]);
                debug("upload_n: " + n);
            }

            debug("writen len: "+off);

            in.close();
            is.close();
            out.flush();
            out.close();
            content.commit(api, null).setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    mToast("Upload successfull!");
                }
            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
};

共有1个答案

关学
2023-03-14

找到了解决办法,忘了提交我的文件。这是我的代码:

public static void doDriveBackup () {
    Drive.DriveApi.newDriveContents(api).setResultCallback(backupContentsCallback);
}

public static void restoreDriveBackup() {
    Query query = new Query.Builder()
            .addFilter(Filters.eq(SearchableField.TITLE, GOOGLE_DRIVE_FILE_NAME))
            .build();

    Drive.DriveApi.query(api, query).setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
        @Override
        public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {

            /*for(int i = 0 ;i < metadataBufferResult.getMetadataBuffer().getCount() ;i++) {
                debug("got index "+i);
                debug("filesize in cloud "+ metadataBufferResult.getMetadataBuffer().get(i).getFileSize());
                debug("driveId(1): "+ metadataBufferResult.getMetadataBuffer().get(i).getDriveId());
            }*/


            int count = metadataBufferResult.getMetadataBuffer().getCount() - 1;
            //debug("Count: " + count);
            driveId = metadataBufferResult.getMetadataBuffer().get(0).getDriveId();
            //debug("driveId: " + driveId);
            debug("filesize in cloud " + metadataBufferResult.getMetadataBuffer().get(0).getFileSize());
            metadataBufferResult.getMetadataBuffer().release();


            mfile = Drive.DriveApi.getFile(api, driveId);

            mfile.open(api, DriveFile.MODE_READ_ONLY, new DriveFile.DownloadProgressListener() {
                @Override
                public void onProgress(long bytesDown, long bytesExpected) {
                    //mToast("Downloading... (" + bytesDown + "/" + bytesExpected + ")");;
                }
            })
                    .setResultCallback(restoreContentsCallback);
        }
    });
}

static final private ResultCallback<DriveApi.DriveContentsResult> restoreContentsCallback =
        new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(DriveApi.DriveContentsResult result) {
                if (!result.getStatus().isSuccess()) {
                    mToast("Unable to open file, try again.");
                    return;
                }

                utilsM.dbClose();

                db_file = getDbPath();
                String path = db_file.getPath();

                if ( !db_file.exists())
                    db_file.delete();

                db_file = new File(path);

                DriveContents contents = result.getDriveContents();
                //debug("driveId:(2)" + contents.getDriveId());

                try {
                    FileOutputStream fos = new FileOutputStream(db_file);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    BufferedInputStream in = new BufferedInputStream(contents.getInputStream());

                    byte[] buffer = new byte[1024];
                    int n, cnt = 0;


                    //debug("before read " + in.available());

                    while( ( n = in.read(buffer) ) > 0) {
                        bos.write(buffer, 0, n);
                        cnt += n;
                        debug("buffer: " + buffer[0]);
                        debug("buffer: " + buffer[1]);
                        debug("buffer: " + buffer[2]);
                        debug("buffer: " + buffer[3]);
                        bos.flush();
                    }

                    //debug(" read done: " + cnt);

                    bos.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                mToast(act.getResources().getString(R.string.restoreComplete));
                DialogFragment_Sync.dismissDialog();

                utilsM.dbOpen();
                mRecyclerView.invalidate();
                mAdapter.notifyDataSetChanged();
                contents.discard(api);

            }
        };






static final private ResultCallback<DriveApi.DriveContentsResult> backupContentsCallback = new ResultCallback<DriveApi.DriveContentsResult>() {

    @Override
    public void onResult(DriveApi.DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            mToast("Error while trying to create new file contents");
            return;
        }

        String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType("db");
        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                .setTitle(GOOGLE_DRIVE_FILE_NAME) // Google Drive File name
                .setMimeType(mimeType)
                .setStarred(true).build();
        // create a file on root folder
        Drive.DriveApi.getRootFolder(api)
                .createFile(api, changeSet, result.getDriveContents())
                .setResultCallback(backupFileCallback);
    }
};
static final private ResultCallback<DriveFolder.DriveFileResult> backupFileCallback = new ResultCallback<DriveFolder.DriveFileResult>() {
    @Override
    public void onResult(DriveFolder.DriveFileResult result) {
        if (!result.getStatus().isSuccess()) {
            mToast("Error while trying to create the file, try again.");
            DialogFragment_Sync.dismissDialog();
            return;
        }
        mfile = result.getDriveFile();
        mfile.open(api, DriveFile.MODE_WRITE_ONLY, new DriveFile.DownloadProgressListener() {
            @Override
            public void onProgress(long bytesDownloaded, long bytesExpected) {
                DialogFragment_Sync.setProgressText("Creating backup file... ("+bytesDownloaded+"/"+bytesExpected+")");
            }
        }).setResultCallback(backupContentsOpenedCallback);
    }
};
static final private ResultCallback<DriveApi.DriveContentsResult> backupContentsOpenedCallback = new ResultCallback<DriveApi.DriveContentsResult>() {
    @Override
    public void onResult(DriveApi.DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            DialogFragment_Sync.dismissDialog();
            mToast("Error opening file, try again.");
            return;
        }

        DialogFragment_Sync.setProgressText("Backing up..");

        DriveContents contents = result.getDriveContents();
        BufferedOutputStream bos = new BufferedOutputStream(contents.getOutputStream());
        byte[] buffer = new byte[1024];
        int n;

        try {
            FileInputStream is = new FileInputStream(getDbPath());
            BufferedInputStream bis = new BufferedInputStream(is);

            while( ( n = bis.read(buffer) ) > 0 ) {
                bos.write(buffer, 0, n);
                DialogFragment_Sync.setProgressText("Backing up...");
            }
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        contents.commit(api, null).setResultCallback(new ResultCallback<Status>() {
            @Override
            public void onResult(Status status) {
                DialogFragment_Sync.setProgressText("Backup completed!");
                mToast(act.getResources().getString(R.string.backupComplete));
                DialogFragment_Sync.dismissDialog();
            }
        });
    }
};
 类似资料:
  • 数据下载分为两部分:下载任务 和 新建下载 1.下载任务 1)通过数据下载功能,可以下载统计报表,同时可以自主选择需要统计报表所需要输出的内容 2)页面有下载记录以及未结束的任务列表 2.新建下载 第一步:点击[新建下载] 第二步:选择需要的时间段及选项,确认下载 第三步:下载格式为Excel,压缩在压缩包里

  • 6.9.1 什么是数据下载 当数据量较大时(超过20w条),可以使用数据下载模块下载数据报表。该模块最多可支持100w行的数据量下载。 数据下载模块在下载数据量较大的报告时,速度会得到明显提升。 因此建议大家在下载少量数据时,优先使用预定报告模块;在下载大量数据时,优先使用数据下载模块。 注意 数据量表示的并非数字的大小,而是行数的多少。 6.9.2 数据下载的使用 数据下载模块使用方法与预定报告

  • 嗨,我想通过JDBC在DB2、Sybase、MySQL等多个数据库上创建表,现在我需要使用文本文件data.txt创建这个表,它包含数据空间分隔值。对于例如。 现在这个data.txt包含数千个记录,以空格分隔的值。我需要使用java io逐行解析这个文件,并对每个记录执行sql insert查询。 我发现有一个加载数据的sql命令。是否有任何JDBC驱动程序支持此命令?如果不是,什么应该是解决这

  • 源码: tensorflow/g3doc/tutorials-mnist/ 本教程的目标是展示如何下载用于手写数字分类问题所要用到的(经典)MNIST数据集。 教程 文件 本教程需要使用以下文件: 文件 目的 input_data.py 下载用于训练和测试的MNIST数据集的源码 准备数据 MNIST是在机器学习领域中的一个经典问题。该问题解决的是把28x28像素的灰度手写数字图片识别为相应的数字

  • 影像/地图下载        LSV现支持任意图源的叠加下载,只需要选择所需要的下载的底图以及叠加图层即可进行一键下载。        这一功能完全免费,只需要邀请一个好友使用分享的地址进行图新账号的注册,成功后即可使用该功能。        下载管理界面如下:        选择新建下载(当前只支持影像/地图的下载,其他功能还尚在开发中)后进行下载区域的设置,以下为“选择面”为例进行操作。  

  • 影像/地图下载        LSV现支持任意图源的叠加下载,只需要选择所需要的下载的底图以及叠加图层即可进行一键下载。        下载管理界面如下:        选择新建下载(当前只支持影像/地图的下载,其他功能还尚在开发中)后进行下载区域的设置,以下为“选择面”为例进行操作。        之后可以设置底图与叠加图层,所供选择的图层为左侧栏图层列表中所有加载的在线地图(lrc图源)。下载