lundi 6 avril 2015

cannot find column _id even though it is KEY_ID=_id

Here is my error. Unable to start activity : no such column: _id (code 1): , while compiling: SELECT _id, name FROM contacts WHERE _id Imade sure my column spelled "_id" instead of "id" I don't understand why this is happening.



public class DatabaseHandler extends SQLiteOpenHelper {


// All Static variables
private static final String TAG = "MyActivity";
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "contactsManager";

// Contacts table name
private static final String TABLE_CONTACTS = "contacts";

// Contacts Table Columns names
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";


public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = " CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT)";
db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

// Create tables again
onCreate(db);
}

// Adding new contact
void addAddress(String name) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, name); // Contact Name


// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}

public boolean deleteAllAddresses() {
SQLiteDatabase db = this.getWritableDatabase();
int doneDelete = 0;
doneDelete = db.delete(TABLE_CONTACTS, null, null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;

}

public Cursor fetchAddressesbyName(String inputText) throws SQLException {

SQLiteDatabase db = this.getWritableDatabase();
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length() == 0) {
mCursor = db.query(TABLE_CONTACTS, new String[]{KEY_ID,
KEY_NAME,},
null, null, null, null, null);

} else {
mCursor = db.query(true, TABLE_CONTACTS, new String[]{KEY_ID,
KEY_NAME,},
KEY_NAME + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;

}

public Cursor fetchAllAddresses() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCursor = db.query(TABLE_CONTACTS, new String[]{ KEY_ID, KEY_NAME,},KEY_ID , null,null,null, null);

if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}


Here is my contact class.



public class Contact {
//private variables
int id;
String name=null;

// getting ID
public int getID(){
return id;
}

// setting id
public void setID(int id){
this.id = id;
}

// getting name
public String getName(){
return name;
}

// setting name
public void setName(String name){
this.name = name;
}


}

Aucun commentaire:

Enregistrer un commentaire