为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

守着一只汪 2021-12-10 15:05:52
所以我一直在尝试使用该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();    }
查看完整描述

2 回答

?
撒科打诨

TA贡献1934条经验 获得超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')");


查看完整回答
反对 回复 2021-12-10
?
富国沪深

TA贡献1790条经验 获得超9个赞

这是获取句柄和查询的方法:


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();

    }


    ...

}


查看完整回答
反对 回复 2021-12-10
  • 2 回答
  • 0 关注
  • 441 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信