显示android中不存在列'_id'

时间:2015-08-11 07:55:29

标签: android sqlite android-sqlite

我知道还有很多其他相同的问题,但没有解决我的问题。

public class ContactDBHandler extends SQLiteOpenHelper {
    public static int TOTAL_COUNT=0;
    public static final int DATABASE_VERSION=1;
    public static final String DATABASE_NAME="CONTACTDATABASE";
    public static final String TABLE_CONTACTS="ContactTable";
    public static final String KEY_NUMBER="NUMBER";
    public static final String KEY_NAME="NAME";
    public static final String KEY_ROWID = "_ID";
    String CREATE_TABLE_CONTACTS="CREATE TABLE "+TABLE_CONTACTS+"("+KEY_ROWID+" INTEGER PRIMARY KEY autoincrement,"+KEY_NUMBER+" TEXT,"+KEY_NAME+" TEXT)";

    public ContactDBHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(CREATE_TABLE_CONTACTS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_CONTACTS);
        onCreate(db);
    } 
    public void AddContact(Contact contact){
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(KEY_NUMBER, contact.getNumber());
        values.put(KEY_NAME, contact.getName());
        db.insert(TABLE_CONTACTS, null, values);
        db.close();
    }
    public Cursor AllContacts(){
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.query(TABLE_CONTACTS, new String[]{KEY_ROWID,KEY_NUMBER,KEY_NAME}, null, null, null, null, null);
        if(cursor!=null){
            cursor.moveToFirst();
        }
    }
}

这是我的数据库代码

这是我的主动:

public class MainActivity extends Activity {
    Button F_new;
    ListView CONTACT_VIEW;
    final ContactDBHandler CB=new ContactDBHandler(this);

    static final int PICK_CONTACT=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        F_new=(Button) findViewById(R.id.button1);
        CONTACT_VIEW=(ListView) findViewById(R.id.listView1);
        CB.AddContact(new Contact("9961478614", "House"));
        UpdateList();

    }

    private void UpdateList() {
        // TODO Auto-generated method stub
        ContactDBHandler CB=new ContactDBHandler(this);
        Cursor cursor=CB.AllContacts();
        String[] ArrayColumn=new String[]{ContactDBHandler.KEY_NUMBER,ContactDBHandler.KEY_NAME};
        int[] ArrayIDs=new int[]{R.id.text_number,R.id.text_name};
        SimpleCursorAdapter dataAdapter=new SimpleCursorAdapter(this, R.layout.layout_contacts_list, cursor, ArrayColumn, ArrayIDs, 0);
        CONTACT_VIEW.setAdapter(dataAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

当我运行此代码时,它显示列'_id'不存在。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

我认为你应该用_id替换_ID。