从一个Android设备传送数据库中的数据到另一个数据库中、数据、设备、Android

2023-09-06 04:52:34 作者:朕要回幼儿园深造

我有一个应用程序需要实现一个分享选项,包含在设备的本地数据库中的数据传输到另一个设备,其中,该设备可以利用这些数据,并将其插入设备的数据库中的相应的表。

I have an app that needs to implement a 'share' option to transfer data contained in the device's local database to another device, where that device can take this data and insert it into the appropriate tables in the device's database.

我有沟通工作,通过蓝牙,我只是在寻找跨越传输这些数据的好方法。有一个简单的方法来做到对设备中的一个SQLite转储,使用蓝牙传输跨越这设备B,并有设备B重新插入这个数据?

I have communication working over bluetooth, I'm just looking for a good way to transfer this data across. Is there an easy way to do an sqlite dump on device A, transfer this across to device B using bluetooth, and have device B reinsert this data?

推荐答案

实现一个的 ContentProvider的是你如何利用Android框架做CRUD操作上的数据库。

Implementing aContentProvider is how you leverage the Android framework to do CRUD operations on a database.

一个很好的方法可以是改变一行到JSON和解包在接收端的字符串,将其插入。这种方式更可测性,更容易从错误中发送过程中恢复,但可能会很慢取决于您的数据的大小。对于加速起来,我要送多行分批测试,以查看批量大小将是最适合的速度和可靠性。

A good way could be to transform a row into Json and unpackage that string on the receiving side to insert it. This way is more testable, easier to recover from errors during send, but might be very slow depending on the size of your data. For speeding it up I would send multiple rows in batches and test to see what batch size would be best for speed and reliability.

有一个快速的方法可能是转储数据平面文件,然后使用的 FileProvider 提供访问该文件作为一个URI和尝试类似的的这里

A fast way might be to dump the data to flat file then use the FileProvider to provide access to that file as a URI and try something like the following from here

    public void sendFile(Uri uri, BluetoothSocket bs) throws IOException {
            BufferedInputStream bis = new BufferedInputStream(getContentResolver().openInputStream(uri));
            OutputStream os = bs.getOutputStream();
        try {
            int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        // we need to know how may bytes were read to write them to the byteBuffer
        int len = 0;
        while ((len = bis.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
    } finally {
        bis.close();
        os.flush();
        os.close();
        bs.close();
    }
}