我编辑了设备名称,我想将编辑后的值保存到数据库,但是没有保存。
String strSQL = "UPDATE DeviceDetails SET devicename ='"+ edittext.getText().toString() +"' WHERE uuid = '"+c.getString(c.getColumnIndex("uuid"))+"'" ;
db.select(strSQL);
以下是我的数据库适配器。在此我执行查询并保存到数据库,但事实并非如此。
public class DataBaseAdapter {
public static final String KEY_ROWID = "_id";
public static final String UUID = "uuid";
public static final String DEVICENAME = "devicename";
public static final String LIGHT = "light";
public static final String ALARM ="alarm";
private static final String TAG = "DDAdapter";
private static final String DATABASE_NAME = "DeviceAdapter.db";
private static final String DATABASE_TABLE = "DeviceDetails";
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_CREATE =
"create table DeviceDetails (uuid varchar(50) primary key , " +
"devicename varchar(50) ,light varchar(50),alarm varchar(50));";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DataBaseAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@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");
Log.i("i am in onUpgrade", ""+db);
db.execSQL("DROP TABLE IF EXISTS DeviceDetails");
onCreate(db);
}
}
public DataBaseAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
public void close()
{
DBHelper.close();
}
public long insertData(String uuid,String devicename ,String light,String alarm )
{
ContentValues initialValues = new ContentValues();
initialValues.put(UUID, uuid);
initialValues.put(DEVICENAME, devicename);
initialValues.put(LIGHT, light);
initialValues.put(ALARM, alarm);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public Cursor getData(){
return db.query(DATABASE_TABLE, new String[] {UUID,DEVICENAME,LIGHT,ALARM,
}, null, null, null, null, null);
}
public Cursor select(String query) throws SQLException {
return db.rawQuery(query, null);
}
/* public void select(String query) throws SQLException {
db.execSQL(query, null);
}*/
public long updateName( String newName) {
ContentValues values = new ContentValues();
values.put(DEVICENAME, newName);
return db.replace(DATABASE_TABLE, null, values);
//(DATABASE_TABLE, values, DEVICENAME + " = '" + newName + "", null);
}
}
答案 0 :(得分:1)
e.g. you want to update the DEVICENAME where UUID is _uuid.
Then use like
public long updateName( String newName, String _uuid)
{
ContentValues values = new ContentValues();
values.put(DEVICENAME, newName);
return db.update(DATABASE_TABLE, values,UUID + "= ?",new String[] { uuid});
}
答案 1 :(得分:1)
String strSQL = "UPDATE DeviceDetails SET devicename ='"+ edittext.getText().toString() +"' WHERE uuid = '"+c.getString(c.getColumnIndex("uuid"))+"'" ;
db.rawQuery(strSQL);
试试这个
答案 2 :(得分:0)
您有两个选择
1)execSQL:
...
String strSQL = "UPDATE DeviceDetails SET devicename ='"+
edittext.getText().toString() +
"' WHERE uuid = '"+
c.getString(c.getColumnIndex("uuid"))+"'" ;
db.execSQL(String sql)
...
或者2)你可以使用update :(参见@ajit代码以获得更合适的例子,他更好地使用UUID)
public long updateName(String newName, String uuid) {
ContentValues values = new ContentValues();
values.put(UUID, uuid);
values.put(DEVICENAME, newName);
return db.update(DATABASE_TABLE, values, KEY_ROWID + "=" + rowId, null);
//(DATABASE_TABLE, values, DEVICENAME + " = '" + newName + "", null);
}
答案 3 :(得分:0)
类似于insert db,而不是db.insert(),我们必须使用db.update(),如下所示,
ContentValues initialValues = new ContentValues();
//update column where u want to update for example,
initialValues.put(DEVICENAME, devicename);
db.update((DATABASE_TABLE, initialValues ,UUID+" = "+ uuid+ "", null);