我有一个插入方法,它首先检查该行是否可用,如果没有,那么它会尝试创建新的,或者如果可用,那么它会更新它,但是这个方法给了我else
部分中的异常,这是我的方法
public long SetSettings(String whichCol, String value) {
// TODO Auto-generated method stub
preferences = ourcontext.getSharedPreferences(filenames, 0);
String[] colum = new String[] { KEY_CAR };
Cursor c = ourDB.query(DBhelper.DATABASE_SETTINGSTABLE, colum, KEY_CAR
+ "=" + "'" + preferences.getString("selectedcar", "") + "'",
null, null, null, null);
ContentValues cvedit = new ContentValues();
cvedit.put(KEY_CAR, preferences.getString("selectedcar", ""));
cvedit.put(whichCol, value);
if (c.getCount() > 0) {
return ourDB.update(
DBhelper.DATABASE_SETTINGSTABLE,
cvedit,
KEY_CAR + "=" + "'"
+ preferences.getString("selectedcar", ""), null);
} else {
return ourDB.insert(DBhelper.DATABASE_SETTINGSTABLE, null, cvedit);
}
}
这就是我创建数据库的方式:
public static final String KEY_ID = "_id";
public static final String KEY_CAR = "car";
public static final String KEY_SET_DISENTRY_MODE = "ditance_entry_mode";
public static final String KEY_SET_DISUNIT = "distance_unit";
public static final String KEY_SET_PETROLUNIT = "petrol_unit";
public static final String KEY_SET_CONDISPLAY = "consumption_display";
public static final String KEY_SET_DATEFORMAT = "date_format";
public static final String KEY_SET_CURRENCY = "currency";
public static final String KEY_SET_STOREFILLLOC = "store_fillloc";
public static final String KEY_SET_GPSPIN = "gps_pin";
public static final String KEY_SET_KEYCLICK = "keyboard_click";
public static final String KEY_SET_KEYVIBRATE = "keyboard_vibrate";
public static final String KEY_SET_BACKUP = "backup";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_SETTINGSTABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_CAR
+ " TEXT NOT NULL, " + KEY_SET_DISENTRY_MODE + " TEXT, "
+ KEY_SET_DISUNIT + " TEXT, " + KEY_SET_PETROLUNIT + " TEXT, "
+ KEY_SET_CONDISPLAY + " TEXT, " + KEY_SET_DATEFORMAT
+ " TEXT, " + KEY_SET_CURRENCY + " TEXT, "
+ KEY_SET_STOREFILLLOC + " TEXT, " + KEY_SET_GPSPIN + " TEXT, "
+ KEY_SET_KEYCLICK + " TEXT, " + KEY_SET_KEYVIBRATE + " TEXT, "
+ KEY_SET_BACKUP + " TEXT NOT NULL);");
}
我无法弄清楚我的问题在哪里,有什么帮助吗?