我的应用程序需要从一个链接下载一个数据库,继承人我用它来下载数据库ADN将其保存在SD卡上的代码:麻烦在下载SQLite数据库
public void DownloadBD() {
try {
URL url = new URL(
"http://mylink/dbHandler.axd?SqliteDbVersion=0");
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot, "DirLaguna.db");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
}
fileOutput.close();
location = file.getAbsolutePath();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
一切似乎都做工精细,但此刻我想查询数据库(我正在使用原始查询),出现一个错误,表示这些表不存在。
之后,我尝试调试应用程序,之后,我得到错误说数据库已损坏。我认为下载过程是造成数据库损坏的原因。
任何帮助将被折衷,如果我错过了一些代码分享请告诉我!
在此先感谢!
你的教程是好的,问题是你修改表,然后将它添加到应用程序,我需要先添加它,然后修改,我仍然会尝试这种方法,谢谢! – CarlosT
对迟到的歉意抱歉,不,我们不会修改和添加,而只是相反。我们不能直接使用数据库。您将需要在您的apk中以编程方式复制它,因为您不能直接与db文件交互并修改内容。它需要成为应用程序的一部分,以便Android可以为您提供SQLite Apis,以便您可以运行查询。另外,请考虑Romins的第二个链接。它会清除你的疑惑 – iabhi