lundi 13 juillet 2015

Could not open the database - sqlite - Android Studio

I copied the database into the assets folder using Firefox's SQLiteManager. I'm not sure I have the Path correct - I changed the /databases to /assets as I read this is the way to do it using Android Studio. Also, I saw this post: http://ift.tt/1O7Hj5j could- not-open-database

and then added the DATABASE_CREATE statement to the onCreate method - but I did not have that in the beginning because I was copying the database from an existing source and did not think I needed the DATABASE_CREATE statement. Please let me know something! Thanks in advance.

Here's the LogCat:

    07-13 16:34:54.726    1060-1060/? E/SQLiteLog﹕ (14) cannot open file at line 30184 of [00bb9c9ce4]
07-13 16:34:54.726    1060-1060/? E/SQLiteLog﹕ (14) os_unix.c:30184: (2) open(/data/data/com.blogspot.joyouslybeingjoy.promisescyb/assets/cybdatabase.sqlite) -
07-13 16:34:54.746    1060-1060/? E/SQLiteDatabase﹕ Failed to open database '/data/data/com.blogspot.joyouslybeingjoy.promisescyb/assets/cybdatabase.sqlite'.
    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:804)
            at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
            at com.blogspot.joyouslybeingjoy.promsiesofthebiblecyb.DatabaseHelper.DBExists(DatabaseHelper.java:88)
            at com.blogspot.joyouslybeingjoy.promsiesofthebiblecyb.DatabaseHelper.createDatabase(DatabaseHelper.java:74)
            at com.blogspot.joyouslybeingjoy.promsiesofthebiblecyb.MainActivity.onCreate(MainActivity.java:50)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
07-13 16:34:54.746    1060-1060/? D/AndroidRuntime﹕ Shutting down VM
07-13 16:34:54.746    1060-1060/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1aa7ba8)
07-13 16:34:54.766    1060-1060/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.blogspot.joyouslybeingjoy.promsiesofthebiblecyb, PID: 1060
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blogspot.joyouslybeingjoy.promsiesofthebiblecyb/com.blogspot.joyouslybeingjoy.promsiesofthebiblecyb.MainActivity}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: 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:804)
            at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
            at com.blogspot.joyouslybeingjoy.promsiesofthebiblecyb.DatabaseHelper.DBExists(DatabaseHelper.java:88)
            at com.blogspot.joyouslybeingjoy.promsiesofthebiblecyb.DatabaseHelper.createDatabase(DatabaseHelper.java:74)
            at com.blogspot.joyouslybeingjoy.promsiesofthebiblecyb.MainActivity.onCreate(MainActivity.java:50)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Here's my MainActivity class:

public class MainActivity extends Activity {

public static String CATEGORY_EXTRA="com.blogspot.joyouslybeingjoy.promsiesofthebiblecyb.MainActivity.category";

private DatabaseHelper dbHelper = null;
private Cursor ourCursor = null;
private CategoryAdapter adapter = null;
ListView listView;

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

    listView = (ListView) findViewById(R.id.listViewCategories);
    String[] categories = getResources().getStringArray(R.array.categories_array);

    dbHelper = new DatabaseHelper(this);
    dbHelper.createDatabase();
    try {
        dbHelper.openDataBase();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    ourCursor = dbHelper.getCursor();
    startManagingCursor(ourCursor);
    adapter = new CategoryAdapter(ourCursor);

    listView.setAdapter(adapter);
    listView.setOnItemClickListener(onListClick);

}

private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        // ListView Clicked item index
        int itemPosition = position;
        // ListView Clicked item value
        String itemValue = (String) listView.getItemAtPosition(position);

        CATEGORY_EXTRA = itemValue;

        Intent i = new Intent(MainActivity.this, DisplayResult.class);
        i.putExtra(CATEGORY_EXTRA, itemValue);
        startActivity(i);
    }
};

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

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

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

static class CategoryHolder {
    private TextView category = null;
    private TextView book = null;
    private TextView chapter = null;
    private TextView verse = null;
    private TextView word = null;

