1.介绍

注意:当有大量相似结构的数据需要存储的时候,需要使用数据库。

2.SQLiteOpenHelper简介

注意:数据库的创建方法总结:

(1)定义一个类继承SQLiteOpenHelper

onCreate()方法:当数据库第一次创建时调用,特别适合做表结构的初始化。

onUpdate()方法:当数据库版本进行更新时调用。

3.简单SQL语句

4.xml文件页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编号:" /> <EditText
android:id="@+id/editText_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:" /> <EditText
android:id="@+id/editText_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"> <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:" /> <EditText
android:id="@+id/editText_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"> <TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入你要查找的编号:" /> <EditText
android:id="@+id/editText_qurryid"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="" /> <Button
android:id="@+id/button_qurryById"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查找" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"> <Button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:layout_marginRight="10dp"/> <Button
android:id="@+id/button_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/button_modify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改" />
</LinearLayout> <TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>

5.java后台

主界面

package com.lucky.test48sqlite;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends AppCompatActivity {
Button button_queryByid;
Button button_add;
Button button_delete;
Button button_modify;
EditText editText_id;
EditText editText_name;
EditText editText_age;
EditText editText_inputid;
TextView textView_findresult;
SQLiteDatabase sqLiteDatabase; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init(); MyDataBase myDataBase=new MyDataBase(MainActivity.this); //实例化数据库
sqLiteDatabase=myDataBase.getWritableDatabase(); //创建数据库 //绑定按钮点击事件
button_queryByid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//根据id进行查询
textView_findresult.setText("");
String sql="select *from user where 编号=?";
Cursor cursor=sqLiteDatabase.rawQuery(sql,new String[]{editText_inputid.getText().toString()});
//判断游标是否可以移到下一行,若可以,则有数据
while (cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex("姓名"));
int id=cursor.getInt(cursor.getColumnIndex("编号"));
textView_findresult.append("\n编号:"+id+"\t姓名:"+name);
}
}
}); button_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//向数据库中添加数据
String sql="insert into user(编号,姓名,年龄) values(?,?,?)";
sqLiteDatabase.execSQL(sql,new Object[]{Integer.parseInt(editText_id.getText().toString()),
editText_name.getText().toString(),
Integer.parseInt(editText_age.getText().toString())});
}
}); button_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//删除数据库中的数据
String sql="delete from user where 编号=?";
sqLiteDatabase.execSQL(sql,new Object[]{Integer.parseInt(editText_id.getText().toString())});
}
}); button_modify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//修改数据库内容
String sql="update user set 编号=?,姓名=?,年龄=?";
sqLiteDatabase.execSQL(sql,new Object[]{Integer.parseInt(editText_id.getText().toString()),
editText_name.getText().toString(),
Integer.parseInt(editText_age.getText().toString())});
}
}); } private void init() {
button_add=findViewById(R.id.button_add);
button_delete=findViewById(R.id.button_delete);
button_modify=findViewById(R.id.button_modify);
button_queryByid=findViewById(R.id.button_qurryById);
editText_age=findViewById(R.id.editText_age);
editText_id=findViewById(R.id.editText_id);
editText_name=findViewById(R.id.editText_name);
editText_inputid=findViewById(R.id.editText_qurryid);
textView_findresult=findViewById(R.id.textView5);
} }

Mydatabase工具类

