0

I am attempting to import a database into Android Studio. Right now, I have a database in my assets folder, and I'm displaying it into a listview. I'm getting a "File is encrypted or is not a database" error.

I'm not sure what database file types that Android Studio supports. I'm using SQL. I named my database helper class Ingredient Helper.

I would really appreciate any suggestions you could offer. Here's my logcat and java files.

Thank You!

Logcat:

06-15 21:04:52.504    2157-2157/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
06-15 21:04:52.504    2157-2157/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
06-15 21:04:52.787    3023-3023/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
06-15 21:04:52.788    3023-3023/? E/android.os.Debug﹕ failed to load memtrack module: -2

06-15 21:04:52.940    3054-3054/? E/SQLiteLog﹕ (14) cannot open file at line 31278 of [2ef4f3a5b1]
06-15 21:04:52.940    3054-3054/? E/SQLiteLog﹕ (14) os_unix.c:31278: (2) open(/data/data/com.dsallee.elsdbexecute/databases/sqlite3.sql) -
06-15 21:04:52.941    3054-3054/? E/SQLiteDatabase﹕ Failed to open database '/data/data/com.dsallee.elsdbexecute/databases/sqlite3.sql'.
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:207)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
        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 com.dsallee.elsdbexecute.IngredientHelper.DBExists(IngredientHelper.java:69)
        at com.dsallee.elsdbexecute.IngredientHelper.createDB(IngredientHelper.java:53)
        at com.dsallee.elsdbexecute.IngredientHelper.createDatabase(IngredientHelper.java:48)
        at com.dsallee.elsdbexecute.MainActivity.onCreate(MainActivity.java:34)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

06-15 21:04:52.941    3054-3054/? E/SqlHelper﹕ database not found

06-15 21:04:58.360    3054-3054/? E/SQLiteLog﹕ (26) file is encrypted or is not a database
06-15 21:04:58.360    3054-3054/? E/DefaultDatabaseErrorHandler﹕ Corruption reported by sqlite on database: /data/data/com.dsallee.elsdbexecute/databases/sqlite3.sql
06-15 21:04:58.360    3054-3054/? E/DefaultDatabaseErrorHandler﹕ deleting the database file: /data/data/com.dsallee.elsdbexecute/databases/sqlite3.sql
06-15 21:04:58.360    3054-3054/? E/SQLiteLog﹕ (14) cannot open file at line 31278 of [2ef4f3a5b1]
06-15 21:04:58.360    3054-3054/? E/SQLiteLog﹕ (14) os_unix.c:31278: (2) open(/data/data/com.dsallee.elsdbexecute/databases/sqlite3.sql) -
06-15 21:04:58.360    3054-3054/? E/SQLiteDatabase﹕ Failed to open database '/data/data/com.dsallee.elsdbexecute/databases/sqlite3.sql'.
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:207)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
        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:794)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
        at com.dsallee.elsdbexecute.IngredientHelper.openDatabase(IngredientHelper.java:121)
        at com.dsallee.elsdbexecute.MainActivity.onCreate(MainActivity.java:35)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

06-15 21:04:58.360    3054-3054/? E/Error﹕ Error in codeandroid.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

Main Activity

public class MainActivity extends Activity {

private IngredientHelper dbIngredientHelper = null;
private Cursor ourCursor = null;
private IngredientAdapter adapter = null;
private CursorAdapter CursorAdapter = null;

@Override
public void onCreate(Bundle savedInstanceState) {

    try {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView myListView = (ListView) findViewById(R.id.myListView);


        dbIngredientHelper = new IngredientHelper(this); // initializes helper
        dbIngredientHelper.createDatabase();
        dbIngredientHelper.openDatabase();

        ourCursor = dbIngredientHelper.getCursor();
        startManagingCursor(ourCursor);

        adapter = new IngredientAdapter(ourCursor);

        myListView.setAdapter(adapter);
    } catch (Exception e) {

        Log.e("Error", "Error in code" + e.toString());
        e.printStackTrace();
    }
}


class IngredientAdapter extends CursorAdapter {
    IngredientAdapter(Cursor c) {
        super(MainActivity.this, c);
    }

    @Override
    public void bindView(View row, Context ctxt,
                         Cursor c) {
        IngredientHolder holder = (IngredientHolder) row.getTag();
        holder.populateFrom(c, dbIngredientHelper);
    }

    @Override
    public View newView(Context ctxt, Cursor c,
                        ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row, parent, false);
        IngredientHolder holder = new IngredientHolder(row);
        row.setTag(holder);
        return (row);
    }
}

