039 Android SQLite数据库(了解)
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数据库(了解)的更多相关文章
- Android Sqlite 数据库版本更新
Android Sqlite 数据库版本更新 http://87426628.blog.163.com/blog/static/6069361820131069485844/ 1.自己写一个类继承 ...
- Android SQLite 数据库详细介绍
Android SQLite 数据库详细介绍 我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用 ...
- Android sqlite数据库存取图片信息
Android sqlite数据库存取图片信息 存储图片:bitmap private byte[] getIconData(Bitmap bitmap){ int size = bitmap.get ...
- Android SQLite 数据库 增删改查操作
Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...
- 图解IntelliJ IDEA 13版本对Android SQLite数据库的支持
IntelliJ IDEA 13版本的重要构建之一是支持Android程序开发.当然对Android SQLite数据库的支持也就成为了Android开发者对IntelliJ IDEA 13版本的绝对 ...
- Android——SQLite/数据库 相关知识总结贴
android SQLite简介 http://www.apkbus.com/android-1780-1-1.html Android SQLite基础 http://www.apkbus.com/ ...
- Android Sqlite数据库加密
Android使用的是开源的SQLite数据库,数据库本身没有加密,加密思路通常有两个: 1. 对几个关键的字段使用加密算法,再存入数据库 2. 对整个数据库进行加密 SQLite数据库加密工具: 收 ...
- Android SQLite数据库使用
在Android开发中SQLite起着很重要的作用,网上SQLite的教程有很多很多,不过那些教程大多数都讲得不是很全面.本人总结了一些SQLite的常用的方法,借着论坛的大赛,跟大家分享分享的.一. ...
- android: SQLite 数据库的最佳实践
6.5.1 使用事务 前面我们已经知道,SQLite 数据库是支持事务的,事务的特性可以保证让某一系列的操 作要么全部完成,要么一个都不会完成.那么在什么情况下才需要使用事务呢?想象以下场 景, ...
随机推荐
- 页面中的radio选择适合的非空判断
var cyjb=$('input:radio[name="jcrwModel.cyjb"]:checked').val(); if(cyjb==n ...
- DM-移除几何上的洞
原视频下载地址:http://yunpan.cn/cujMhvXt4fCfa 访问密码 8afb
- 2019全国大学生信息安全竞赛ciscn-writeup(4web)
web1-JustSoso php伪协议获取源码 ?file=php://filter/read=convert.base64-encode/resource=index.php index.php ...
- 第十四周助教工作总结——NWNU李泓毅
助教博客链接:https://www.cnblogs.com/NWNU-LHY/ 本次作业的要求:团队项目需求改进与系统设计:https://www.cnblogs.com/nwnu-daizh/p/ ...
- c#传不确定的参数个数,比如int型
a(params int[] ) 调用时a(1,2,3,4,5,6)
- 似然函数 | 最大似然估计 | likelihood | maximum likelihood estimation | R代码
学贝叶斯方法时绕不过去的一个问题,现在系统地总结一下. 之前过于纠结字眼,似然和概率到底有什么区别?以及这一个奇妙的对等关系(其实连续才是f,离散就是p). 似然函数 | 似然值 wiki:在数理统计 ...
- WebSocket——SuperWebSocket实现服务端和客户端
WebSocket——SuperWebSocket实现服务端和客户端具体实现如下: 注:本作者是基于vs2019 enterprise版本,所有项目均为.Net Framwork4.7版本(因为Web ...
- vue入门|ElementUI使用指南
vue入门|ElementUI使用指南 1.开发前务必熟悉的文档: vue.js2.0中文,项目所使用的js框架 vue-router,vue.js配套路由 vuex 状态管理 Element UI框 ...
- ubuntu16.04和ubuntu18.04安装dlib
- # for macOS brew install cmake brew install boost brew install boost-python --with-python3 # for U ...
- qemu通过控制台向虚拟机输入组合键
ctrl+alt+2 开启控制台 ctrl+alt+1 关闭控制台 在控制台中输入 sendkey ctrl-alt-delete 发送指令