update方法的四个参数:

update()方法参数 对应的sql部分 描述
table update table_name 更新的表名
values set column=xxx ContentValues
whereClause where column 修改条件
whereArgs where column = xx 修改条件的参数

看代码:

MainActivity.java

package cn.lixyz.sqlite;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private EditText name, age, paramter, originalAge, newAge;
private Button insertButton, selectButton, paramterSelect, updateButton; private SQLiteDatabase database;
private MySQLiteOpenHelper msop; public String inputSex; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findView(); msop = new MySQLiteOpenHelper(this, "user.db", null, 1);
database = msop.getReadableDatabase(); } private void findView() {
name = (EditText) findViewById(R.id.name);
age = (EditText) findViewById(R.id.age);
insertButton = (Button) findViewById(R.id.insertButton);
selectButton = (Button) findViewById(R.id.selectButton);
paramter = (EditText) findViewById(R.id.paramter);
paramterSelect = (Button) findViewById(R.id.paramterSelect);
originalAge = (EditText) findViewById(R.id.originalAge);
newAge = (EditText) findViewById(R.id.newAge);
updateButton = (Button) findViewById(R.id.updateButton);
} public void clickButton(View view) {
switch (view.getId()) {
case R.id.selectButton:
selectData();
break; case R.id.insertButton:
insertData();
break;
case R.id.paramterSelect:
paramterSelect();
break;
case R.id.updateButton:
updateData();
break;
}
} private void updateData() {
String oldAge = originalAge.getText().toString();
String updateAge = newAge.getText().toString();
ContentValues cv = new ContentValues();
cv.put("age", updateAge);
String whereColumn = "age=?";// 修改条件
String[] whereArgs = { oldAge };
int i = database.update("user", cv, whereColumn, whereArgs);
if (1 > 0) {
Toast.makeText(MainActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "更新不成功", Toast.LENGTH_SHORT).show();
}
} private void paramterSelect() {
String inputAge = paramter.getText().toString();
Cursor c = database.rawQuery("select * from user where age>?", new String[] { inputAge });
if (c.moveToFirst()) {
do {
int id = c.getInt(c.getColumnIndex("id"));
String name = c.getString(c.getColumnIndex("name"));
String age = c.getString(c.getColumnIndex("age"));
Log.d("TTTT", "id=" + id + ",name=" + name + ",age=" + age);
} while (c.moveToNext());
}
c.close(); } private void insertData() {
String inputAge = age.getText().toString();
String inputName = name.getText().toString();
ContentValues cv = new ContentValues();
cv.put("name", inputName);
cv.put("age", inputAge);
database.insert("user", null, cv);
Toast.makeText(MainActivity.this, "插入成功", Toast.LENGTH_SHORT).show();
age.setText("");
name.setText(""); } private void selectData() {
Cursor c = database.query("user", null, null, null, null, null, null);
if (c.moveToFirst()) {
do {
int id = c.getInt(c.getColumnIndex("id"));
String name = c.getString(c.getColumnIndex("name"));
String age = c.getString(c.getColumnIndex("age"));
Log.d("TTTT", "id=" + id + ",姓名=" + name + ",年龄=" + age);
} while (c.moveToNext());
}
c.close(); } class MySQLiteOpenHelper extends SQLiteOpenHelper { private static final String CREATE_USER = "create table user(id integer primary key autoincrement,name text,age text)"; private Context mContext; public MySQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
mContext = context;
} @Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_USER);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub } }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入姓名" /> <EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入年龄" /> <Button
android:id="@+id/insertButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="点击插入" /> <Button
android:id="@+id/selectButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="点击查询" /> <TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="条件搜索" /> <EditText
android:id="@+id/paramter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="您要搜多少岁以上的?" /> <Button
android:id="@+id/paramterSelect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="点击搜索" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="update" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <EditText
android:id="@+id/originalAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="要修改的年龄" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改为" /> <EditText
android:id="@+id/newAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="新年龄" />
</LinearLayout> <Button
android:id="@+id/updateButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="UPDATE" /> </LinearLayout>

  运行结果:

  通过结果可以看到,将之前数据库内的年龄为12的修改为20

 

最新文章

  1. 堆 poj 2442
  2. Fiddler-010-网络延时应用小技巧-模拟低网速环境
  3. 六个创建模式之工厂方法模式(Factory Method Pattern)
  4. Hadoop系列之(一):Hadoop单机部署
  5. Uva(10048),最短路Floyd
  6. openjudge 大师兄,师傅被妖怪抓走啦
  7. 【maven】之使用jetty发布web项目
  8. 愉快的开始 - Windows程序设计(SDK)000
  9. CI引入外部javascript和css
  10. CSS的float与clear
  11. 170116、centos6.4下nginx和ftp搭建图片服务器
  12. ural1316 Electronic Auction
  13. C# System.Collections
  14. linux进程的一些日常处理
  15. python之路----面向对象的多态特性
  16. hadoop学习;hdfs操作;执行抛出权限异常: Permission denied;api查看源代码方法;源代码不停的向里循环;抽象类通过debug查找源代码
  17. ySQL性能优化的21个最佳实践 和 mysql使用索引
  18. cmake设置默认静态链接库
  19. [JSOI2009]电子字典 hash
  20. vue2.0--vue-router路由

热门文章

  1. vue---数据列表循环
  2. (转)tomcat 安全配置文档
  3. Flutter Android 正式打包、以及升级应用 重新打包
  4. Python3.7安装(解决ssl问题)
  5. docker卷挂载与容器内外互相拷贝数据
  6. 10分钟完成一个最最简单的BLE蓝牙接收数据的DEMO
  7. [LeetCode] 804. Unique Morse Code Words 独特的摩斯码单词
  8. 10分钟弄懂Raft算法
  9. C/c++语言开源项目总结
  10. 第16届(2019)全国大学生信息安全与对抗技术竞赛全国线下总决赛 Writeup