static class IngredientHolder {
    private TextView note = null;

    IngredientHolder(View row) {
        note = (TextView)row.findViewById(R.id.noteText);
    }

    void populateFrom(Cursor c, IngredientHelper r) {
        note.setText(r.getName(c));

    }
}
}

Ingredient Helper

class IngredientHelper extends SQLiteOpenHelper {

private static final String DATABASE_PATH = "/data/data/com.dsallee.elsdbexecute/databases/";
private static final String DATABASE_NAME = "redo.sql";
private static final int SCHEMA_VERSION = 1;

public static final String TABLE_NAME = "ELS";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "number";

public SQLiteDatabase dbSqlite;

private final Context myContext;
public IngredientHelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    this.myContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {

}

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

}

public void createDatabase() {
    createDB();
}

private void createDB() {

    boolean dbExist = DBExists();

    if (!dbExist) {

        this.getReadableDatabase();
        copyDBFromResource();

    }
}

private boolean DBExists() {

    SQLiteDatabase db = null;

    try {
        String databasePath = DATABASE_PATH + DATABASE_NAME;
        db = SQLiteDatabase.openDatabase(databasePath, null,
                SQLiteDatabase.OPEN_READWRITE);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
        db.setVersion(1);

    } catch (SQLiteException e) {

        Log.e("SqlHelper", "database not found");

    }

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

    }

    return db != null ? true : false;
}

private void copyDBFromResource() {
    InputStream inputStream = null;
    OutputStream outStream = null;
    String dbFilePath = DATABASE_PATH + DATABASE_NAME;

    try {

        inputStream = myContext.getAssets().open(DATABASE_NAME);

        outStream = new FileOutputStream(dbFilePath);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, length);
        }

        outStream.flush();
        outStream.close();
        inputStream.close();

    } catch (IOException e) {

        throw new Error("Problem copying database from resource file.");

    }

}

public void openDatabase() throws SQLException {

    String myPath = DATABASE_PATH + DATABASE_NAME;
    dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);

}

@Override
public synchronized void close() {

    if (dbSqlite != null) {
        dbSqlite.close();
    }
    super.close();
}


public Cursor getCursor() {

    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(TABLE_NAME);
    String[] asColumnsToReturn = new String[]{COLUMN_ID, COLUMN_TITLE};

    Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
            null, null, null, null);

    // ACTUAL TABLE QUERY ABOVE
    // COLUMNS TO RETURN IS COLUMNS TO BE DISPLAYED IN THE LISTVIEW

    return mCursor;
}

public String getName(Cursor c) {
    return (c.getString(1));
    // GET COLUMN INDEX 1 FROM ASCOLUMNSTORETURN
}
}
Kirill
  • 7,580
  • 6
  • 44
  • 95
DSallee
  • 61
  • 8
  • You can't actually open a DB directly from the assets folder ([ref](http://stackoverflow.com/questions/20857734/reading-sqlite-file-from-asset-folder)). You should try the `copyDBFromResource()` method first, but you still have to create your own database before that. I strongly recommend that you use SQLiteAssetHelper as it makes this process much easier. – Daniel Jun 16 '16 at 03:12

1 Answers1

1

since my old db is not encrypted so what should i set password in that case SQLCipher for Android can open an unencrypted database using "" as the password.

i have tried with passing null in password string and also with "" but no luck Then either:

You have a bug in your code, or The database is already encrypted with another password, or The database is corrupted.

Nachiket
  • 159
  • 1
  • 8
  • @Daniel K You can't actually open a DB directly from the assets folder (ref). You should try the copyDBFromResource() method first, but you still have to create your own database before that. I strongly recommend that you use SQLiteAssetHelper as it makes this process much easier. – DSallee Jun 17 '16 at 02:50
  • @Daniel K Thank you for your suggesting SQLiteAsssetHelper! I was able to look it up and find how to use it. However, while I was researching, I read that the database needs to be a .db file type. Is this necessary? I'm using an SQLite3 database, and I can't find any progam to convert SQLite3 to .db. Thanks! – DSallee Jun 17 '16 at 03:27
  • @Daniel Thank you for your suggesting SQLiteAsssetHelper! I was able to look it up and find how to use it. However, while I was researching, I read that the database needs to be a .db file type. Is this necessary? I'm using an SQLite3 database, and I can't find any progam to convert SQLite3 to .db. Thanks! – DSallee Jun 17 '16 at 03:32