我尝试更改SQLite数据库的版本。我不认为我这样做是对的。我的fetchAllMeds方法由于不一致的_id列而引发错误。我的updateData应该更新了我的SQLite。我想我错过了什么。
public class DBAdapter {
public static final String ROW_ID ="_id";
public static final String KEY_DRUGNAME = "drugname";
public static final String KEY_DOSE = "dose";
public static final String KEY_FREQUENCY = "frequency";
public static final String KEY_ROUTE = "route";
public static final String KEY_REFILLDATE = "refilldate";
public static final String KEY_EXPIRATIONDATE = "expirationdate";
private static final String DATABASE_NAME = "MymedlistDB";
private static final String DATABASE_TABLE = "mymedlist";
private static final int DATABASE_VERSION = 2;
private static final String TAG = "MedDBAdapter";
private DatabaseHelper mDatabaseHelper;
private SQLiteDatabase mDb;
private final Context mCtx;
private static final String DATABASE_CREATE =
"CREATE TABLE if not exists " + DATABASE_TABLE + " (" +
ROW_ID + "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
KEY_DRUGNAME + "," +
KEY_DOSE + "," +
KEY_FREQUENCY + "," +
KEY_ROUTE + "," +
KEY_REFILLDATE + "," +
KEY_EXPIRATIONDATE + ")";
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super( context, DATABASE_NAME, null, DATABASE_VERSION );
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
db.execSQL( DATABASE_CREATE );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate( db );
}
}
public DBAdapter(Context ctx){
this.mCtx = ctx;
}
public DBAdapter open() throws SQLException{
mDatabaseHelper = new DatabaseHelper( mCtx );
mDb = mDatabaseHelper.getWritableDatabase();
return this;
}
public void close(){
if (mDatabaseHelper != null){
mDatabaseHelper.close();
}
}
public long createMed(String drugname, String dose, String frequency,
String route, String refilldate, String expirationdate){
ContentValues initialValues = new ContentValues( );
initialValues.put(KEY_DRUGNAME, drugname);
initialValues.put(KEY_DOSE, dose);
initialValues.put(KEY_FREQUENCY, frequency);
initialValues.put(KEY_ROUTE, route);
initialValues.put(KEY_REFILLDATE, refilldate );
initialValues.put(KEY_EXPIRATIONDATE, expirationdate);
return mDb.insert( DATABASE_TABLE, null, initialValues );
}
public int updateData(String drugname, String dose, String frequency,
String route, String refilldate, String expirationdate){
ContentValues medsUpdate = new ContentValues( );
medsUpdate.put(DBAdapter.KEY_DRUGNAME,drugname);
medsUpdate.put(DBAdapter.KEY_DOSE,dose);
medsUpdate.put(DBAdapter.KEY_FREQUENCY,frequency);
medsUpdate.put(DBAdapter.KEY_ROUTE,route);
medsUpdate.put(DBAdapter.KEY_REFILLDATE,refilldate);
medsUpdate.put(DBAdapter.KEY_EXPIRATIONDATE,expirationdate);
int i = mDb.update(DBAdapter.DATABASE_TABLE, medsUpdate, DBAdapter.ROW_ID, null);
return i;
}
public Cursor fetchAllMeds(){
String[] columns = new String[] {ROW_ID,KEY_DRUGNAME, KEY_DOSE, KEY_FREQUENCY, KEY_ROUTE, KEY_REFILLDATE, KEY_EXPIRATIONDATE};
Cursor mCursor = mDb.query( DATABASE_TABLE,columns,ROW_ID, null, null, null, null, null);
if (mCursor != null){
mCursor.moveToFirst();
}
return mCursor;
}
}
这是我的MedMainActivity的fetchAllMed部分:
Cursor cursor = dbcon.fetchAllMeds();
String[] from = new String[]{DBAdapter.ROW_ID, DBAdapter.KEY_DRUGNAME, DBAdapter.KEY_DOSE, DBAdapter.KEY_FREQUENCY, DBAdapter.KEY_ROUTE, DBAdapter.KEY_REFILLDATE
, DBAdapter.KEY_EXPIRATIONDATE};
int[] to = new int[]{R.id._id, R.id.drugName3, R.id.dose3, R.id.frequency3, R.id.route3, R.id.refillDate3, R.id.expirationDate3};
答案 0 :(得分:3)
请代替
ROW_ID + "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
试
ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +