SQLiteDataBase数据库

XML界面设计  
<?xml version="1.0" encoding="utf-8"?>
<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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="姓名:" /><EditTextandroid:id="@+id/etc_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="50px" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="班级:" /><EditTextandroid:id="@+id/etc_class"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="50px" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="学号:" /><EditTextandroid:id="@+id/etc_id"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="50px" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="添加数据"android:textAlignment="center" /><Buttonandroid:id="@+id/btn_show"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="全部显示"android:textAlignment="center" /><Buttonandroid:id="@+id/btn_clr"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="清除数据"android:textAlignment="center" /><Buttonandroid:id="@+id/btn_del"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="全部删除"android:textAlignment="center" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="ID:" /><EditTextandroid:id="@+id/etc_id2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="2" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_del_id"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="ID删除"android:textAlignment="center" /><Buttonandroid:id="@+id/btn_show_id"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="ID查询"android:textAlignment="center" /><Buttonandroid:id="@+id/btn_update_id"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="ID更新"android:textAlignment="center" /></LinearLayout><TextViewandroid:id="@+id/txt_end"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="" /><ListViewandroid:id="@+id/list"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>
People.java
package com.example.exp6;public class People {public int ID = -1;public String Name;public String Class;public String Number;//重载toString方法  返回值String类型@Overridepublic String toString(){String result = "";result += "ID:" + this.ID + ",";result += "姓名:" + this.Name + ",";result += "班级:" + this.Class + ", ";result += "学号:" + this.Number;return result;}
}
 DBAdapter.java
