这是我的错误。
无法启动活动:无此列:_id(代码1):,而
编译:SELECT _id,在联系人_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;
}
}
最佳答案
尝试将Database_Version从1升级到2 ...我认为这将对您有所帮助。
关于java - ANDROID数据库找不到列_id,即使它是KEY_ID = _id,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29482019/