我一直收到错误,表示无法找到表格中的列。无法弄清楚为什么会这样。列名称似乎拼写正确。
我收到此错误:
06-11 21:27:02.701 3703-3703/com.example.luca.contatti E/SQLiteLog﹕ (1) table Contatti has no column named imageUri
这是我的DatabaseHandler页面:
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());
我对其他陈述有疑问。
我可以做些什么来解决这个问题?请帮帮我!!
答案 0 :(得分:3)
首先,您必须在每个列名称及其类型之间放置空格:
db.execSQL("CREATE TABLE " + TABLE + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + NAME + " TEXT," + PHONE + " TEXT," + EMAIL + " TEXT," + ADDRESS + " TEXT," + IMAGEURI + " TEXT " + ")");
其次,在你的光标中:
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)));
最后确保您的Datamodel已更新。
如果您刚刚在先前的调试会话期间创建了数据库时添加了imageUri列,则它将崩溃。你很好地覆盖了onUpgrade
方法来删除现有的表,但我希望你也不会忘记增加数据库版本值; - )