当我尝试在sqlite上更新我的数据时会发生此错误
07-04 07:20:52.289: E/AndroidRuntime(2633): android.database.sqlite.SQLiteException: no such column: nick (code 1): , while compiling: UPDATE Contacts SET phone=?,nick=?,_id=?,name=? WHERE _id=2
我不明白为什么会出现这个错误。我认为变量不会转移到更新查询
这是我的更新查询
public void update(int id, String name, String phone, String nick) throws SQLException{
ContentValues cvUpdate=new ContentValues();
cvUpdate.put(KEY_ID,id);
cvUpdate.put(KEY_NAME,name);
cvUpdate.put(KEY_PHONE,phone);
cvUpdate.put(KEY_NICK,nick);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ID+"="+id, null);
我从edittext获取值,并将这些值放入mainactivity中的字符串和int变量中,然后我编写此代码:
info.open();
info.update(intid, contactname, contactphone, contactnick);
info.close();
这是数据库创建 公共类contacts_nick {
//private static final String TAG = contacts_nick.class.getSimpleName();
public static final String KEY_ID="_id";
public static final String KEY_NAME="name";
public static final String KEY_PHONE="phone";
public static final String KEY_NICK="nick";
private static final String DATABASE_NAME="mydb";
private static final String DATABASE_TABLE="Contacts";
private static final int DATABASE_VERSION=1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
//To create db
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " +DATABASE_TABLE+ " (" +KEY_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "+KEY_NAME+" TEXT, "+KEY_PHONE+" TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public contacts_nick(Context c){
ourContext=c;
}
public contacts_nick open() throws SQLException{
ourHelper=new DbHelper(ourContext);
ourDatabase=ourHelper.getWritableDatabase();
return this;
}
public contacts_nick open_read() throws SQLException{
ourHelper=new DbHelper(ourContext);
ourDatabase=ourHelper.getReadableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
答案 0 :(得分:1)
您的问题是您没有将Nick列添加到数据库中。
答案 1 :(得分:0)
在数据库类中替换此行
db.execSQL("CREATE TABLE " +DATABASE_TABLE+ " (" +KEY_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "+KEY_NAME+" TEXT, "+KEY_PHONE+" TEXT, "+KEY_NICK+" TEXT);");