博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android – 报错column ‘_id’ does not exist的解决
阅读量:4113 次
发布时间:2019-05-25

本文共 896 字,大约阅读时间需要 2 分钟。

1、程序中用到了sqlite,结果运行时报错如下

1
java.lang.IllegalArgumentException: column
'_id'
does not exist

2、网上搜索一番,出现该错误原因是:

使用Cursor相关的Adapter时需要一个自增的列,且名字必需为 _id。而我创建的表里没有这个字段,只有一个名为id的自增列。
3、解决办法

1)创建数据表时插入一个名为_id的列,类型为自增量,因为在使用Cursor相关的Adapter时需要用到这个列

2)如果实在不需要这个列的话,可以把数据表中某一列使用AS映射为 _id,再进行操作

如,我有一个表, 表中两个字段分别为id, 和jokeContent, 现在我想用一个SimpleCursorAdapter来把这个表中的数据显示到一个listView上,注意其中有"id as _id", 以及new String[]{"_id", "jokeContent"}.

ListView lv = getListView();

lv.setOnItemClickListener(this);

SQLiteDatabase db = .....;

Cursor result = db.query("jokesExport", new String[]{

"id as _id", "jokeContent"}, null, null, null, null, null);

startManagingCursor(result);

if (result != null) {

SimpleCursorAdapter la = new SimpleCursorAdapter(this, R.layout.customcell, result, new String[]{

"_id", "jokeContent"}, new int[]{R.id.mytext1, R.id.mytext2});

lv.setAdapter(la);

}

// 然后就能把这个已经存在的数据库用listView显示出来了

转载地址:http://fhwpi.baihongyu.com/

你可能感兴趣的文章
python: extend (扩展) 与 append (追加) 的差别
查看>>
「译」在 python 中,如果 x 是 list,为什么 x += "ha" 可以运行,而 x = x + "ha" 却抛出异常呢?...
查看>>
浅谈JavaScript的语言特性
查看>>
LeetCode第39题思悟——组合总和(combination-sum)
查看>>
LeetCode第43题思悟——字符串相乘(multiply-strings)
查看>>
LeetCode第44题思悟——通配符匹配(wildcard-matching)
查看>>
LeetCode第45题思悟——跳跃游戏(jump-game-ii)
查看>>
LeetCode第46题思悟——全排列(permutations)
查看>>
LeetCode第47题思悟—— 全排列 II(permutations-ii)
查看>>
LeetCode第48题思悟——旋转图像(rotate-image)
查看>>
驱动力3.0,动力全开~
查看>>
记CSDN访问量10万+
查看>>
Linux下Oracle数据库账户被锁:the account is locked问题的解决
查看>>
记CSDN访问20万+
查看>>
Windows 环境下Webstorm 2020.3 版本在右下角找不到Git分支切换部件的一种解决方法
查看>>
Electron-Vue项目中遇到fs.rm is not a function问题的解决过程
查看>>
飞机换乘次数最少问题的两种解决方案
查看>>
有向无回路图的理解
查看>>
设计模式中英文汇总分类
查看>>
WPF实现蜘蛛纸牌游戏
查看>>