dimanche 3 mai 2015

Failed opening Database Sqlite

I have this code which read Database from folder Assets and copy to data/data/databases , i insert data in SqlBrowser but when i start my app i get a lot of errors. What methods i write incorrect?

package vodnik.ua.cursovaa;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;


public class DataBaseHelper extends SQLiteOpenHelper implements BaseColumns {
private static String DB_PATH = "/data/data/vodnik.ua.cursovaa/databases/";
private static final String DB_NAME = "mushrooms.db";
    private SQLiteDatabase myDataBase;
    private  static final int DATABASE_VERSION = 1;
    private final Context myContext;
    public static final String TABLE_NAME = "mushrooms_table";


    DataBaseHelper(Context context) {

        super(context, DB_NAME, null, DATABASE_VERSION);
        this.myContext = context;
    }

    public void createDatabase() throws IOException {
        boolean dbexits = checkDataBase();
        if(dbexits) {



        }else {
            this.getReadableDatabase();

            try {
                copyDatabase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }


    private boolean checkDataBase(){
        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        }catch(SQLiteException e){

        }
        if(checkDB != null){
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }


    private void copyDatabase() throws  IOException{
        Log.i("Database",
                "new database copying in device!");
        int length;
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];

        while ((length = myInput.read(buffer))>0) {
            myOutput.write(buffer , 0 , length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
        Log.i("Database",
                "copying in device 100%");
    }


    public void open() throws SQLException {


        String myPath  = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath , null , SQLiteDatabase.OPEN_READWRITE);

    }

    public synchronized void close() {

        if (myDataBase != null) {
            myDataBase.close();

        }
        super.close();
    }


    public Cursor getInfromation(int id) {
        String selection = "_id = ?";
        String[] selectionArgs = new String[]{String.valueOf(id)};

        return myDataBase.query(TABLE_NAME , null , selection , selectionArgs , null , null , null);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

Here is my log

1845-1845/vodnik.ua.cursovaa I/art﹕ Not late-enabling -Xcheck:jni (already on)
05-03 17:59:34.973    1845-1852/vodnik.ua.cursovaa E/art﹕ Failed writing handshake bytes (-1 of 14): Broken pipe
05-03 17:59:35.019    1845-1852/vodnik.ua.cursovaa I/art﹕ Debugger is no longer active
05-03 17:59:35.065    1845-1852/vodnik.ua.cursovaa W/art﹕ Suspending all threads took: 44.013ms
05-03 17:59:36.683    1845-1860/vodnik.ua.cursovaa I/OpenGLRenderer﹕ Initialized EGL, version 1.4
05-03 17:59:36.756    1845-1860/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 17:59:36.756    1845-1860/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xaeef5780, error=EGL_SUCCESS
05-03 17:59:49.834    1845-1860/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 17:59:49.834    1845-1860/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa7a36740, error=EGL_SUCCESS
05-03 17:59:53.167    1845-1860/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 17:59:53.167    1845-1860/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xaeef5780, error=EGL_SUCCESS
05-03 18:00:01.562    1845-1852/vodnik.ua.cursovaa I/art﹕ Debugger is no longer active
05-03 18:08:11.641    1845-1852/vodnik.ua.cursovaa I/art﹕ Debugger is no longer active
05-03 18:12:02.082    3741-3756/vodnik.ua.cursovaa I/OpenGLRenderer﹕ Initialized EGL, version 1.4
05-03 18:12:02.147    3741-3756/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 18:12:02.147    3741-3756/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xaeefa6e0, error=EGL_SUCCESS
05-03 18:14:58.143    3741-3756/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 18:14:58.143    3741-3756/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa7a33720, error=EGL_SUCCESS
05-03 18:15:02.713    3741-3756/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 18:15:02.713    3741-3756/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xaeefa6e0, error=EGL_SUCCESS
05-03 18:27:57.328    3796-3812/vodnik.ua.cursovaa I/OpenGLRenderer﹕ Initialized EGL, version 1.4
05-03 18:27:57.389    3796-3812/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 18:27:57.396    3796-3812/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa79142e0, error=EGL_SUCCESS
05-03 18:28:10.181    3796-3812/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 18:28:10.181    3796-3812/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa79ff3c0, error=EGL_SUCCESS
05-03 18:28:22.451    3796-3812/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 18:28:22.451    3796-3812/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa79142e0, error=EGL_SUCCESS
05-03 18:28:24.299    3796-3812/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 18:28:24.299    3796-3812/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6e1eda0, error=EGL_SUCCESS
05-03 18:28:26.195    3796-3812/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 18:28:26.195    3796-3812/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa79142e0, error=EGL_SUCCESS
05-03 18:46:40.831    3937-3953/vodnik.ua.cursovaa I/OpenGLRenderer﹕ Initialized EGL, version 1.4
05-03 18:46:40.915    3937-3953/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 18:46:40.915    3937-3953/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xaeedcfa0, error=EGL_SUCCESS
05-03 18:46:50.898    3937-3937/vodnik.ua.cursovaa E/SQLiteLog﹕ (14) cannot open file at line 30046 of [9491ba7d73]
05-03 18:46:50.898    3937-3937/vodnik.ua.cursovaa E/SQLiteLog﹕ (14) os_unix.c:30046: (2) open(/data/data/vodnik.ua.cursovaa/databases/mushrooms.db) -
05-03 18:46:50.917    3937-3937/vodnik.ua.cursovaa E/SQLiteDatabase﹕ Failed to open database '/data/data/vodnik.ua.cursovaa/databases/mushrooms.db'.
    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
            at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
            at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
            at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
            at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
            at vodnik.ua.cursovaa.DataBaseHelper.checkDataBase(DataBaseHelper.java:57)
            at vodnik.ua.cursovaa.DataBaseHelper.createDatabase(DataBaseHelper.java:35)
            at vodnik.ua.cursovaa.Information.onCreate(Information.java:27)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-03 18:46:51.197    3937-3937/vodnik.ua.cursovaa I/Database﹕ Новая база данных копируется на устройство!
05-03 18:46:51.228    3937-3937/vodnik.ua.cursovaa I/Database﹕ Новая база данных скопирована на устройство
05-03 18:46:51.493    3937-3953/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 18:46:51.493    3937-3953/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa7982320, error=EGL_SUCCESS
05-03 18:46:54.743    3937-3953/vodnik.ua.cursovaa W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-03 18:46:54.743    3937-3953/vodnik.ua.cursovaa W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xaeedcfa0, error=EGL_SUCCESS

Aucun commentaire:

Enregistrer un commentaire