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. wordpress对使用的国外主题进行本地汉化

    wordpress有非常多优秀与专业的主题,当然大多数是非中文的 这些主题本身总会有些无法通过wordpress admin后台来配置的在页面上的英文输出 此时你可以去对应的代码去改掉那些输出,不过这 ...

  2. Html5学习进阶一 视频和音频

    HTML5 规定了一种通过 video 元素来包含视频的标准方法. 视频格式 当前,video 元素支持两种视频格式:   Internet Explorer Firefox 3.5 Opera 10 ...

  3. centos 安装使用smb

    http://blog.csdn.net/edu_enth/article/details/52964295

  4. UVA 10330 Power Transmission

    题意:懒得打了.LUCKY CAT 里有 http://163.32.78.26/homework/q10330.htm 第一个网络流题目.每个节点都有一个容量值.需要拆点.拆成i - > i ...

  5. Java工厂模式浅析理解

    由于本人缺乏工作经验,本篇文章作为随笔,只是对工厂模式有一个简单的认识 工厂模式分为以下三种: 1:简单工厂(Simple Factory).2:工厂方法(Factory Method).3:抽象工厂 ...

  6. shiro多realm验证之——shiro实现不同身份使用不同Realm进行验证(转)

    转自: http://blog.csdn.net/xiangwanpeng/article/details/54802509 (使用特定的realm实现特定的验证) 假设现在有这样一种需求:存在两张表 ...

  7. android的百度地图开发(一)

    1,注册百度开发者账号 2,申请key  ,注意开发版SH和发布版的SH  获取开发版SHA1: 输入命令:keytool -list -v -keystore debug.keystore,回车输入 ...

  8. 【bugfree】安装

    我用的是WIN8系统 首先要安装XAMPP,开始里面的Apache和MySQL服务. 在运行Apache服务时报错: ----------------------------------------- ...

  9. hdu 畅通工程系列题目

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 并查集水. #include <stdio.h> #include <iost ...

  10. Laravel使用Eloquent ORM操作数据库

    1.定义模型 <?php namespace App; use Illuminate\Database\Eloquent\Model; class Flight extends Model{ p ...