Android SQLite 问题:'Table [...] has no column named' 错误

Android SQLite issue: 'Table [...] has no column named' error

我不断收到错误消息,提示无法找到 table 中的一列。无法弄清楚为什么会这样。列名似乎拼写正确。

我收到此错误: 06-11 21:27:02.701 3703-3703/com.example.luca.contatti E/SQLiteLog﹕ (1) table Contatti has no column named imageUri

这是我的数据库处理程序页面:

public class DatabaseHandler extends SQLiteOpenHelper {
    public static final String DB_NAME = "dbContatti",
        TABLE = "Contatti",
        ID = "id",
        NAME = "name",
        PHONE = "phone",
        EMAIL = "email",
        ADDRESS = "address",
        IMAGEURI = "imageUri";
public static final int DB_VERSION = 4;

public DatabaseHandler(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
    //Creazione della tabella per il DB
    db.execSQL("CREATE TABLE " + TABLE + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + NAME + "TEXT," + PHONE + "TEXT," + EMAIL + "TEXT," + ADDRESS + "TEXT," + IMAGEURI + "TEXT " + ")");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE);

    onCreate(db);
}

// CRUD: CREATE

public void createContact (ContactList contact){
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(NAME, contact.getName());
    values.put(PHONE, contact.getPhone());
    values.put(EMAIL, contact.getEmail());
    values.put(ADDRESS, contact.getAddress());
    values.put(IMAGEURI, contact.getImageUri().toString());

    db.insert(TABLE, null, values);
    db.close();
}

//CRUD: READ

public ContactList getContact(int id){
    SQLiteDatabase db = getReadableDatabase();

    Cursor cursor = db.query(TABLE, new String[] {ID, NAME, PHONE, EMAIL, ADDRESS, /*IMAGEURI*/}, ID + "=?", new String[] {String.valueOf(id)},null,null,null,null );

    if (cursor != null)
        cursor.moveToFirst();

    ContactList contact = new ContactList(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), Uri.parse(cursor.getString(5)));
    db.close();
    cursor.close();
    return contact;
}

//CRUD: UPDATE

public int updateContact(ContactList contact){
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(NAME, contact.getName());
    values.put(PHONE, contact.getPhone());
    values.put(EMAIL, contact.getEmail());
    values.put(ADDRESS, contact.getAddress());
    values.put(IMAGEURI, contact.getImageUri().toString());

    return db.update(TABLE, values, ID + "=?", new String[] { String.valueOf(contact.getId()) });
}

//CRUD: DELETE

public void deleteContact(ContactList contact){
    SQLiteDatabase db = getWritableDatabase();

    db.delete(TABLE, ID + "=?", new String[] {String.valueOf(contact.getId())});
    db.close();
}

public int getContactsCount(){
    SQLiteDatabase db = getReadableDatabase();

    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE, null);
    int count = cursor.getCount();
    db.close();
    cursor.close();
    return count;
}

public List<ContactList> getAllContacts(){
    List<ContactList> contacts = new ArrayList<ContactList>();

    SQLiteDatabase db = getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE, null);

    if(cursor.moveToFirst()){
        do {
            ContactList contact = new ContactList(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), Uri.parse(cursor.getString(5)));
            contacts.add(contact);
        }
        while (cursor.moveToNext());
    }
    return contacts;
}
}

此外,当我删除此语句时:

values.put(IMAGEURI, contact.getImageUri().toString());

我对其他语句有疑问。

我该怎么做才能解决这个问题?请帮帮我!!

首先,您必须在每个列名称及其类型之间放置空格:

db.execSQL("CREATE TABLE " + TABLE + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + NAME + " TEXT," + PHONE + " TEXT," + EMAIL + " TEXT," + ADDRESS + " TEXT," + IMAGEURI + " TEXT " + ")");

其次,在您的 Cursor 中:

Cursor cursor = db.query(TABLE, new String[] {ID, NAME, PHONE, EMAIL, ADDRESS, /*IMAGEURI*/}, ID + "=?", new String[] {String.valueOf(id)},null,null,null,null );

你写了/*IMAGEURI*/!所以很正常,后来有些行, cursor.getString(5) 不存在,它也崩溃了:

ContactList contact = new ContactList(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), Uri.parse(cursor.getString(5)));

最后确保您的数据模型已更新。 如果在之前的调试会话期间创建数据库时刚刚添加 imageUri 列,它将崩溃。您很好地重写了 onUpgrade 方法来删​​除现有表,但我希望您也没有忘记增加数据库版本值 ;-)