package com.example.exp6;
import android.annotation.SuppressLint;
import android.content.*;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;import androidx.annotation.Nullable;//对数据库进行操作的类
public class DBAdapter {private static final String DB_NAME = "student.db";   //数据库名称private static final String DB_TABLE = "peopleinfo";  //表名private static final int DB_VERSION = 1;              //数据库版本public static final String KEY_ID = "_id";            //数据库表的属性名称public static final String KEY_NAME = "name";public static final String KEY_CLASS = "class";public static final String KEY_NUMBER = "number";private SQLiteDatabase db;              //数据库的实例dbprivate final Context context;          //Context的实例化private DBOpenHelper dbOpenHelper;      //帮助类的实例化 dbOpenHelper//内部类:对帮助类 构建//继承SQLiteOpenHelper//必须重载onCreate和onUpgrade方法private static class DBOpenHelper extends SQLiteOpenHelper {//帮助类的构造函数 -- 4个参数//Code -> SQLiteOpenHelper 即可成功插入该构造函数public DBOpenHelper(@Nullable Context context, @Nullable String name,SQLiteDatabase.CursorFactory factory, int version) {super(context, name, factory, version);}//static常量字符串--创建表的 sql 命令//create table peopleinfo("id integer primary key autoincrement,name text not null,// class text not null,number text not null")private static final String DB_CREATE = "create table " +DB_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " +KEY_NAME + " text not null, " + KEY_CLASS + " text not null," +KEY_NUMBER + " text not null);";//重载帮助类onCreate方法//1个参数SQLiteDatabase类型@Overridepublic void onCreate(SQLiteDatabase sqLiteDatabase) {//execSQL()方法 1个String类型的 sql 建表命令sqLiteDatabase.execSQL(DB_CREATE);}//重载帮助类onUpdate方法//一般在升级的时候被调用//删除旧的数据库表 并将数据转移到新版本的数据库表//3个参数SQLiteDatabase类型、int i-旧版本号、int i1-新版本号@Overridepublic void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {//仅删除原表后建立新表sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);onCreate(sqLiteDatabase);}}//对数据库进行操作的类DBAdapter的构造//参数1个 Context类型public DBAdapter(Context _context) {context = _context;}//DBAdapter类的open方法//抛出异常!!!!public void open() throws SQLiteException {//帮助类实例化dbOpenHelper需要4个参数dbOpenHelper = new DBOpenHelper(context, DB_NAME,null, DB_VERSION);//调用getWritableDatabase方法try {db = dbOpenHelper.getWritableDatabase();} catch (SQLiteException ex) {db = dbOpenHelper.getReadableDatabase();}}//DBAdapter类的close方法public void close() {if (db != null) {db.close();db = null;}}//DBAdapter类的insert方法//参数1个:数据库储存的类型(本例是Peoplepublic long insert(People people) {//用ContentValues类型ContentValues newValues = new ContentValues();//在put的时候不需要put Key_ID!!!newValues.put(KEY_NAME, people.Name);newValues.put(KEY_CLASS, people.Class);newValues.put(KEY_NUMBER, people.Number);//返回值是新数据插入的位置 ID值//数据库对象.insert方法//参数3个:表名 在null时的替换数据 ContentValues类型需要添加的数据return db.insert(DB_TABLE, null, newValues);}//DBAdapter类的deleteAllData方法//无参数public long deleteAllData() {//返回值是被删除的数据的数量//参数3个:表名 删除条件(删除全部数据条件是null)return db.delete(DB_TABLE, null, null);}//DBAdapter类的deleteOneData方法//参数1个:long类型的id值public long deleteOneData(long id) {//返回值是被删除的数据的数量//参数3个:表名 删除条件(删除形参给的id)return db.delete(DB_TABLE,  KEY_ID + "=" + id, null);}//DBAdapter类的UpdataOneData方法//参数2个:long类型的id值 和 People类型public long updateOneData(long id , People people){//更新和插入一样用到了ContentValues类型ContentValues updateValues = new ContentValues();//同样不需要放Key_IDupdateValues.put(KEY_NAME, people.Name);updateValues.put(KEY_CLASS, people.Class);updateValues.put(KEY_NUMBER, people.Number);//返回值是被更新的数据数量//参数4个 表明 ContentValues 更新条件string类型 nullreturn db.update(DB_TABLE, updateValues,  KEY_ID + "=" + id, null);}//private类型//DBAdapter类的ConvertToPeople方法//参数1个 Cursor类型//返回People数组//查询需要基于该 ConvertToPeople 方法(应该是自定义方法?)@SuppressLint("Range")private People[] ConvertToPeople(Cursor cursor){//getCount方法返回查询结果总行数int resultCounts = cursor.getCount();//行数为0||moveToFirst方法返回false返回结果为空if (resultCounts == 0 || !cursor.moveToFirst()){//该方法返回nullreturn null;}//新建resultCounts个People对象People[] peoples = new People[resultCounts];//循环 resultCounts次for (int i = 0 ; i<resultCounts; i++){peoples[i] = new People();peoples[i].ID = cursor.getInt(0);//根据 列属性索引 得到 属性值peoples[i].Name = cursor.getString(cursor.getColumnIndex(KEY_NAME));peoples[i].Class = cursor.getString(cursor.getColumnIndex(KEY_CLASS));peoples[i].Number = cursor.getString(cursor.getColumnIndex(KEY_NUMBER));//游标/指针下移cursor.moveToNext();}//返回People[]return peoples;}//查询操作://DBAdapter类的getOneData方法//参数1个:long类型的id值public People[] getOneData(long id) {//query方法//参数7个(6String 1String[]):表名称 属性列-String[] 查询条件//查询条件是否使用通配符 分组条件 分组过滤条件 排序方式  后4个全是null//返回Cursor类型Cursor results =  db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_CLASS, KEY_NUMBER},KEY_ID + "=" + id, null, null, null, null);//返回People[]return ConvertToPeople(results);}//DBAdapter类的getAllData方法public People[] getAllData() {//参数查询条件为null  存在5个nullCursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_CLASS, KEY_NUMBER},null, null, null, null, null);return ConvertToPeople(results);}}
MainActivity.java 
package com.example.exp6;import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
import android.widget.ArrayAdapter;public class MainActivity extends AppCompatActivity {EditText etc_name,etc_class,etc_id,etc_id2;TextView txt_end;ListView listView;Button btn_add,btn_show,btn_clr,btn_del,btn_del_id,btn_show_id,btn_update_id;DBAdapter dbAdapter;SQLiteDatabase db;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=findViewById(R.id.list);ArrayList<String> data=new ArrayList<String>();//数组适配器//实例化 参数3个ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);etc_name=findViewById(R.id.etc_name);etc_class=findViewById(R.id.etc_class);etc_id=findViewById(R.id.etc_id);btn_add=findViewById(R.id.btn_add);btn_show=findViewById(R.id.btn_show);btn_clr=findViewById(R.id.btn_clr);btn_del=findViewById(R.id.btn_del);btn_del_id=findViewById(R.id.btn_del_id);btn_show_id=findViewById(R.id.btn_show_id);btn_update_id= findViewById(R.id.btn_update_id);etc_id2=findViewById(R.id.etc_id2);txt_end=findViewById(R.id.txt_end);//处理数据库的类的实例对象//参数1个 Context类型dbAdapter=new DBAdapter(this);//调用DBAdapter对象的 open 方法dbAdapter.open();btn_add.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//将文本框中的内容用来创建对象PeoplePeople t=new People();t.Name=etc_name.getText().toString();t.Class=etc_class.getText().toString();t.Number=etc_id.getText().toString();//插入一个对象People//返回插入的位置idlong colunm=dbAdapter.insert(t);if (colunm == -1 ){txt_end.setText("添加过程错误!");} else {txt_end.setText("ID:"+String.valueOf(colunm)+"   姓名:"+t.Name+"   班级:"+t.Class+"   学号:"+t.Number);}}});btn_show.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//调用得到数据库中全部的信息People [] peoples =dbAdapter.getAllData();if (peoples == null){txt_end.setText("数据库中没有数据");return;}String t="数据库:\n";for(int i=0;i<peoples.length;++i){t += peoples[i].toString()+"\n";}txt_end.setText(t);}});btn_clr.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {txt_end.setText("");}});btn_del.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {dbAdapter.deleteAllData();txt_end.setText("已删除所有数据!");}});btn_del_id.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {int id=Integer.parseInt(etc_id2.getText().toString());//返回删除的数据的数量//参数要求int类型的long result=dbAdapter.deleteOneData(id);String msg = "删除ID为"+etc_id2.getText().toString()+"的数据" + (result>0?"成功":"失败");txt_end.setText(msg);}});btn_show_id.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {int id=Integer.parseInt(etc_id2.getText().toString());//查找方法 参数id-int//返回值是People[]People people[]=dbAdapter.getOneData(id);if(people==null){txt_end.setText("Id为"+id+"的记录不存在!");}else{//因为仅只查找了一条信息 所以是people[0]//并且在People类型中已经重载了函数toString()txt_end.setText("查询成功:\n"+people[0].toString());}}});btn_update_id.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {int id=Integer.parseInt(etc_id2.getText().toString());People t=new People();t.Name=etc_name.getText().toString();t.Class=etc_class.getText().toString();t.Number=etc_id.getText().toString();//更新方法//参数2个 id-int people类型//返回值 被更新的数据数量long n=dbAdapter.updateOneData(id,t);if (n<0){txt_end.setText("更新过程错误!");}else{txt_end.setText("成功更新数据,"+String.valueOf(n)+"条");}}});}@Overrideprotected void onStop() {super.onStop();dbAdapter.close();}
}
结果

数据添加(朱迪&尼克狐尼克)

全部显示 

ID删除 (25-朱迪)

