![](/img/trans.png)
[英]How to update a certain value in my database based on the value of another child?
[英]How to update my string value into database
有一個數據庫,其表名status
,列名為column1
。 我需要更新。
我在Activity1
有一個字符串值
String check = "value";
我已經將此值傳遞到我的DBHelper
。
dbhelper = new DBHelper(this);
dbhelper.sample(check);
我在DBHelpe
r.here中獲得了此值。 像這樣
public void sample(String prms){
Log.d("sucess",prms);
}
現在我需要如何將String prms
更新到我的數據庫columnname column1
我已經嘗試過這樣
public void sample( String prms) {
Log.d("DBHELPER SUCCESS", prms);
try{
SQLiteDatabase db1 = this.getWritableDatabase();
db1.execSQL("update appstatus SET status = '"+prms+"' WHERE id = 1 ");
}catch(Exception e){
System.out.println("GET SAMPLE VALUE"+e);
}
}
我的語法有什么問題? 如何實現呢?
它顯示異常為
02-28 12:09:45.604: I/System.out(4975): GET SAMPLE VALUEandroid.database.sqlite.SQLiteException: table report already exists (code 1): , while compiling: create table report(level TEXT, topic TEXT, start TEXT, end TEXT, date TEXT)
rawQuery()
實際上並未運行SQL。 使用execSQL()
代替:
db.execSQL("update status SET column1 = '"+prms+"' ");
(請參閱使用查詢字符串在Android SQLiteDatabase中執行插入/更新/刪除的正確方法是什么,以了解更多rawQuery()
和execSQL()
如何在rawQuery()
工作的信息。)
您可以像這樣直接Execute
此查詢:
db.execSQL("update status SET column1 = '"+prms+"' ", null);
您應該替換您的代碼
Cursor cursor = db.execSQL("update status SET column1 = '"+prms+"' ", null);
用
db.execSQL("update status SET column1 = '"+prms+"' ", null);
無效的execSQL(String sql)
執行一個SQL語句不是SELECT或返回數據的任何其他SQL語句。
它無法返回任何數據(例如受影響的行數)。 相反,建議您盡可能使用insert(String,String,ContentValues , update(String,ContentValues,String,String [])等。
db.execSQL(“更新狀態SET column1 ='” + prms +“'”,null);
更新您的以下行代碼,
Cursor cursor = db.rawQuery("update status SET column1 = '"+prms+"' ", null);
到這條線
db.execSQL( "update status SET column1 = '"+prms+"' " );
因此您的方法將如下所示:
public void sample(String prms)
{
Log.d("sucess",prms);
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("update status SET column1 = '"+prms+"'WHERE id = '5' " ); // remove second param from here.
}
警告:此查詢將更新Column1的所有行; 您應該包括where
子句
遵循此代碼,它返回編號。 更新時影響的行數。
SQLiteHelper helper;
public int updateName(String oldName,String newName)
{
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(this.NAME, newName);
String [] whereArgs={oldName};
int count = db.update(helper.TABLE_NAME, values, helper.NAME+" =? ",whereArgs);
return count;
}
其中helper是擴展SQLiteOpenHelper
的類的對象,如下面的代碼。
static class SQLiteHelper extends SQLiteOpenHelper
{
....////
private static final String DATABASE_NAME = "databaseName";
private static final String TABLE_NAME = "TABLENAME";
private static final int DATABASE_VERSION = 1;
private static final String UID = "_id";
private static final String NAME = "Name";
private static final String PASSWORD = "Password";
private static final String CREATE_TABLE ="CREATE TABLE "+ TABLE_NAME +"(" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT," + NAME + " VARCHAR(255)," + PASSWORD + " VARCHAR(255));";
public SQLiteHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
Message.Message(context, "Constructor Called");
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
Message.Message(context, "OnCreate Called");
db.execSQL(CREATE_TABLE);
} catch (SQLException e) {
// TODO Auto-generated catch block
Message.Message(context, "" + e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
try {
Message.Message(context, "onUpgrade Called");
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
// TODO Auto-generated catch block
Message.Message(context, ""+e);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.