我希望更新SQLite数据库中的一行。我想知道的是,我可以在没有用户输入rowId的情况下更新行吗?我的代码目前需要将rowId传递给DBAdapter,但我想消除这一点,只使用其他两个变量(item和lite)更新行。这甚至可能吗?
这是我在DBAdapter类中的更新功能
//---updates a record---
public boolean updateRecord(long rowId, String item, String litres)
{
ContentValues args = new ContentValues();
args.put(KEY_ITEM, item);
args.put(KEY_LITRES, litres);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
这是我的更新课程,
package com.example.rory.dbtest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.pinchtapzoom.R;
public class Update extends Activity {
public EditText updateItem;
public EditText updateLitres;
Button updateBtn;
DBAdapter db = new DBAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
updateBtn = (Button)findViewById(R.id.updateBtn);
updateItem = (EditText)findViewById(R.id.updateItem);
updateLitres = (EditText)findViewById(R.id.updateLitres);
}
public void onClick(View v)
{
String item = updateItem.getText().toString();
String litres = updateLitres.getText().toString();
db.updateRecord(rowId, String item, String litres);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.update, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//returning to the first activity, when here this calls the running functions ie. printing records
public void viewAssignments(View v)
{
Intent i = new Intent(this, MyActivity.class);
startActivity(i);
}
}
答案 0 :(得分:0)
我在项目中使用这部分代码。
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
String sql = "UPDATE table_name SET product_qty = \'"+values.getAsString("product_qty")+"\' Where row_id = \'"+selectionArgs[0]+"\'"+" ; ";
Log.e("sql", sql) ;
db_cc.getWritableDatabase().execSQL(sql);
return 0;
}
我在selectionArgs数组的[0]位置传递我的row_id,在我的情况下,我想更新product_qty,所以我通过使用ContentValues传递了该值。
答案 1 :(得分:0)
您可以使RAW更新变得如此简单:
public boolean updateRecord(String item, String litres)
{
return db.execSQL("UPDATE "+DATABASE_TABLE+" SET "+KEY_LITRES+"='"+litres+"' WHERE "+KEY_ITEM+"='"+item+"'")
}
但这将使用KEY_ITEM = item 条件更新所有行或... 您可以添加另一个参数,如下所示:
public boolean updateRecord(String item, String litres, String newLitres)
{
return db.execSQL("UPDATE "+DATABASE_TABLE+" SET "+KEY_LITRES+"='"+newLitres+"' WHERE "+KEY_ITEM+"='"+item+"' AND "+KEY_LITRES+"='"+litres+"'")
}
这将只更新具有项AND升的给定参数的行 但是如果你有2行或更多行具有相同的项目和升,那么就会出现问题。
我仍然建议使用唯一键或主键。