Android学习(十) SQLite 基于内置函数的操作方式

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();
            }
        });
    }

}
时间: 2024-08-28 19:54:01

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

Python学习(十六)内置函数,递归

1.递归 def test1(): num=int(input('输入数字')) if num%2==0: #判断输入数字是不是偶数 return True #是偶数,程序退出,返回true print('不是偶数请重新输入') return test1() #不是偶数的话继续调用自己,输入值 print(test1()) 递归的效率不高,最多递归999次 2.内置函数 python自带的函数 id()#看内存地址type()#看数据类型print()#打印input()#输入list() #转

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

本篇涉及内容 内置函数 装饰器 内置函数 callable()   判断对象是否可以被调用,返回一个布尔值 1 2 3 4 5 6 7 8 9 10 11 num = 10 print(callable(num))   # num不能够被调用,返回False    def f1():     print("name")    print(callable(f1))     # f1可以被调用,返回True    # 输出 False True ?chr()   将十进制整数转换为asc

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

 MySQL常用内置函数 说明: 1)可以用在SELECT/UPDATE/DELETE中,及where,orderby,having中 2)在函数里将字段名作为参数,变量的值就是字段所对应的每一行的值. 3)在程序设计语言如C++中提供的函数,MySQL大部分也提供了,关于MySQL函数的完整信息,请参阅<MySQL参考手册> 一.字符串函数[比较常用,需要掌握] 1. concat(s1,s2,...,sn) #把传入的参数连接成一个字符串 selectconcat('abc','def

十四、内置函数

一. 简介 python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:点击查看, 为了方便查看,将内置函数的总结记录下来. 二. 使用说明 以下是Python3版本所有的内置函数: 1. abs() 获取绝对值 1 >>> abs(-10) 2 10 3 >>> abs(10) 4 10 5 >>> abs(0) 6 0 7 >>> a = -10 8 >>> a.__abs__() 9

Python 学习笔记 -- time模块内置函数及实例

1 import time 2 #时间戳:1970.1.1.08:00:00起到现在的总秒数 3 #-----------------------------Time模块内置函数----------------------------- 4 #time.altzone #返回格林威治西部的夏令时地区的偏移秒数 5 print("夏令时区的偏移秒数:time.altzone %d " % time.altzone) 6 7 print("\n-----------------分

Python 基础第十四天(内置函数和匿名函数)

今天主要内容 1.生成器补充--生成器推导式 2.内置函数 3.匿名函数 1.生成器推导式 (1)列表推导式:一行搞定 ,简单,感觉高端.但是,不易排错. 例: l1 = [] for i in range(1,12):  l1.append('python%s期' % i) print(l1) 生成式: l2 = ['python%s期' %i  i  for i in range(1,12)] print(l2) 结构: 循环模式[经过加工的i for i in 可迭代对象] 筛选模式 [经

python基础学习4-函数、内置函数、os模块、time模块

  1       函数 1.1     字符串格式化方法 Python中字符串格式化输出的几种方法: https://www.cnblogs.com/hongzejun/p/7670923.html 字符串格式化另外一种方式format方式 #字符串format()方法 #第一种 import datetime msg = '欢迎光临{name},今天的日期是{today}' msg = msg.format(name = 'zhangsan',today = datetime.datetim

学习13.内容# 1.内置函数二 # 2.闭包

目录 内置函数二 重要的内置函数和匿名函数 闭包 内置函数二 abs 绝对值 返回的都是正数 print([abd(i) for i in lst]) enumerate 枚举 ("可迭代对象","序号的起始值") 默认起始值是0 [(0,1),(1,2),(2,3)] print([i for i in enumerate(lst,10)]) lst = [11,22,33,-44,23,21] new_lst = [] for i in enumerate(ls

python学习之-类的内置函数

内置方法:__str__(该方法必须返回字符串类型),在对像被打印时自动触发,然后将该方法的返回值当做打印结果输出) class People: def __init__(self,name,age): self.name=name self.age=age def __str__(self): #绑定给对象的方法 return '<%s:%s>' %(self.name,self.age) #这个方法必须返回一个字符串类型的值,格式自定义一不限 obj=People('egon',18)pr