我正在制作这个示例应用程序,它将插入,更新,删除和检索数据库中的数据。现在我面临的问题是如何更新记录。我知道我必须通过Id,但是如何。
MainActivity.java:
public class MainActivity extends Activity {
Button btnSubmit, btnUpdate, btnDelete;
EditText UserName, FirstName, LastName, txtPassword;
RunDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new RunDatabase(this);
UserName = (EditText) findViewById(R.id.User_Name);
FirstName = (EditText) findViewById(R.id.First_Name);
LastName = (EditText) findViewById(R.id.Last_Name);
txtPassword = (EditText) findViewById(R.id.Password);
btnSubmit = (Button) findViewById(R.id.btn_Submit);
btnSubmit.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
addRow();
}
});
btnUpdate = (Button) findViewById(R.id.btn_Update);
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateRow();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void addRow()
{
try
{
db.addRow
(
UserName.getText().toString(),
FirstName.getText().toString(),
LastName.getText().toString(),
txtPassword.getText().toString()
);
// remove all user input from the Activity
emptyFormFields();
}
catch (Exception e)
{
Log.e("Add Error", e.toString());
e.printStackTrace();
}
}
private void updateRow()
{
try
{
db.updateRow
(
UserName.getText().toString(),
FirstName.getText().toString(),
LastName.getText().toString(),
txtPassword.getText().toString()
);
emptyFormFields();
}
catch (Exception e)
{
Log.e("Update Error", e.toString());
e.printStackTrace();
}
}
private void emptyFormFields()
{
User_Id.setText("");
FirstName.setText("");
LastName.setText("");
UserName.setText("");
txtPassword.setText("");
}
}
RunDatabase.java:
public class RunDatabase {
Context context;
private SQLiteDatabase db; // a reference to the database manager class.
private final String DB_NAME = "Records"; // the name of our database
private final int DB_VERSION = 1; // the version of the database
// the names for our database columns
private final String TABLE_NAME = "tblRecords";
private final String TABLE_ROW_ID = "id";
private final String TABLE_ROW_ONE = "UserName";
private final String TABLE_ROW_TWO = "FirstName";
private final String TABLE_ROW_THREE = "LastName";
private final String TABLE_ROW_FOUR = "Password";
// the beginnings our SQLiteOpenHelper class
public void onCreate(SQLiteDatabase db)
{
String newTableQueryString =
"create table " +
TABLE_NAME +
" (" +
TABLE_ROW_ID + " integer primary key autoincrement not null, " +
TABLE_ROW_ONE + " text, " +
TABLE_ROW_TWO + " text, " +
TABLE_ROW_THREE + " text, " +
TABLE_ROW_FOUR + " text" +
");";
System.out.println(newTableQueryString);
// execute the query string to the database.
db.execSQL(newTableQueryString);
}
public RunDatabase(Context context)
{
this.context = context;
// create or open the database
CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
this.db = helper.getWritableDatabase();
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void addRow(String rowStringOne, String rowStringTwo, String rowStringThree, String rowStringFour)
{
// this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);
values.put(TABLE_ROW_THREE, rowStringThree);
values.put(TABLE_ROW_FOUR, rowStringFour);
try
{
db.insert(TABLE_NAME, null, values);
}
catch(Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace(); // prints the stack trace to the log
}
}
public void updateRow(long rowID, String rowStringOne, String rowStringTwo, String rowStringThree, String rowStringFour)
{
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);
values.put(TABLE_ROW_THREE, rowStringThree);
values.put(TABLE_ROW_FOUR, rowStringFour);
try {db.update(TABLE_NAME, values, TABLE_ROW_ID + "=" + rowID, null);}
catch (Exception e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION.
// OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE.
}
}
}
答案 0 :(得分:0)
你可以尝试这样 -
db.update(TABLE_NAME, values, TABLE_ROW_ID + "=?", String[]{rowID});
答案 1 :(得分:0)
1。 在您的创建表字符串
中String newTableQueryString =
"create table " +
TABLE_NAME +
" (" +
TABLE_ROW_ID + " integer primary key autoincrement not null, " +
TABLE_ROW_ONE + " text, " +
TABLE_ROW_TWO + " text, " +
TABLE_ROW_THREE + " text, " +
TABLE_ROW_FOUR + " text" +
");";
ROW_ID是一个整数,所以在函数updateRow()
中,rowID
参数的数据类型应该是整数,它在“where”子句中传递。所以应该改变
- > public void updateRow(long rowID, String rowStringOne,...
2。 要识别您添加的行,您需要拥有一个主键,您可以根据该条目识别该主键。
自动增量整数有助于确保数据库每次都有一个新行,
但您需要的主键应该是您将要知道的区分条目的东西。
可以尝试一个派生密钥,它将是你的数据库中的另一个列,插入相同的添加行将是:
String customPrimaryKey="UserName.getText().toString()+FirstName.getText().toString()+txtPassword.getText().toString()";
db.updateRow
(
UserName.getText().toString(),
FirstName.getText().toString(),
LastName.getText().toString(),
txtPassword.getText().toString(),
// add one more value for a particular entry-a primary key for You to identify that row
customPrimaryKey
);
因此,您可以确定要更新的行,并在where子句的updateRow()
参数中传递相同的内容