    CategoryHolder(View row) {
        category = (TextView)row.findViewById(R.id.txtViewCategory);
        book = (TextView)row.findViewById(R.id.txtViewBook);
        chapter = (TextView)row.findViewById(R.id.txtViewChapter);
        verse = (TextView)row.findViewById(R.id.txtViewVerse);
        word = (TextView)row.findViewById(R.id.txtViewWord);
    }

    void populateFrom(Cursor c, DatabaseHelper r) {
        category.setText(r.getColumnCategory(c));
        book.setText(r.getColumnBook(c));
        chapter.setText(r.getColumnChapter(c));
        verse.setText(r.getColumnVerse(c));
        word.setText(r.getColumnWord(c));

    }

Here's the second activity, DisplayResult: public class DisplayResult extends Activity {

String passedVar = null;
private TextView passedView = null;

private DatabaseHelper dbHelper = null;
private Cursor ourCursor = null;
private VerseAdapter adapter = null;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verses);

    passedVar = getIntent().getStringExtra(MainActivity.CATEGORY_EXTRA);

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

    dbHelper = new DatabaseHelper(this);
    // a set of results from a db query
    ourCursor = dbHelper.getVersesByCategory(passedVar);
    startManagingCursor(ourCursor);
    adapter = new VerseAdapter(ourCursor);
    myListView.setAdapter(adapter);
}

class VerseAdapter extends CursorAdapter {
    VerseAdapter(Cursor c) {
        super(DisplayResult.this, c);
    }

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

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

static class VerseHolder {
    private TextView category = null;
    private TextView book = null;
    private TextView chapter = null;
    private TextView verse = null;
    private TextView word = null;

    VerseHolder(View row) {
        category = (TextView)row.findViewById(R.id.txtViewCategory);
        book = (TextView)row.findViewById(R.id.txtViewBook);
        chapter = (TextView)row.findViewById(R.id.txtViewChapter);
        verse = (TextView)row.findViewById(R.id.txtViewVerse);
        word = (TextView)row.findViewById(R.id.txtViewWord);
    }

    void populateFrom(Cursor c, DatabaseHelper r) {
        category.setText(r.getColumnCategory(c));
        book.setText(r.getColumnBook(c));
        chapter.setText(r.getColumnChapter(c));
        verse.setText(r.getColumnVerse(c));
        word.setText(r.getColumnWord(c));

    }
}

And here's the DatabaseHelper class:

public class DatabaseHelper extends SQLiteOpenHelper {





private static final int DB_VERSION = 1;                   


private static final S

ing DB_NAME = "cybdatabase.sqlite";

private String DB_PATH = "/data/data/

om.blogspot.joyouslybeingjoy.promisescyb/assets/";

public sta

ic final String TABLE_NAME = "cybpromises";

public static final

String COLUMN_ID = "_id";

public 

tatic

inal String C LUMN_CATEGORY = "category";

public stat

c final String COLUMN_BOOK = "book";

public static final String COLUMN_CHAPTER = "c

apter";
pub ic static final String COLUMN_VERSE = "verse";

public stati

final String COLUMN_WORD = "word";

private SQLiteDatabase dbSqlite;                                 




private fin

Context myContext;

private Context con

ext;

public DatabaseHelper(Context context) {                                                

    super(context, DB_NAME, null, 

B_VERSION);

this.myContext = cont

xt;

}   













@Override                                   

publ

c void onCreate(SQLiteDatabase db) {

    String DATABASE_

REATE =

            "CRE

TE TABLE if not exists " + TABLE_NAME + " ( " +

             COLUMN_ID + " INTEGER PRIMARY K

Y, " +

                    COLUMN_CATEGORY + " TEXT, " +                                 




                 COLUMN_BOOK +

" TEXT, " +

COLUMN_CHAPTER + " INTEGER " +

  COLUMN_VERSE + " INTEGER, " +    


                    COLUMN_WORD + " TEXT " + ")";     


    db.execSQL(DATABASE_CREATE);                       
















}    












@Override 


public void onUpgrade(SQLiteDatabase db, 

n

Aucun commentaire:

Enregistrer un commentaire