更新android数据库中的值

发布于 2024-11-06 12:24:39 字数 671 浏览 1 评论 0原文

我的应用程序中有一个设置选项卡,它通过用户的 edittext 获取 int 值并将其存储到数据库中。当用户现在想要更改设置时,应在数据库中更新旧设置。我如何为此编写数据库代码。

下面是我将值保存在数据库中的代码:

DatabaseHelper dha = new DatabaseHelper(this);
           SQLiteDatabase db = dha.getWritableDatabase();
           Log.d("","Done with database Sqlite");
           ContentValues cv = new ContentValues();
           EditText editText2 = (EditText) this.findViewById(R.id.editText1);
           setTime = editText2.getText().toString(); 
           Log.d(setTime, "This is the setTime value");
           cv.put(DatabaseHelper.TIME, setTime);
           db.insert("finalP", DatabaseHelper.TIME, cv);
           db.close();

I have a settings tab in my application which takes an int value through edittext from user and stores it into the database. When the user now wants to change the setting the old setting should be updated in the database. How do I write the database code for that.

Below is my code to save the value in db:

DatabaseHelper dha = new DatabaseHelper(this);
           SQLiteDatabase db = dha.getWritableDatabase();
           Log.d("","Done with database Sqlite");
           ContentValues cv = new ContentValues();
           EditText editText2 = (EditText) this.findViewById(R.id.editText1);
           setTime = editText2.getText().toString(); 
           Log.d(setTime, "This is the setTime value");
           cv.put(DatabaseHelper.TIME, setTime);
           db.insert("finalP", DatabaseHelper.TIME, cv);
           db.close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2024-11-13 12:24:39

你应该做这样的事情:

// Create content values that contains the name of the column you want to update and the value you want to assign to it 
ContentValues cv = new ContentValues();
cv.put("my_column", "5");

String where = "my_column=?"; // The where clause to identify which columns to update.
String[] value = { "2" }; // The value for the where clause.

// Update the database (all columns in TABLE_NAME where my_column has a value of 2 will be changed to 5)
db.update(TABLE_NAME, cv, where, value);

You should do something like this:

// Create content values that contains the name of the column you want to update and the value you want to assign to it 
ContentValues cv = new ContentValues();
cv.put("my_column", "5");

String where = "my_column=?"; // The where clause to identify which columns to update.
String[] value = { "2" }; // The value for the where clause.

// Update the database (all columns in TABLE_NAME where my_column has a value of 2 will be changed to 5)
db.update(TABLE_NAME, cv, where, value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文