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. openresty开发系列32--openresty执行流程之1初始化阶段

    openresty开发系列32--openresty执行流程之初始化阶段 一)初始化阶段 1)init_by_lua   init_by_lua_block     init_by_lua_file语 ...

  2. C# redis客户端帮助类

    需要在NuGet里面引用ServiceStack.Redis using ServiceStack.Redis; using ServiceStack.Redis.Generic; using Sys ...

  3. Python3基础 yield 在无返回值的函数中的简单示例

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  4. tqdm学习-一个快速,可扩展的Python和CLI进度条

    参考:https://pypi.org/project/tqdm/ 1.安装: (base) userdeMacBook-Pro:~ user$ conda activate deeplearning ...

  5. pytorch torch.backends.cudnn设置作用

    cuDNN使用非确定性算法,并且可以使用torch.backends.cudnn.enabled = False来进行禁用 如果设置为torch.backends.cudnn.enabled =Tru ...

  6. 001-mac搭建Python开发环境、Anaconda、zsh兼容

    一.概述 mac下搭建python环境推荐使用Anaconda+Pycharm. 1.1.Anaconda Anaconda是一个免费开源的Python和R语言的发行版本,用于计算科学(数据科学.机器 ...

  7. Spring整合Redis,并配置Jedis连接池

    目录 只言片语 创建redis连接池的配置文件 单机版 spring整合redis(使用JedisPool) 项目中使用示例 集群版 spring整合redis(使用JedisCluster) 项目中 ...

  8. 【python基础】argparse模块

    参考 1. argumentparser-objects: 2. argparse.ArgumentParser()用法解析:

  9. [LeetCode] 61. Rotate List 旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  10. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...