我有一个数据库应用程序。最初我想把个人的细节。后来我想展示他们的形象。这是我的SQLiteOpenHeloer类---
public class FriendsDbHelper extends SQLiteOpenHelper {
public FriendsDbHelper(Context context) {
super(context, TABLE_NAME, null, FriendContract.FriendEntry.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
updateData(db,0,FriendContract.FriendEntry.DATABASE_VERSION);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
updateData(db,oldVersion,newVersion);
}
private void updateData(SQLiteDatabase db,int oldVersion,int newVersion)
{
if(oldVersion<1)
{
String SQL_CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
+ FriendContract.FriendEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ FriendContract.FriendEntry.NAME + " TEXT NOT NULL,"
+ FriendContract.FriendEntry.DESCRIPTION + " TEXT,"
+ FriendContract.FriendEntry.RESIDENCE + " TEXT,"
+ FriendContract.FriendEntry.STATUS + " TEXT );";
db.execSQL(SQL_CREATE_TABLE);
}
if(oldVersion<2)
{
String SQL_CREATE_TABLE="ALTER TABLE "+TABLE_NAME+ "ADD COLUMN "+ KEY_IMAGE +"BLOB";
db.execSQL(SQL_CREATE_TABLE);
}
}
}
这是FriendContract类---
public class FriendContract {
private FriendContract()
{
}
public static final class FriendEntry implements BaseColumns {
public static final int DATABASE_VERSION = 2;
public static final String TABLE_NAME = "friends";
public static final String _ID = BaseColumns._ID;
public static final String NAME = "name";
public static final String DESCRIPTION = "description";
public static final String RESIDENCE = "residence";
public static final String STATUS = "status";
public static final String KEY_IMAGE="image_data";
}
}
显示错误-----
SQLiteException:没有这样的列:image_data(代码1):,同时编译:SELECT _id,image_data,name,description,residence,status FROM friends
它将错误指向主要活动中的以下行---
Cursor newCursor=db.query(TABLE_NAME,new String[]{_ID,NAME,DESCRIPTION,RESIDENCE,STATUS,KEY_IMAGE},null,null,null,n
ULL,NULL);
错误是什么?请解释一下。
答案 0 :(得分:0)
问题在于: -
String SQL_CREATE_TABLE="ALTER TABLE "+TABLE_NAME+ "ADD COLUMN "+ KEY_IMAGE +"BLOB";
这解析为ALTER TABLE friendsADD COLUMN image_dataBLOB
表名和ADD之间以及列名和列类之间需要一个空格,即它应该是: -
String SQL_CREATE_TABLE="ALTER TABLE "+TABLE_NAME+ " ADD COLUMN "+ KEY_IMAGE +" BLOB"; //<<<< space added before BLOB