我打算编写一个简单的Android应用程序,就像一个教授的小目录.它将有他们的名字,电子邮件,电话和他们的照片.我需要手动将sqlite文件从服务器发送到手机.我一直在尝试研究如何执行此操作,但是看起来有很多方法!我希望有人能指出我的最佳方向!
解决方法:
我能想到的最简单的方法是打开服务器的URLConnection,读取响应并将其保存到应用程序的数据库目录(或SD卡)中.
例如:
URL url = new URL("http://example.com/file.sqlite");
URLConnection conn = url.openConnection();
BufferedInputStream bin = new BufferedInputStream(conn.getInputStream());
FileOutputStream fos =
new FileOutputStream("/data/data/[your.pkg.name]/databases/file.sqlite");
byte[] buffer = new byte[1024];
int read = 0;
do {
read = bin.read(buffer, 0, buffer.length);
if (read > 0)
fos.write(buffer, 0, read);
} while (read >= 0);
之后,您可以使用SQLiteOpenHelper的子类或直接通过调用SQLiteDatabase.openDatabase(..)打开数据库:
例如.:
SQLiteDatabase.openDatabase("/data/data/[your.pkg.name]/databases/file.sqlite",
null, SQLiteDatabase.OPEN_READWRITE);
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!