samedi 13 juin 2015

I can not change SQLite database created externally

first I want to apologize to possible errors in this post because I am using Google translator to write in English. OK, let's go ...

I'm developing an application that basically will do multiple queries on different database according to choose the User. For this, I created a method that downloads the .db file to the folder /data/data/mypackage/databases and then make the appointments. This part works fine, no problems, but when I try to consult an error is generated. The problem disappears only the second execution of the application.

I found that creating an empty database on the onCreate method of MainActivity it runs normally, but this does not guarantee the exchange database at runtime when I implement it.

follows my code:

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
    private Button btn;
    private Button btn2;
    private static String bdName = "2015_03.db";
    private final String urlDownload ="http://example.com/"+ bdName;
    private final String file ="/data/data/mypackage/databases/"+ bdName;
    private SQLiteDatabase db;
    private String msg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            db = getBaseContext().openOrCreateDatabase(bdName, MODE_PRIVATE, null);
        } catch (Exception e) {
        }

            btn = (Button) findViewById(R.id.button);

            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {                    

            DownloadsManager dm = new DownloadsManager(bdName, urlDownload, file);
                    dm.downloadFile();
              }
            });

            btn2 = (Button) findViewById(R.id.button2);

            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    ManagerDB gb = new ManagerDB(getBaseContext(),bdName,urlDownload,file);
                    gb.action();
                }
            });    
        }
}

if I take this passage nothing else works

    try {
        db = getBaseContext().openOrCreateDatabase(bdName, MODE_PRIVATE, null);
    } catch (Exception e) {
    }

my db manager:

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class ManagerDB {

    private String bdName;
    private String urlDownload;
    private String file;
    private Context context;


    public ManagerDB(Context context, String bdName, String urlDownload, String file) {
        this.context=context;
        this.bdName = bdName;
        this.urlDownload = urlDownload;
        this.file = file;

    }

    public void action() {

        try {

            int total = 0;
            int answer = 0;
            int missed = 0;
            try {

                SQLiteDatabase dbaux = SQLiteDatabase.openDatabase(file, null, SQLiteDatabase.OPEN_READWRITE);
                String[] col = new String[]{"_id", "total", "answer", "missed"};
                Cursor cursor = dbaux.query("registros", col, "_id=1", null, null, null, null);

                if (cursor.getCount() > 0) {

                    cursor.moveToNext();

                    total = cursor.getInt(1);
                    answer = cursor.getInt(2);
                    missed = cursor.getInt(3);
                }

                Log.i("log", "total" +total+"/ answer"+answer+"/ missed:"+missed);

            } catch (Exception e) {               
            }
        } catch (SQLiteException e) {
           }
    }
    }

Aucun commentaire:

Enregistrer un commentaire