明明数据库中有表,将JSON数据存入数据库时仍提示找不到表,让人很是郁闷,仔细分析以后才发现原来是自己粗心,拿出来糗一下。
HttpGet requestGet = new HttpGet(URL_GET_CATEGORY); httpResponse = new DefaultHttpClient().execute(requestGet); jsonResults = EntityUtils.toString(httpResponse.getEntity()); JSONArray jsonArray = new JSONArray(jsonResults); // 存入数据库 InspectorApplication.myDataBase.insertCategoryList(TABLE_CATEGORY_CSBJ,getCategoryListFromJSONArray(jsonArray));
/** 向数据库Table中存入数据 */ public void insertCategoryList(String mTable, ArrayList<Category> mCategoryList) { SQLiteDatabase db = getWritableDatabase(); // 插入前先清空表内容 db.delete(mTable, null, null); for (int i = 0; i < mCategoryList.size(); i++) { Category mCategory = mCategoryList.get(i); ContentValues mContentValues = new ContentValues(); mContentValues.put("id", mCategory.id); mContentValues.put("pid", mCategory.pid); mContentValues.put("name", mCategory.name); mContentValues.put("xOrder", mCategory.xOrder); /** 返回新添记录的行号,与主键id无关,发生错误返回-1 */ long value = db.insert(mTable, null, mContentValues); } }
其中:
利用code1、code2试图将JSON数据存入表T_Category_CSBJ时提示错误:E/SQLiteLog(16899): (1) no such table,具体error如图
1、一般找不到表有两种情况:一是没有这张表,二是指向这张表的路径错误。
大家看到了,我“明明”已经新建了表T_Category_CSBJ,代码也没有问题,为什么依然提示错误呢?其实,你只看到LocalCase_new.db中确实有这张表,而忘记对其进行重新打包了,而我们存储的位置恰恰是zip中那张表。
2、先前安装的APP中的数据没有被清理干净。
1、保证zip中一定要有这张表;
2、调试安装前先卸载原来的APP(卸载会清空原来的数据表),而不要直接安装替换。