0

问题是我在执行下一个代码时收到了 SqLiteDatabaseCorrupException:

ArrayList<Advertiser> arr = new ArrayList<Advertiser>();
        Cursor holo = db.rawQuery("select * from Advertiser;", null);
        while(holo.moveToNext()){
            Advertiser adver = new Advertiser();
            adver.setId(holo.getString(0));
            adver.setNombre(holo.getString(1));
            adver.setDescripcion(holo.getString(2));
            adver.setDireccion(holo.getString(3));
            adver.setContacto(holo.getString(4));
            adver.setSitioWeb(holo.getString(5));
            adver.setFacebook(holo.getString(6));
            adver.setTwitter(holo.getString(7));
            adver.setPosx(holo.getDouble(8));
            adver.setPosy(holo.getDouble(9));
            adver.setCiudad(holo.getString(10));
            System.out.println("Objeto: " + adver.toString());
            arr.add(adver);
        }

发生的情况是 while 开始正常执行,但是当 log cat 显示数据库已损坏然后数据库被删除时,它会到达一个点。发生这种情况的任何原因?

已编辑:我忘了添加,我的应用程序在主要活动启动时下载数据库,这与数据库损坏有关吗?数据库被下载并存储在 sd 卡上。

编辑#2:这是我如何下载数据库的代码,希望这会有所帮助:

public void DescargaBD() {
    try {
        URL url = new URL(
                "http://71.6.150.179:8079/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();
    }
}

提前致谢。

4

0 回答 0