java - Android Studio sqlite 更新
问题描述
我制作了一个 sqlite 表,可以在其中为我的 Trip 应用程序插入数据。我正在尝试更新行,但我无法弄清楚。我已经搜索了很多,但我在更新部分做错了,我不知道是什么。
public static final String DATABASE_NAME = "triptracker.db";
public static final String TABLE_NAME = "trip_table";
public static final String COL_1 = "trip_id";
public static final String COL_2 = "trip_title";
public static final String COL_3 = "trip_description";
public static final String COL_4 = "trip_image";
public static final String COL_5 = "trip_location";
当我插入数据时:
public boolean insertData(String title, String description, String location) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, title);
contentValues.put(COL_3, description);
// contentValues.put(COL_4, image);
contentValues.put(COL_5, location);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
//When I'm updating the database
public boolean update(String title, String description, String location){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, title);
contentValues.put(COL_3, description);
contentValues.put(COL_5, location);
db.update(TABLE_NAME,contentValues, "location=?", new String[]{location} );
return true;
}
// I need to press a button in order to save the data
saveItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if(!editLocation.getText().toString().equals("") && !editTitle.getText().toString().equals("") && !editDescription.getText().toString().equals("")){
boolean insert = myDb.update(editLocation.getText().toString(),
editDescription.getText().toString(),
editTitle.getText().toString());
}
else {
Toast.makeText(singlemomentactivity.this, "Your moment is not added ", Toast.LENGTH_LONG);
}
解决方案
试试这个,也许关键参考是问题所在。但是在你尝试这个之后让我知道。
public static final String DATABASE_NAME = "triptracker.db";
public static final String TABLE_NAME = "trip_table";
public static final String COL_1 = "trip_id";
public static final String COL_2 = "trip_title";
public static final String COL_3 = "trip_description";
public static final String COL_4 = "trip_image";
public static final String COL_5 = "trip_location";
public static final String COL_6 = "ID";
//oncreate method of SQLite DB
@Override
public void onCreate(SQLiteDatabase db) {
//Create Search Table
String EXAMPLE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
+ COL_6 + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COL_1 + " TEXT UNIQUE,"
+ COL_2 + " TEXT,"
+ COL_3 + " TEXT,"
+ COL_4 + " TEXT,"
+ COL_5 + " TEXT,"
+ ")";
db.execSQL(EXAMPLE_TABLE);
}
public boolean update(String title, String description, String location){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, title);
contentValues.put(COL_3, description);
contentValues.put(COL_5, location);
//code for only if location is updated
if(//location needs to be updated){
ContentValues values = new ContentValues();
values.put(COL_5,location);
db.update(SarathCityLocalDatabase.EXAMPLE_TABLE,values,COL_6 + " = ?",new String[]{location});
}
return true;
}