dimanche 4 octobre 2015

Android on eclipse, failed to open database

I am new on android app development, and currently develop an app.

Here is my logcat:

10-04 22:32:08.596: E/Trace(2405): error opening trace file: No such file or directory (2)
10-04 22:32:09.356: E/SQLiteLog(2405): (14) cannot open file at line 30174 of [00bb9c9ce4]
10-04 22:32:09.356: E/SQLiteLog(2405): (14) os_unix.c:30174: (2) open(/data/data/com.imr.khadijah/databases/KhadDB.db) - 
10-04 22:32:09.366: E/SQLiteDatabase(2405): Failed to open database '/data/data/com.imr.khadijah/databases/KhadDB.db'.
10-04 22:32:09.366: E/SQLiteDatabase(2405): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at com.imr.khadijah.DataBaseOpenHelper.checkDataBase(DataBaseOpenHelper.java:70)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at com.imr.khadijah.DataBaseOpenHelper.create(DataBaseOpenHelper.java:45)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at com.imr.khadijah.Login.onCreate(Login.java:22)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.app.Activity.performCreate(Activity.java:5008)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.os.Looper.loop(Looper.java:137)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at java.lang.reflect.Method.invoke(Method.java:511)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-04 22:32:09.366: E/SQLiteDatabase(2405):     at dalvik.system.NativeStart.main(Native Method)
10-04 22:32:09.606: D/dalvikvm(2405): GC_CONCURRENT freed 107K, 4% free 6267K/6471K, paused 24ms+44ms, total 113ms
10-04 22:32:09.646: D/AndroidRuntime(2405): Shutting down VM
10-04 22:32:09.656: W/dalvikvm(2405): threadid=1: thread exiting with uncaught exception (group=0xb5ecb288)
10-04 22:32:09.656: E/AndroidRuntime(2405): FATAL EXCEPTION: main
10-04 22:32:09.656: E/AndroidRuntime(2405): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.imr.khadijah/com.imr.khadijah.Login}: java.lang.RuntimeException: KhadDB.db
10-04 22:32:09.656: E/AndroidRuntime(2405):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at android.os.Looper.loop(Looper.java:137)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at java.lang.reflect.Method.invoke(Method.java:511)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at dalvik.system.NativeStart.main(Native Method)
10-04 22:32:09.656: E/AndroidRuntime(2405): Caused by: java.lang.RuntimeException: KhadDB.db
10-04 22:32:09.656: E/AndroidRuntime(2405):     at com.imr.khadijah.DataBaseOpenHelper.create(DataBaseOpenHelper.java:56)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at com.imr.khadijah.Login.onCreate(Login.java:22)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at android.app.Activity.performCreate(Activity.java:5008)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-04 22:32:09.656: E/AndroidRuntime(2405):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-04 22:32:09.656: E/AndroidRuntime(2405):     ... 11 more

I failed to read error messages on logcat... and here is my DatabaseOpenHelper class

public class DataBaseOpenHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "KhadDB.db";
    private static final int DATABASE_VERSION = 2;

    private static final String TABLE_NAME = "users";
    private static final String COLUMN_ID = "user_id";
    private static final String COLUMN_USERNAME = "username";
    private static final String COLUMN_PASSWORD = "password";

    private SQLiteDatabase database;

    private final Context context;

    // database path
    private static String DATABASE_PATH;

    /** constructor */
    public DataBaseOpenHelper(Context ctx) {
        super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = ctx;
        DATABASE_PATH = context.getFilesDir().getParentFile().getPath()
                + "/databases/";
    }

    /**
     * Creates a empty database on the system and rewrites it with your own
     * database.
     **/
    public void create() throws IOException {
        boolean check = checkDataBase();
        SQLiteDatabase db_Read = null;

        // Creates empty database default system path
        db_Read = this.getWritableDatabase();
        db_Read.close();
        try {
            if (!check) {
                copyDataBase(); 
            }
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage()); //Error("Error copying database");
        }
    }

    /**
     * Check if the database already exist to avoid re-copying the file each
     * time you open the application.
     * 
     * @return true if it exists, false if it doesn't
     **/
    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            String myPath = DATABASE_PATH + DATABASE_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLiteException e) {
            // database does't exist yet.
        }

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

    /**
     * Copies your database from your local assets-folder to the just created
     * empty database in the system folder, from where it can be accessed and
     * handled. This is done by transfering bytestream.
     **/
    private void copyDataBase() throws IOException {

        // Open your local db as the input stream
        InputStream myInput = context.getAssets().open(DATABASE_NAME);

        // Path to the just created empty db
        String outFileName = DATABASE_PATH + DATABASE_NAME;

        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    /** open the database */
    public void open() throws SQLException {
        String myPath = DATABASE_PATH + DATABASE_NAME;
        database = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    }

    /** close the database */
    @Override
    public synchronized void close() {
        if (database != null)
            database.close();
        super.close();
    }

    // insert a user into the database
    public long insertUser(String username, String password) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(COLUMN_USERNAME, username);
        initialValues.put(COLUMN_PASSWORD, password);
        return database.insert(TABLE_NAME, null, initialValues);
    }

    // updates a user
    public boolean updateUser(long rowId, String username, String password) {
        ContentValues args = new ContentValues();
        args.put(COLUMN_USERNAME, username);
        args.put(COLUMN_PASSWORD, password);
        return database.update(TABLE_NAME, args, COLUMN_ID + "=" + rowId, null) > 0;
    }

    // retrieves a particular user
    public Cursor getUser(long rowId) throws SQLException {
        Cursor mCursor = database.query(true, TABLE_NAME, new String[] {
                COLUMN_ID, COLUMN_USERNAME, COLUMN_PASSWORD },
                COLUMN_ID + " = " + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }

        return mCursor;
    }

    // delete a particular user
    public boolean deleteContact(long rowId) {
        return database.delete(TABLE_NAME, COLUMN_ID + "=" + rowId, null) > 0;
    }

    // retrieves all users
    public Cursor getAllUsers() {
        return database.query(TABLE_NAME, new String[] { COLUMN_ID,
    COLUMN_USERNAME, COLUMN_PASSWORD }, null, null,
    null, null, null);
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

and this is my login activity...

public class Login extends Activity {

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

        DataBaseOpenHelper myDbHelper = new DataBaseOpenHelper(this);

        try {
            // check if database exists in app path, if not copy it from assets
            myDbHelper.create();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }

        try {
            // open the database
            myDbHelper.open();
            myDbHelper.getWritableDatabase();
        } catch (SQLException sqle) {
            throw sqle;
        }

        // retrieve a particular user
        Cursor c = myDbHelper.getUser(1);
        Toast.makeText(this,
                "id: " + c.getInt(0) + "\n username: " + c.getString(1)
                + "\n password: " + c.getString(2), Toast.LENGTH_LONG).show();
        c.close();

        // close the database
        myDbHelper.close();

    }
}

I've tried to reinstall the app but no luck, I think its error when copying database...

Aucun commentaire:

Enregistrer un commentaire