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. c# winform访问 带有windows身份验证的webservice

    1 将webservice设置为windows身份验证iis10中,要确认已安装windows身份验证在 控制面板 - >打开或关闭Windows功能 - >万维网服务 - >安全性 ...

  2. Linux 常用操作和命令

    腾讯云部署 java web 环境:https://blog.csdn.net/niceLiuSir/article/details/78879844 Tomcat部署和配置:https://blog ...

  3. 爬虫urllib2中Handler处理器和自定义Opener

    Handler处理器 和 自定义Opener opener是 urllib2.OpenerDirector 的实例,urlopen是一个特殊的opener(也就是模块已经构建好的). 但是基本的url ...

  4. 强化学习——如何提升样本效率 ( DeepMind 综述深度强化学习:智能体和人类相似度竟然如此高!)

    强化学习     如何提升样本效率 参考文章: https://news.html5.qq.com/article?ch=901201&tabId=0&tagId=0&docI ...

  5. Qt QtXml读取xml文件内容

    Qt QtXml读取xml文件内容 xml文件内容 <?xml version="1.0" encoding="UTF-8"?> <YG_RT ...

  6. Nginx - 代理后端通过域名访问

    目录- 前言- Nginx 根据域名反向代理- Nginx proxy_pass 关于 '/' 的作用 1. 前言 接到一个需求,通过nginx 代理互联网上某一个页面,刚开始的时候觉得很简单的,直接 ...

  7. [转帖]Chrome用户请尽快更新:谷歌发现两个严重的零日漏洞

    Chrome用户请尽快更新:谷歌发现两个严重的零日漏洞 https://news.cnblogs.com/n/647075/ 强烈建议:Chrome 用户请尽快升级浏览器!在谷歌今天发布的紧急补丁程序 ...

  8. python 之 Django框架(Django框架简介、视图装饰器、request对象、Response对象)

    12.33 Django框架简介: MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器( ...

  9. 『Go基础』第5节 第一个Go程序

    本节我们来学习写一个最简单的Go程序: 打印 Hello Go. 第一个Go程序, 只要跟着做, 留下个印象就可以. 用Goland创建一个 hello_go.go 文件(后缀为 .go ). 文件内 ...

  10. 在Firefox中操作iframe的一个小问题

    在做一个 Web 的打印功能时,需要将被打印的文档写到 iframe 的 document 中. <!doctype html> <html lang="en"& ...