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

 

Android笔记(四十二) Android中的数据存储——SQLite(四)update的更多相关文章

  1. Android笔记(四十) Android中的数据存储——SQLite(二) insert

    准备工作: 我们模拟一个注册的页面,先看UI 我们需要创建一个数据库:user,数据库包含表user,user表包含字段id.username.password.mobilephone MainAct ...

  2. Android笔记(四十四) Android中的数据存储——SQLite(六)整合

    实现注册.登录.注销账户 MainActivity.java package cn.lixyz.activity; import android.app.Activity; import androi ...

  3. Android笔记(六十五) android中的动画——属性动画(propertyanimation)

    补间动画只能定义起始和结束两个帧在“透明度”.“旋转”.“倾斜”.“位移”4个方面的变化,逐帧动画也只能是播放多个图片,无法满足我们日常复杂的动画需求,所以谷歌在3.0开始,推出了属性动画(prope ...

  4. Android笔记(六十二)网络框架volley

    什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...

  5. Android笔记(七十五) Android中的图片压缩

    这几天在做图记的时候遇第一次遇到了OOM,好激动~~ 追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那 ...

  6. Android笔记(七十二) Style和Theme

    我们尝尝需要使用setText.setColor.setTextSize等属性来设置控件的样式,但是每个控件都需要设置这些属性,工作量无疑是巨大的,并且后期维护起来也不方便. Style Androi ...

  7. Android笔记(六十六) android中的动画——XML文件定义属性动画

    除了直接在java代码中定义动画之外,还可以使用xml文件定义动画,以便重用. 如果想要使用XML来编写动画,首先要在res目录下面新建一个animator文件夹,所有属性动画的XML文件都应该存放在 ...

  8. Android笔记(四十一) Android中的数据存储——SQLite(三)select

    SQLite 通过query实现查询,它通过一系列参数来定义查询条件. 各参数说明: query()方法参数 对应sql部分 描述 table from table_name 表名称 colums s ...

  9. Android笔记(十二)AndroidManiFest.xml

    AndroidManiFest.xml清单文件是每个Android项目所必须的,它是整个Android应用的全局描述文件.AndroidManiFest.xml清单文件说明了该应用的名称.所使用的图标 ...

随机推荐

  1. t4模版快速入门

    语法 主要包括指令.文本块.控制块. 1.指令 指令主要包括template, output, assembly, import, include等类型,用以告诉T4引擎如何编译和运行一个模板.这些指 ...

  2. BDD介绍

    TDD: TDD(Test-Drivern Development)测试驱动开发,是敏捷开发中的一项核心实践和技术,也是一种设计方法论.TDD的原理是在开发功能代码之前,先编写单元测试用例代码,测试代 ...

  3. 必须要注意的 C++ 动态内存资源管理(一)——视资源为对象

    必须要注意的 C++ 动态内存资源管理(一)——视资源为对象 一.前言         所谓资源就是,一旦你用了它,将来必须还给系统.如果不这样,糟糕的事情就会发生.C++ 程序中最常见使用的资源就是 ...

  4. Docker安装ES和Kibana

    1.ES安装 1.下载ES镜像问题 docker pull elasticsearch 2.运行ES docker run -it --name elasticsearch -d -p 9200:92 ...

  5. Docker CentOS / Ubuntu容器开启 SSH 服务

    Docker CentOS / Ubuntu容器开启 SSH 服务 在CentOS容器内执行 yum install passwd openssl openssh-server -y # Ubuntu ...

  6. 转 zabbix debug and zabbix使用percona插件监控mysql

    ########## https://www.cnblogs.com/keithtt/p/8542987.html zabbix使用percona插件监控mysql   1.添加percona仓库. ...

  7. [LeetCode] 257. Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  8. Ackermann Steering System

    Source : https://www.hotrod.com/articles/ctrp-0407-ackermann-steering-system/ Tuning Your Steering S ...

  9. 07 XML知识点--学习笔记

    1.XML概念:Extensible Markup Language 可扩展标记语言 可扩展:标签都是自定义的. <user> <student> 功能 * 存储数据 1. 配 ...

  10. 备份数据库中的某个表的数据报错Statement violates GTID consistency

    1.错误描述 执行CREATE TABLE tig_pairs_20190521 AS SELECT *FROM tig_pairs报错: 1 queries executed, 0 success, ...