我的PC上有一个包含一个表的简单SQLite数据库。我的Android应用程序中有相同的SQLite数据库。数据库的PC版本会不时更改数据。如何将数据从PC上的SQLite数据库同步到Android设备上的SQLite数据库?提前感谢任何链接或示例。
答案 0 :(得分:1)
这是一个带套接字的简单解决方案:
服务器端:
ServerSocket servsock = new ServerSocket(2004);
while (true) {
System.out.println("Waiting...");
Socket sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
BufferedReader input = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
String serverResponse = input.readLine();
// sendfile
File myFile = new File("C://XXXX/XXXX/XXXXX.db");
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
sock.close();
}
客户方:
private String serverIpAddress = "xxx.xxx.xxx.xxx";
private static final int REDIRECTED_SERVERPORT = 2004;
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
int filesize = 6022386;
int bytesRead;
int current = 0;
byte[] mybytearray = new byte[filesize];
InputStream is = socket.getInputStream();
BufferedReader input = new BufferedReader(new InputStreamReader(is));
FileOutputStream fos = new FileOutputStream(
"/data/data/XXXXX/databases/XXXXX.db");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray, 0, mybytearray.length);
current = bytesRead;
do {
bytesRead = is.read(mybytearray, current,
(mybytearray.length - current));
if (bytesRead >= 0)
current += bytesRead;
} while (bytesRead > -1);
bos.write(mybytearray, 0, current);
bos.flush();
bos.close();
socket.close();
答案 1 :(得分:0)
这需要相当多的工作才能让它正常运行。我就是这样的:
我们为音乐应用程序做到了这一点。这不是一项微不足道的任务,但我们设法让它运作得很好。