Android笔记(四十二) Android中的数据存储——SQLite(四)update
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的更多相关文章
- Android笔记(四十) Android中的数据存储——SQLite(二) insert
准备工作: 我们模拟一个注册的页面,先看UI 我们需要创建一个数据库:user,数据库包含表user,user表包含字段id.username.password.mobilephone MainAct ...
- Android笔记(四十四) Android中的数据存储——SQLite(六)整合
实现注册.登录.注销账户 MainActivity.java package cn.lixyz.activity; import android.app.Activity; import androi ...
- Android笔记(六十五) android中的动画——属性动画(propertyanimation)
补间动画只能定义起始和结束两个帧在“透明度”.“旋转”.“倾斜”.“位移”4个方面的变化,逐帧动画也只能是播放多个图片,无法满足我们日常复杂的动画需求,所以谷歌在3.0开始,推出了属性动画(prope ...
- Android笔记(六十二)网络框架volley
什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...
- Android笔记(七十五) Android中的图片压缩
这几天在做图记的时候遇第一次遇到了OOM,好激动~~ 追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那 ...
- Android笔记(七十二) Style和Theme
我们尝尝需要使用setText.setColor.setTextSize等属性来设置控件的样式,但是每个控件都需要设置这些属性,工作量无疑是巨大的,并且后期维护起来也不方便. Style Androi ...
- Android笔记(六十六) android中的动画——XML文件定义属性动画
除了直接在java代码中定义动画之外,还可以使用xml文件定义动画,以便重用. 如果想要使用XML来编写动画,首先要在res目录下面新建一个animator文件夹,所有属性动画的XML文件都应该存放在 ...
- Android笔记(四十一) Android中的数据存储——SQLite(三)select
SQLite 通过query实现查询,它通过一系列参数来定义查询条件. 各参数说明: query()方法参数 对应sql部分 描述 table from table_name 表名称 colums s ...
- Android笔记(十二)AndroidManiFest.xml
AndroidManiFest.xml清单文件是每个Android项目所必须的,它是整个Android应用的全局描述文件.AndroidManiFest.xml清单文件说明了该应用的名称.所使用的图标 ...
随机推荐
- HTTP和HTTPS概念
HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法. HTTPS(Hypertext Transfer ...
- [LeetCode] 202. Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- OpenStack-keystone命令行
Keystone简介 Keystone(OpenStack Identity Service)是OpenStack框架中,负责身份验证.服务规则和服务令牌的功能, 它实现了OpenStack的Iden ...
- 八、spring生命周期之BeanPostProcessor
BeanPostProcessor我们一般称为Bean的后置处理器,它与我们前面介绍的InitialingBean.init-method等一样,都是在bean的初始化时被调用,具体的用法我们在举例中 ...
- php_MVC实现步骤一
1.Match 基本实现 功能,比赛列表,球队信息 数据表初始化: 比赛列表: 球队一 比分 球队二 比赛时间 韩国 1:2 澳大利亚 15-01-31 17:00 从SQL思考,如何完成上面的查询: ...
- leetocode 207 课程表
解题思路: 本题可约化为:课程安排图是否是 有向无环图(DAG).即课程间规定了前置条件,但不能构成任何环路,否则课程前置条件将不成立. 思路是通过 拓扑排序 判断此课程安排图是否是 有向无环图(DA ...
- Miller_Rabin算法_单个素数检测_启发式算法
/** Miller_Rabin 算法进行素数测试 快速判断一个<2^63的数是不是素数,主要是根据费马小定理 */ #define ll __int128 ; ///随机化算法判定次数 ll ...
- String初解
String 类型是不可变的对象,因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然后有一次看到一个代码段,如下: 这段代码返回的是ture,当时以为是f ...
- PMBOK(第六版) PMP备考知识总汇!
记录本人学习PMBOK第六版的学习笔记. 备考知识总汇! PMBOK序章 PMP备考指南之相关事项介绍 PMP备考指南之第一章:引论 PMP备考指南之第二章:项目运作环境 PMP备考指南之第三章:项目 ...