Android Studio getWritableDatabase(); is causing my app to crash

android studio app crashes on startup
android studio app crashes without error
android app crash log
sqliteopenhelper example
android studio app crashes on device
android sqliteopenhelper singleton example
android studio app keeps stopping in emulator
app keeps stopping android studio

So I have been trying to do a query with the getData() method, and whenever I run debug, I can see that it crashes with the dbHelper.getWritableDatabase() line.

I thought I was creating the database in the onCreate() method so I am not sure what is going on.

My code is down below. I would appreciate any help!

public String getData(int firstSelection, int secondSelection, int thirdSelection,
                          int fourthSelection, int fifthSelection)
    {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String firstSelectionStr, secondSelectionStr, thirdSelectionStr, fourthSelectionStr, fifthSelectionStr;

        firstSelectionStr = Integer.toString(firstSelection);
        secondSelectionStr = Integer.toString(secondSelection);
        thirdSelectionStr = Integer.toString(thirdSelection);
        fourthSelectionStr = Integer.toString(fourthSelection);
        fifthSelectionStr = Integer.toString(fifthSelection);


        String[] columns = {DBHelper.UID,DBHelper.CNAME};

        String selectQuery = "SELECT " + DBHelper.CNAME + " FROM "+ "CraftsAppDatabase" + " WHERE First_Attribute=? "
                + " AND Second_Attribute=? " + " AND Third_Attribute=? " + " AND Fourth_Attribute=? "
                + " AND Fifth_Attribute=? ";
        Cursor cursor=db.rawQuery(selectQuery, new String[] {firstSelectionStr, secondSelectionStr, thirdSelectionStr,
                            fourthSelectionStr, fifthSelectionStr});
        StringBuilder buffer = new StringBuilder();

        // Append every data together
        while (cursor.moveToNext())
        {

            String chosenItem = cursor.getString(cursor.getColumnIndex(DBHelper.CNAME));
            buffer.append(chosenItem + "/n");
        }
        return buffer.toString();
    }

static class DBHelper extends SQLiteOpenHelper
    {
        private static final String DATABASE_NAME = "CraftsAppDatabase";    // Database Name
        private static final String TABLE_NAME = "CraftTools";   // Table Name
        private static final String RESULT_TABLE = "Result";   // Table Name
        private static final int DATABASE_Version = 1;    // Database Version
        private static final String UID="_id";     // Column I (Primary Key)
        private static final String CNAME = "Craft_Name";    //Column II
        private static final String RESULT = "Result_Name";    //Column II
        private static final String FIRST_ATTRIBUTE = "First_Attribute";    //Column III
        private static final String SECOND_ATTRIBUTE = "Second_Attribute";    //Column IV
        private static final String THIRD_ATTRIBUTE = "Third_Attribute";    //Column V
        private static final String FOURTH_ATTRIBUTE = "Fourth_Attribute";    //Column VI
        private static final String FIFTH_ATTRIBUTE = "Fifth_Attribute";    //Column VII
        private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+
                " ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+CNAME+" VARCHAR(255)" +
                ", "+FIRST_ATTRIBUTE+" VARCHAR(255), "+SECOND_ATTRIBUTE+" VARCHAR(255)" +
                ", "+THIRD_ATTRIBUTE+" VARCHAR(255), "+FOURTH_ATTRIBUTE+" VARCHAR(255)" +
                ", "+FIFTH_ATTRIBUTE+" VARCHAR(255));";
        private static final String CREATE_OTHER_TABLE = "CREATE TABLE "+RESULT_TABLE+
                " ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+RESULT+" VARCHAR(255));";
        private static final String DROP_TABLE ="DROP TABLE IF EXISTS "+TABLE_NAME;
        private Context context;

        public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_Version);
            this.context=context;
        }


        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE);
            db.execSQL(CREATE_OTHER_TABLE);
            db.execSQL("INSERT INTO " + TABLE_NAME + "(CNAME, First_Attribute, Second_Attribute, Third_Attribute, Fourth_Attribute, Fifth_Attribute ) " +
                    "VALUES ('Landscape Drawing', '1', '4','8', 'NONE', 'NONE')");
            db.execSQL("INSERT INTO " + TABLE_NAME + "(CNAME, First_Attribute, Second_Attribute, Third_Attribute, Fourth_Attribute, Fifth_Attribute ) " +
                    "VALUES ('Popsicle Sticks House', '2', '3','NONE', 'NONE', 'NONE')");
            db.execSQL("INSERT INTO " + TABLE_NAME + "(CNAME, First_Attribute, Second_Attribute, Third_Attribute, Fourth_Attribute, Fifth_Attribute ) " +
                    "VALUES ('Sunset Painting', '4', '7','10', 'NONE', 'NONE')");

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL(DROP_TABLE);
            onCreate(db);
            if (newVersion > oldVersion) {
                db.execSQL("ALTER TABLE CraftTools ADD COLUMN FIRST_ATTRIBUTE INTEGER DEFAULT 0");
                db.execSQL("ALTER TABLE CraftTools ADD COLUMN SECOND_ATTRIBUTE INTEGER DEFAULT 0");
                db.execSQL("ALTER TABLE CraftTools ADD COLUMN THIRD_ATTRIBUTE INTEGER DEFAULT 0");
                db.execSQL("ALTER TABLE CraftTools ADD COLUMN FOURTH_ATTRIBUTE INTEGER DEFAULT 0");
                db.execSQL("ALTER TABLE CraftTools ADD COLUMN FIFTH_ATTRIBUTE INTEGER DEFAULT 0");
            }
        }

