用于更改行的更新方法,但是它不能正常工作,结果显示像我们添加数据一样,但是我想更改表的行,但是它无法正常工作。
public int UpdateUserDetails(String name, String location, String designation ,String id) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cVals = new ContentValues();
cVals.put(KEY_ID,id);
cVals.put(KEY_NAME,name);
cVals.put(KEY_LOC, location);
cVals.put(KEY_DESG, designation);
int count = db.update(TABLE_Users, cVals, KEY_ID+ "= ?",new String[{id});
return count;
}
主要活动类
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class VladimirPutin extends AppCompatActivity {
EditText name, loc, desig, Sno;
Button saveBtn, updateBtn;
Intent intent;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Home Page");
toolbar.setNavigationContentDescription("Nevigation icon");
name = (EditText) findViewById(R.id.editname);
loc = (EditText) findViewById(R.id.editlocation);
desig = (EditText) findViewById(R.id.editdesignation);
Sno = (EditText) findViewById(R.id.serialid);
saveBtn = (Button) findViewById(R.id.add);
updateBtn = (Button) findViewById(R.id.update);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = name.getText().toString() + "\n";
String location = loc.getText().toString();
String designation = desig.getText().toString();
DataBaseHelper dbHandler = new DataBaseHelper(VladimirPutin.this);
dbHandler.insertUserDetails(username, location, designation);
intent = new Intent(VladimirPutin.this, DetailsActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Details Inserted Successfully", Toast.LENGTH_SHORT).show();
}
});
updateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String snumber=Sno.getText().toString()+"\n";
String username = name.getText().toString();
String location = loc.getText().toString();
String designation = desig.getText().toString();
DataBaseHelper dbHandler = new DataBaseHelper(VladimirPutin.this);
dbHandler.UpdateUserDetails(username, location, designation,snumber);
intent = new Intent(VladimirPutin.this, DetailsActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Details updated Successfully", Toast.LENGTH_SHORT).show();
}
});
}
}
虽然我使用Cursor
类来获取数据库中的值,但是我花了一个星期的时间在这个项目上工作,但不知道我会在哪里出错。
答案 0 :(得分:0)
用更新方法调用替换下面的行
int count = db.update(TABLE_Users,cVals,KEY_ID +“ =” + id,null);
并删除行cVals.put(KEY_ID,id);
答案 1 :(得分:-1)
首先可能是语法错误。我建议将KEY_ID+ "= ?"
更改为KEY_ID+ "=?"
。这意味着new String[{id}
是什么意思?奇怪的代码。