ANDROID数据库找不到列_id,即使它是KEY_ID = _id

时间:2015-04-07 00:59:10

标签: java android database sqlite

这是我的错误。

  

无法启动活动:没有这样的列:_id(代码1):, while   编译:SELECT _id,名称FROM联系人WHERE _id

我确定我的专栏拼写_id而不是id我不明白为什么会发生这种情况。

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

这是我的联系课程。

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


}

1 个答案:

答案 0 :(得分:2)

尝试将Database_Version从1升级到2 ......我认为这对您有帮助。