 ID查询 (26)

ID更新 (26) 

全部删除

全部显示 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/64611.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

k8s部署nginx+sshd实现文件上传下载

要通过 nginx 和 sshd 实现文件的上传和下载&#xff0c;通常的做法是结合 SSH 协议和 HTTP 协议&#xff0c;使用 nginx 提供 Web 服务器功能&#xff0c;同时使用 sshd&#xff08;即 SSH 服务&#xff09;来处理通过 SSH 协议进行的文件传输。 SSH 实现文件的上传和下载&…

Golang 中 Goroutine 的调度

Golang 中 Goroutine 的调度 Golang 中的 Goroutine 是一种轻量级的线程&#xff0c;由 Go 运行时&#xff08;runtime&#xff09;自动管理。Goroutine 的调度基于 M:N 模型&#xff0c;即多个 Goroutine 可以映射到多个操作系统线程上执行。以下是详细的调度过程和策略&…

clickhouse-backup配置及使用(Linux)

一、下载地址 Releases Altinity/clickhouse-backup GitHub 二、上传到服务器解压安装 自行上传至服务器&#xff0c;解压命令&#xff1a; tar xvf clickhouse-backup-linux-amd64.tar.gz 三、创建软连接 sudo ln -sv build/linux/amd64/clickhouse-backup /usr/local/bin/…

如何在群晖NAS上安装并配置MySQL与phpMyAdmin远程管理数据库

文章目录 前言1. 安装MySQL2. 安装phpMyAdmin3. 修改User表4. 本地测试连接MySQL5. 安装cpolar内网穿透6. 配置MySQL公网访问地址7. 配置MySQL固定公网地址8. 配置phpMyAdmin公网地址9. 配置phpmyadmin固定公网地址 前言 大家是不是经常遇到需要随时随地访问自己数据的情况&am…

《向量数据库指南》——Milvus Cloud 2.5:Sparse-BM25引领全文检索新时代

Milvus Cloud BM25:重塑全文检索的未来 在最新的Milvus Cloud 2.5版本中,我们自豪地引入了“全新”的全文检索能力,这一创新不仅巩固了Milvus Cloud在向量数据库领域的领先地位,更为用户提供了前所未有的灵活性和效率。作为大禹智库的向量数据库高级研究员,以及《向量数据…

SQL 总结

SQL 总结 引言 SQL(Structured Query Language,结构化查询语言)是一种用于管理关系数据库管理系统(RDBMS)的标准编程语言。自1974年首次提出以来,SQL已成为数据库领域中不可或缺的一部分。它允许用户执行各种操作,如查询、更新、插入和删除数据库中的数据。本文旨在提…

ESP32-CAM开发板入门 (下载示例程序)

ESP32-CAM开发板例程使用 1、准备工作1.1、硬件准备1.2、软件准备 2、选择示例程序并录入第一步 1、准备工作 1.1、硬件准备 1.2、软件准备 Arduino IDE &#xff1a; 编程与写入&#xff08;下载地址 https://www.arduino.cc/en/software&#xff09; 安装好后将软件设置到…

企业赋能是什么意思-国际数字影像产业园解读

在当今竞争激烈的商业环境中&#xff0c;企业赋能已成为推动企业发展、提升竞争力的关键策略。国际数字影像产业园作为数字影像产业的重要集聚地&#xff0c;通过一系列创新举措为入驻园区的我众多企业赋能。那么&#xff0c;企业赋能究竟是什么意思呢&#xff1f; 企业赋能是…

混合并行训练框架性能对比

混合并行训练框架性能对比 1. 框架类型 DeepSpeed、Megatron - LM、Colossal - AI、SageMaker、Merak、FasterMoE、Tutel、Whale、Alpa、DAPPLE、Mesh - TensorFlow 2. 可用并行性(Available parallelisms) DNN framework(深度神经网络框架)DP(数据并行,Data Parallelis…

客户案例:基于慧集通集成平台,打通屠宰管理系统与用友U8C 系统的全攻略

一、引言 本原型客户成立于2014年&#xff0c;是一家集饲草种植、肉牛养殖、精深加工、冷链物流、餐饮服务于一体的大型农牧综合体。公司下设三个子公司分别涵盖农业、畜牧业、肉制品加工业与餐饮物流服务业。公司严格按照一二三产业融合发展要求&#xff0c;以肉牛产业化为支…

HTML5滑块(Slider)

HTML5 的滑块&#xff08;Slider&#xff09;控件允许用户通过拖动滑块来选择数值。以下是如何实现一个简单的滑块组件的详细说明。 HTML5 滑块组件 1. 基本结构 使用 <input type"range"> 元素可以创建一个滑块。下面是基本实现的代码示例&#xff1a; <…

25. C++继承 1 (继承的概念与基础使用, 继承的复制兼容规则,继承的作用域)

⭐上篇模板文章&#xff1a;24. C模板 2 (非类型模板参数&#xff0c;模板的特化与模板的分离编译)-CSDN博客 ⭐本篇代码&#xff1a;c学习 橘子真甜/c-learning-of-yzc - 码云 - 开源中国 (gitee.com) ⭐标⭐是比较重要的部分 目录 一. 继承的基础使用 1.1 继承的格式 1.2 …

露营小程序搭建有哪些步骤?小程序里面可以找个露营搭子

露营不仅仅是走进大自然的旅程&#xff0c;它也成为了一种社交和体验式的活动。随着小程序的普及&#xff0c;露营活动也越来越多地开始在线上开展。通过搭建一个露营小程序&#xff0c;商家不仅可以为用户提供更多的露营选择&#xff0c;还可以帮助他们找到合适的露营搭子。那…

XIAO ESP32 S3网络摄像头——2视频获取

本文主要是使用XIAO Esp32 S3制作网络摄像头的第2步,获取摄像头图像。 1、效果如下: 2、所需硬件 3、代码实现 3.1硬件代码: #include "WiFi.h" #include "WiFiClient.h" #include "esp_camera.h" #include "camera_pins.h"// 设…

记一次 dockerfile 的循环依赖错误

文章目录 1. 写在最前面1.1 具体循环依赖的例子 2. 报错的位置2.1 代码快速分析2.2 代码总结2.3 关于 parser 的记录 3. 碎碎念 1. 写在最前面 笔者在使用 dockerfile 多阶段构建的功能时&#xff0c;写出了一个「circular dependency detected on stage: xx」的错误。 解决方…

AAAI 2025论文分享┆一种接近全监督的无训练文档信息抽取方法:SAIL(文中附代码链接)

本推文详细介绍了一篇上海交通大学乐心怡老师课题组被人工智能顶级会议AAAI 2025录用的的最新论文《SAIL: Sample-Centric In-Context Learning for Document Information Extraction》。论文的第一作者为张金钰。该论文提出了一种无需训练的、以样本为中心的、基于上下文学习的…

小程序信息收集(小迪网络安全笔记~

免责声明&#xff1a;本文章仅用于交流学习&#xff0c;因文章内容而产生的任何违法&未授权行为&#xff0c;与文章作者无关&#xff01;&#xff01;&#xff01; 附&#xff1a;完整笔记目录~ ps&#xff1a;本人小白&#xff0c;笔记均在个人理解基础上整理&#xff0c;…

pat 乙级1096 大美数

若正整数 N 可以整除它的 4 个不同正因数之和&#xff0c;则称这样的正整数为“大美数”。本题就要求你判断任一给定的正整数是否是“大美数”。 输入格式&#xff1a; 输入在第一行中给出正整数 K&#xff08;≤10&#xff09;&#xff0c;随后一行给出 K 个待检测的、不超过…

C#封送类

封送类&#xff08;Marshaling classes&#xff09;在.NET框架中扮演着至关重要的角色&#xff0c;尤其是在托管代码与非托管代码之间进行数据交换时。封送过程涉及到将托管环境中的对象转换为非托管环境中可以理解的形式&#xff0c;并且反之亦然。这一过程确保了两种不同类型…

计算机体系结构期末考试

1、描述计算机系统性能评估的关键指标&#xff0c;并以SPEC CPU benchmark为例&#xff0c;讨论如何使用几何平均数与加权平均数对性能进行量化。此外&#xff0c;描述Amdahl定律并分析该定律的应用场景及其对性能优化的局限性 2、请对比RISC和CISC指令集架构的设计思想及优缺点…