DATABASE_NAME don't have the .db extension. that can be a problem. another issue can arise because of not initializing dbHelper.

Simply add a space after 'id' in the private static final String COLUMN_ID = "id";. I always print my db commands constructed in the code in the  SupportSQLiteDatabase getWritableDatabase () Create and/or open a database that will be used for reading and writing. The first time this is called, the database will be opened and onCreate(SupportSQLiteDatabase) , onUpgrade(SupportSQLiteDatabase, int, int) and/or onOpen(SupportSQLiteDatabase) will be called.

this is how to get a handle and query:

public DBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME     = "crafts.db"; 
    private static final String TABLE_CRAFT_TOOLS = "craft_tools";
    private static final String KEY_CRAFT_NAME    = "craft_name";

    protected static SQLiteDatabase db = null;

    /** Constructor */
    public SqliteBaseHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
        if(db == null) {db = getWritableDatabase();}
    }

    /** this method possibly belongs into the helper class */
    public String getString(int firstSelection, int secondSelection, int thirdSelection, int fourthSelection, int fifthSelection) {

        StringBuilder sb = new StringBuilder();

        String sql = "SELECT " + KEY_CRAFT_NAME + " FROM " + TABLE_CRAFT_TOOLS + " WHERE " +
            "First_Attribute=? AND " +
            "Second_Attribute=? AND " +
            "Third_Attribute=? AND " +
            "Fourth_Attribute=? AND " +
            "Fifth_Attribute=? ";

        String[] selectionArgs = new String[] {
            Integer.toString(firstSelection),
            Integer.toString(secondSelection),
            Integer.toString(thirdSelection),
            Integer.toString(fourthSelection),
            Integer.toString(fifthSelection)
        };

        try {
            Cursor cursor = db.rawQuery(sql, selectionArgs);
            if (cursor.moveToFirst()) {
                do {
                    sb.append(cursor.getString(cursor.getColumnIndex(KEY_CRAFT_NAME)) + "/n");
                } while(cursor.moveToNext());
            } else {
                Log.w(LOG_TAG, "no crafts found.");
            }
        } catch (SQLiteException e) {
            Log.e(LOG_TAG, e.getMessage());
        } finally {
            if (! cursor.isClosed()) {
                cursor.close();
            }
        }
        return sb.toString();
    }

    ...
}

First time that i run the app crash, second runs fine. I creating a quiz game sql){ SQLiteDatabase db = getWritableDatabase(); Cursor cursor = db.rawQuery(sql  Create a Database Android Application in Android Studio 3.0 This article explains the steps for creating a database application in Android Studio for those getting started with Android programming. by

I finally figured this out. That CNAME in the command below should have been Craft_Name.

db.execSQL("INSERT INTO " + TABLE_NAME + "(CNAME, First_Attribute, Second_Attribute, Third_Attribute, Fourth_Attribute, Fifth_Attribute ) " +
                    "VALUES ('Landscape Drawing', '1', '4','8', 'NONE', 'NONE')");

An Android app crashes whenever there's an unexpected exit caused by an unhandled exception or signal. An app that is written using Java or  And when we talk about android so SQLite is that default feature which is used as a database and also used as a local database for any application. This tutorial shows a very simple example which is to just store important data like shops address or contacts using SQLite Database in the android studio.

Learn to migrate databases safely using the Room Library. and an incorrectly-​defined migration could cause your app to crash. getWritableDatabase() This causes a discrepancy in the underlying table between updates Developer guides · Design guides · API reference · Samples · Android Studio. The following are top voted examples for showing how to use android.database.sqlite.SQLiteOpenHelper.These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.

Im getting following log on app crash, on android 5.0v devices. Caused by: net.​sqlcipher.database.SQLiteException: I am wondering if there is an issue within the internals of getWritableDatabase() . As an executeInsert();. then i got I mean previously I was using aar in Android studio, like - compile  Android comes in with built in SQLite database implementation. SQLite supports all the relational database features. In order to access this database, you don't need to establish any kind of connections for it like JDBC,ODBC e.t.c. Database - Package. The main package is android.database.sqlite that contains the classes to manage your own databases

The app crashes at the first "database" call of my Migrations' migrate method. The error was caused by another crash shortly before, which closed the db. getWritableDatabase(); if (mMigrated) { // there might be a connection w/ log of the 'Run'-window of Android Studio, it is only visible in the Logcat. Local Databases with SQLiteOpenHelper. Edit Page Page History. Overview. For maximum control over local data, developers can use SQLite directly by leveraging SQLiteOpenHelper for executing SQL requests and managing a local database.

Comments
  • post your logcat