有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android Studio getWritableDatabase();导致我的应用程序崩溃

所以我一直在尝试使用getData()方法进行查询,每当我运行debug时,我都能看到它在dbHelper.getWritableDatabase()行中崩溃

我以为我是用onCreate()方法创建数据库的,所以我不确定是怎么回事

我的代码在下面。我会感激任何帮助

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");
            }
        }

共 (2) 个答案

  1. # 1 楼答案

    以下是如何获取句柄和查询:

    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();
        }
    
        ...
    }
    
  2. # 2 楼答案

    我终于明白了。下面命令中的CNAME应该是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')");