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" > <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="插入数据" /> <Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取数据" /> <Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改数据" /> <Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除数据" /> </LinearLayout>

main.java

package com.example.sqlitedemo2;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity { Button btn1;
Button btn2;
Button btn3;
Button btn4;
SQLiteDatabase db; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); db = openOrCreateDatabase("stu.db", MODE_PRIVATE, null); db.execSQL("create table if not exists tb_user(id integer primary key autoincrement,name text not null,age integer not null,sex text not null)"); btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4); btn1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put("name", "李四");
values.put("age", 20);
values.put("sex", "女");
db.insert("tb_user", null, values);
values.clear(); values.put("name", "王五");
values.put("age", 22);
values.put("sex", "男");
db.insert("tb_user", null, values);
Toast.makeText(MainActivity.this, "添加成功", 1).show();
}
}); btn2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Cursor cur = db.query("tb_user", new String[]{"name","age","sex"}, null, null, null, null, null);
while(cur.moveToNext()){
String name = cur.getString(cur.getColumnIndex("name"));
int age = cur.getInt(cur.getColumnIndex("age"));
String sex = cur.getString(cur.getColumnIndex("sex")); Log.i("stuinfo", name + "," + age + "," + sex);
}
cur.close();
} }); btn3.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
//修改数据
ContentValues values = new ContentValues();
values.put("name", "张三丰");
int result = db.update("tb_user", values, "id=?", new String[]{"1"});
if(result > 0)
Toast.makeText(MainActivity.this, "修改成功", 1).show();
else
Toast.makeText(MainActivity.this, "修改失败", 1).show();
}
}); btn4.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
//删除数据
int result = db.delete("tb_user", "id=?", new String[]{"1"});
if(result > 0)
Toast.makeText(MainActivity.this, "删除成功", 1).show();
else
Toast.makeText(MainActivity.this, "删除失败", 1).show();
}
});
} }

Android学习(十) SQLite 基于内置函数的操作方式的更多相关文章

  1. mysql学习(十二)内置函数

    常用的内置函数,常用select\ 字符串函数 contat('' , '', .....) //连接字符串 select concat(name, ' age is ', age) from per ...

  2. Prometheus监控学习笔记之PromQL 内置函数

    概述 Prometheus 提供了其它大量的内置函数,可以对时序数据进行丰富的处理.某些函数有默认的参数,例如:year(v=vector(time()) instant-vector).其中参数 v ...

  3. python学习 day013打卡 内置函数

    本节主要内容: 内置函数: 内置函数就是python给你提供的.拿来直接用的函数,比如print,input等等.截止到python版本3.6.2 python一共提供了68个内置函数.他们就是pyt ...

  4. Python学习:6.python内置函数

    Python内置函数 python内置函数,是随着python解释器运行而创建的函数,不需要重新定义,可以直接调用,那python的内置函数有哪些呢,接下来我们就了解一下python的内置函数,这些内 ...

  5. hive学习笔记之七:内置函数

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  6. python学习之路-4 内置函数和装饰器

    本篇涉及内容 内置函数 装饰器 内置函数 callable()   判断对象是否可以被调用,返回一个布尔值 1 2 3 4 5 6 7 8 9 10 11 num = 10 print(callabl ...

  7. MySQL学习笔记_7_MySQL常用内置函数

    MySQL常用内置函数 说明: 1)可以用在SELECT/UPDATE/DELETE中,及where,orderby,having中 2)在函数里将字段名作为参数,变量的值就是字段所对应的每一行的值. ...

  8. python3 第二十九章 - 内置函数之tuple相关

    Python元组包含了以下内置函数 序号 方法及描述 实例 1 len(tuple)计算元组元素个数. >>> tuple1 = ('Google', 'Baidu', 'Taoba ...

  9. Python学习(八) —— 内置函数和匿名函数

    一.递归函数 定义:在一个函数里调用这个函数本身 递归的最大深度:997 def func(n): print(n) n += 1 func(n) func(1) 测试递归最大深度 import sy ...

随机推荐

  1. 汕头市队赛 SRM 08 B

    B-3 SRM 08 描述 给长度为 n 的数列 A 和长度为 m 的数列 B,问有多少长度为 m 的数列 C 满足 输入格式 第一行俩整数 n 和 m 第二行 n 个整数 ,表示数列 A 第三行 m ...

  2. Linux将命令添加到PATH中【转】

    转自:http://www.jb51.net/LINUXjishu/150167.html 电脑中必不可少的就是操作系统.而Linux的发展非常迅速,有赶超微软的趋势.这里介绍Linux的知识,让你学 ...

  3. 【原创】SSIS-WMI 数据读取器任务:监控物理磁盘空间

    1.背景 随着时间的推移,我们的DW会越来越大,也就意味着磁盘空间会越来越小,那如果哪一天留意不当,就会造成磁盘空间的不足而导致ETL失败,最终影响我们的系统的数据正确性和使用,更严重的有可能导致物理 ...

  4. codeforces-505B

    题目连接:http://codeforces.com/contest/505/problem/B B. Mr. Kitayuta's Colorful Graph time limit per tes ...

  5. Google Kickstart Round E 2018 B. Milk Tea

    太蠢了,,,因为初始化大数据没过,丢了10分,纪念一下这个错误 大概思路:先求出让损失值最小的排列,由已生成的这些排列,通过更改某一个位置的值,生成下一个最优解,迭代最多生成m+1个最优解即可,遍历求 ...

  6. Bfs【p2385】 青铜莲花池

    题目描述--->p2385 青铜莲花池 分析 很明显了,题目告诉我们有八个方向,当然优先考虑bfs! 很简单的bfs,重点在于考虑清楚8个方向. 自己刚开始打错了 emmm 给大家上一个图.↓ ...

  7. 洛谷——P2656 采蘑菇

    P2656 采蘑菇 题目描述 小胖和ZYR要去ESQMS森林采蘑菇. ESQMS森林间有N个小树丛,M条小径,每条小径都是单向的,连接两个小树丛,上面都有一定数量的蘑菇.小胖和ZYR经过某条小径一次, ...

  8. 【字符串】Your Ride Is Here

    题目描述 It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect ...

  9. Java多线程设计模式(4)线程池模式

    前序: Thread-Per-Message Pattern,是一种对于每个命令或请求,都分配一个线程,由这个线程执行工作.它将“委托消息的一端”和“执行消息的一端”用两个不同的线程来实现.该线程模式 ...

  10. 基于CoreText的排版引擎

    前言 本人今年主要在负责猿题库iOS客户端的开发,本文旨在通过分享猿题库iOS客户端开发过程中的技术细节,达到总结和交流的目的. 这是本技术分享系列文章的第三篇.本文涉及的技术细节是:基于CoreTe ...