-3

I know there was many other same questions but nothing was solved my problem.

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

This is my code for database

And this is my Mainactivity :

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

When I run this code it shows column '_id' does not exist. Can any one help me?

Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
Kannan
  • 51
  • 1
  • 8
  • You may get answers here:http://stackoverflow.com/questions/3359414/android-column-id-does-not-exist, http://stackoverflow.com/questions/21797699/column-id-doesnt-exist-error-though-it-exists-in-table – Ganesh Kumar Aug 11 '15 at 08:05
  • Thanks, that second link helped me. Really thanks. The problem was ID must be in small letters – Kannan Aug 11 '15 at 08:12

1 Answers1

1

I think you should replace _ID with _id.

MarshallLee
  • 1,290
  • 4
  • 23
  • 42