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

杀人放火历飞雨 2018-12-28 03:45:21
package com.example.user.acconting;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener{
String db_name="db_name";
String tb_name="tb_name";
static final int MAX=8;
static final String[] FROM=new String[] {"amount","idcategory","date"};
SQLiteDatabase db;
Cursor cur;
SimpleCursorAdapter adapter;
EditText etDate,etAmount,etCategory;
Button btInsert,btUpdate,btDelete,btQuery;
ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
etDate=(EditText)findViewById(R.id.editText3);
etAmount=(EditText)findViewById(R.id.editText5);
etCategory=(EditText)findViewById(R.id.editText4);
btInsert=(Button)findViewById(R.id.button);
btDelete=(Button)findViewById(R.id.button1);
btUpdate=(Button)findViewById(R.id.button2);
btQuery=(Button)findViewById(R.id.button3);
lv=(ListView)findViewById(R.id.lv);
db=openOrCreateDatabase(db_name, Context.MODE_PRIVATE,null);
String createTable="CREATE TABLE IF NOT EXISTS "+tb_name
+ "(_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "amount VARCHAR(16),"
+ "category VARCHAR(32),"
+ "date VARCHAR(32))";
db.execSQL(createTable);
addDate("100", "生活用品", "20170915");
addDate("100","生活用品","20170915");
cur=db.rawQuery("SELECT * FROM tb_name",null);
adapter=new SimpleCursorAdapter(this,R.layout.item,cur,
FROM,
new int[]{R.id.amount,R.id.category,R.id.date},0);
lv=(ListView)findViewById(R.id.lv);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);

}
public void onItemClick (AdapterView<?> parent, View v, int position,long id){
cur.moveToPosition(position);
etAmount.setText(cur.getInt(cur.getColumnIndex(FROM[0])));
etCategory.setText(cur.getString(cur.getColumnIndex(FROM[1])));
etDate.setText(cur.getInt(cur.getColumnIndex(FROM[2])));
}
private void addDate(String amount,String category,String date){
ContentValues cv=new ContentValues(3);
cv.put("amount",amount);
cv.put("category",category);
cv.put("date",date);
db.insert(tb_name,null,cv);
}

}
总显示这个错误: java.lang.IllegalArgumentException: column '_id' does not exist,但我数据库有_id啊,这是为什么?我搜索了一下,也是要加_id,求大佬帮帮忙
...全文
314 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jklwan 2018-12-29
  • 打赏
  • 举报
回复
不会吧,我用的你的代码,改了之后直接运行,没问题
  • 打赏
  • 举报
回复
难道要卸载重装软件吗
  • 打赏
  • 举报
回复
卸载重装了app还是有这个问题
jklwan 2018-12-28
  • 打赏
  • 举报
回复
引用 2 楼 qq_39163982 的回复:
感谢大佬,怎么清除数据?
简单点儿卸载重装就行
  • 打赏
  • 举报
回复
感谢大佬,怎么清除数据?
jklwan 2018-12-28
  • 打赏
  • 举报
回复
static final String[] FROM=new String[] {"amount","idcategory","date"}; 代码里是字段是_id,amount,category,date第二个idcategory改成category。然后清除数据重新运行。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