package com.lucky.test48sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; public class MyDataBase extends SQLiteOpenHelper {
static int version=1;
static String name="lucky.db";
public MyDataBase( Context context) {
//参数1:context为上下文,参数2:name为数据库名称,参数3可默认为null,参数4:为数据库版本
super(context, name, null, version);
} //当数据库第一次被创建的时候,调用该方法,适合进行数据库表的初始化,创建数据库(注意:数据库创建在安卓app文件包内)
@Override
public void onCreate(SQLiteDatabase db) {
String sql="create table user(编号 Integer,姓名 varchar(10),年龄 Integer)";
db.execSQL(sql);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}

039 Android SQLite数据库(了解)的更多相关文章

  1. Android Sqlite 数据库版本更新

      Android Sqlite 数据库版本更新 http://87426628.blog.163.com/blog/static/6069361820131069485844/ 1.自己写一个类继承 ...

  2. Android SQLite 数据库详细介绍

    Android SQLite 数据库详细介绍 我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用 ...

  3. Android sqlite数据库存取图片信息

    Android sqlite数据库存取图片信息 存储图片:bitmap private byte[] getIconData(Bitmap bitmap){ int size = bitmap.get ...

  4. Android SQLite 数据库 增删改查操作

    Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...

  5. 图解IntelliJ IDEA 13版本对Android SQLite数据库的支持

    IntelliJ IDEA 13版本的重要构建之一是支持Android程序开发.当然对Android SQLite数据库的支持也就成为了Android开发者对IntelliJ IDEA 13版本的绝对 ...

  6. Android——SQLite/数据库 相关知识总结贴

    android SQLite简介 http://www.apkbus.com/android-1780-1-1.html Android SQLite基础 http://www.apkbus.com/ ...

  7. Android Sqlite数据库加密

    Android使用的是开源的SQLite数据库,数据库本身没有加密,加密思路通常有两个: 1. 对几个关键的字段使用加密算法,再存入数据库 2. 对整个数据库进行加密 SQLite数据库加密工具: 收 ...

  8. Android SQLite数据库使用

    在Android开发中SQLite起着很重要的作用,网上SQLite的教程有很多很多,不过那些教程大多数都讲得不是很全面.本人总结了一些SQLite的常用的方法,借着论坛的大赛,跟大家分享分享的.一. ...

  9. android: SQLite 数据库的最佳实践

    6.5.1    使用事务 前面我们已经知道,SQLite 数据库是支持事务的,事务的特性可以保证让某一系列的操 作要么全部完成,要么一个都不会完成.那么在什么情况下才需要使用事务呢?想象以下场 景, ...

随机推荐

  1. Python 的特性

    Copyright © 1999-2019, CSDN.NET, All Rights Reserved     原 python面试题整理(一) 崔先生的博客阅读数:2402018-08-03 前言 ...

  2. namenode 性能优化 RPC队列拆分

    一.Service RPC port NameNode默认使用8020端口侦听所有的RPC请求(HDP版本),包括客户端数据请求,DataNode心跳和block上报,ZKFC模块监控检查和切换控制. ...

  3. SpringMVC从Request域中获取数据

    SpringMVC从Request域中获取数据的三种方式 SpringMVC环境自行搭建, 约定存在如下目录和文件:/WEB-INF/pages/success.jsp 方式一:传入Model对象 前 ...

  4. Mysql 查看所有线程,被锁的表

    查看所有MySQl相关的线程 show full processlist; 杀死线程id为2的线程 kill 2 查看服务器状态 show status like '%lock%'; 查看服务器配置参 ...

  5. 请你谈谈Cookie的弊端

    a. 每个特定的域名下最多生成的cookie个数有限制 b. IE和Opera 会清理近期最少使用的cookie,Firefox会随机清理cookie c. cookie的最大大约为4096字节,为了 ...

  6. SKU是什么意思?

    在做电商项目时候必定会遇到这个词,那是什么意思呢?其实简单来讲就是一个单位. SKU全称为Stock Keeping Unit(库存量单位),即库存进出计量的基本单元,可以是以件,盒,托盘等为单位.S ...

  7. SQLServer 临时表的使用

    临时表在Sqlserver数据库中,是非常重要的,下面就详细介绍SQL数据库中临时表的特点及其使用,仅供参考. 临时表与永久表相似,但临时表存储在tempdb中,当不再使用时会自动删除.临时表有两种类 ...

  8. layui 鼠标悬停单元格显示全部

    {field : 'subjectId',title : '主题id',align: 'center',edit : 'text',templet:'<div><span title ...

  9. 【转载】 谷歌集群数据分析 clusterdata-2011-2

    原文地址: https://www.twblogs.net/a/5c2dc304bd9eee35b21c418b/zh-cn ------------------------------------- ...

  10. 003-结构型-05-桥接模式(Bridge)

    一.概述 将抽象部分与它的具体实现部分分离.使它们都可以独立地变化.通过组合的方式建立两个类之间联系,而不是继承. Bridge 模式又叫做桥接模式,是构造型的设计模式之一.Bridge模式基于类的最 ...