0

当我尝试在 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();
}
4

2 回答 2

1

您的问题是您没有将 Nick 列添加到数据库中。

于 2013-07-04T08:15:09.540 回答
0

在您的数据库类中替换此行

db.execSQL("CREATE TABLE " +DATABASE_TABLE+ " (" +KEY_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "+KEY_NAME+" TEXT, "+KEY_PHONE+" TEXT, "+KEY_NICK+" TEXT);");

于 2013-07-04T10:08:51.043 回答