猿问

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

所以我一直在尝试使用该getData()方法进行查询,并且每当我运行调试时,我都可以看到它与该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();

    }

守着一只汪
浏览 444回答 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')");

富国沪深

这是获取句柄和查询的方法: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();    }    ...}
随时随地看视频慕课网APP

相关分类

Java
我要回答