源码

效果展示


    所有操作都在这个界面完成,操作完直接显示

设计

    一个class用来创建数据库,建表,一个activity用来执行增删改查操作

代码

DatebaseHlper

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;public class DatabaseHelper extends SQLiteOpenHelper {public DatabaseHelper(Context context){super(context,"Test.db",null,1);}//第一个参数是上下文,第二个参数是数据库名称,//第三个参数是CursorFactory对象,一般设置为null,第四个参数是数据库的版本public void onCreate(SQLiteDatabase db) {db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),age INTEGER)");}//创建表 表名information 表结构 自增id,字符串姓名,int年龄public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {Log.d("myDeBug","数据库版本已更新");}//数据库版本发生变化时调用
}

DictActivity

布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="姓名"android:textSize="30sp"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入姓名"android:textSize="20sp"android:id="@+id/name"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="年龄"android:textSize="30sp"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入年龄"android:textSize="20sp"android:id="@+id/age"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:textSize="20sp"android:text="插入"android:id="@+id/btn_insert"android:textAllCaps="false"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:textSize="20sp"android:text="更新"android:id="@+id/btn_update"android:textAllCaps="false"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:textSize="20sp"android:text="查询"android:id="@+id/btn_search"android:textAllCaps="false"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:textSize="20sp"android:text="删除"android:id="@+id/btn_delete"android:textAllCaps="false"/></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:layout_weight="1"android:id="@+id/tv_show"android:textSize="20sp"android:gravity="center_horizontal"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:id="@+id/tv_showAge"android:textSize="20sp"android:gravity="center_horizontal"/>
</LinearLayout></LinearLayout>

    最外层垂直线性布局,内部三个线性布局,后两个用的水平排版。
    因为不清楚表格TextView怎么做,TableView太麻烦,索性两个TextView,weight都设置为1就可以了。

主要代码
import androidx.appcompat.app.AppCompatActivity;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.widget.Button;
import android.widget.EditText;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;public class DictActivity extends AppCompatActivity {private Button insertButton,updateButton,searchButton,deleteButton;private EditText name,age;private TextView show,showAge;final DatabaseHelper dbHelper=new DatabaseHelper(DictActivity.this);@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_dict);insertButton=findViewById(R.id.btn_insert);updateButton=findViewById(R.id.btn_update);searchButton=findViewById(R.id.btn_search);deleteButton=findViewById(R.id.btn_delete);name=findViewById(R.id.name);age=findViewById(R.id.age);show=findViewById(R.id.tv_show);showAge=findViewById(R.id.tv_showAge);SQLiteDatabase db=dbHelper.getReadableDatabase();myShow();insertButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db=dbHelper.getWritableDatabase();ContentValues values=new ContentValues();values.put("name",name.getText().toString());values.put("age",age.getText().toString());long id =db.insert("information",null,values);Log.d("myDeBug","insert");myShow();db.close();name.setText(null);age.setText(null);}});updateButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db=dbHelper.getWritableDatabase();ContentValues values=new ContentValues();values.put("age",age.getText().toString());db.update("information",values,"name=?",new String[]{name.getText().toString()});myShow();db.close();Log.d("myDebug","update");name.setText(null);age.setText(null);}});searchButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db=dbHelper.getWritableDatabase();String name1=name.getText().toString();show.setText(null);if(name1.equals("")){//                    show.setText("姓名");
//                    showAge.setText("年龄");
//                    Cursor cursor = db.rawQuery("select * from information",null);
//
//                    while (cursor.moveToNext()) {//                        String newName = cursor.getString(cursor.getColumnIndex("name"));
//                        int newAge = cursor.getInt(cursor.getColumnIndex("age"));
//                        show.setText(show.getText() + "\n" + newName);
//                        showAge.setText(showAge.getText()+"\n" + newAge);
//                    }myShow();db.close();}else {show.setText("姓名");showAge.setText("年龄");Cursor cursor = db.rawQuery("select * from information where name = ? ", new String[]{name1});while (cursor.moveToNext()) {String newName = cursor.getString(cursor.getColumnIndex("name"));int newAge = cursor.getInt(cursor.getColumnIndex("age"));
//                        show.setText(show.getText() + "\n" + newName + "\t" + newAge);show.setText(show.getText() + "\n" + newName);showAge.setText(showAge.getText()+"\n" + newAge);}cursor.close();db.close();name.setText(null);age.setText(null);}}});deleteButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db=dbHelper.getWritableDatabase();db.delete("information","name=?",new String[]{name.getText().toString()});myShow();db.close();Log.d("myDeBug","DeleteSuccess");name.setText(null);age.setText(null);}});}public void myShow(){SQLiteDatabase db=dbHelper.getReadableDatabase();show.setText("姓名");showAge.setText("年龄");Cursor cursor = db.rawQuery("select * from information",null);while (cursor.moveToNext()) {String newName = cursor.getString(cursor.getColumnIndex("name"));int newAge = cursor.getInt(cursor.getColumnIndex("age"));show.setText(show.getText() + "\n" + newName);showAge.setText(showAge.getText()+"\n" + newAge);}cursor.close();}

    实例化四个Button,两个EditText,两个TextView,连接数据库
按钮增加监听点击事件,editText用来获取输入,TextView来展示成果。
    说一下myShow()方法,在这个里面我写了展示代码,先连接数据库,设置“表头”,用cursor来遍历表内所有信息,直到没有行,每过一行,获取name和age列的数据,设置TextView的文本(setText())之前的文本,换行,加上这行的数据。其他的增删改查,只是简单地执行sql语句或者调用SQLiteDatabase的方法。
在这里解释一下cursor
    Cursor默认是行的集合:
    查询出来的cursor的初始位置是指向第一条记录的前一个位置的
    cursor.moveToFirst()指向查询结果的第一个位置。
    一般通过判断cursor.moveToFirst()的值为true或false来确定查询结果是否为空。cursor.moveToNext()是用来做循环的,一般这样来用:while(cursor.moveToNext()){ }
    cursor.moveToPrevious()是指向当前记录的上一个记录,是和moveToNext相对应的;
    cursor.moveToLast()指向查询结果的最后一条记录
    使用cursor可以很方便的处理查询结果以便得到想要的数据
    上面关于cursor这部分来自Cursor.moveToNext();和Cursor.moveToFirst();

Android Studio SQLite 数据库 增删改查 简单相关推荐

  1. 基于python的SQLite数据库增删改查

    与其他数据库管理系统不同,SQLite不是一个客户端/服务器结构的数据库引擎,而是一种嵌入式数据库,他的数据库就是一个文件.SQLite将整个数据库,包括定义.表.索引以及数据本身,作为一个单独的.可 ...

  2. IOS sqlite数据库增删改查

    1.简介 简单封装sqlite数据库操作类 BaseDB 用于完成对sqlite的增删改查,使用前先导入libsqlite3.0.dylib库 2.BaseDB.h // // BaseDB.h // ...

  3. android 实现Sqlite的增删改查及系统的登录注册功能

    文章目录 1.用户实体类 2.SQLiteOpenHelper类 3.数据库名常量类 4.数据库增删改查方法封装 5.界面设计 5.1 登录页面 5.2 注册界面 5.3 修改密码界面 6. 源码下载 ...

  4. Android 绿豆通讯录【 SQLite数据库(增删改查、展示数据) + ListView数据展示控件(展示所有数据) 】

    前情提要:Android 数据库(SQLite) [简介.创建.使用(增删改查.事务.实战演练).数据显示控件(ListView.Adapter.实战演练-绿豆通讯录)] https://blog.c ...

  5. SQLite数据库-增删改查语句

    Android中自带的数据库----SQLite 这里使用Navicat工具来学习 1.创建新的SQLite连接 数据库文件名字和连接名一样就好 2.数据库的创建语句 //创建一个book表 crea ...

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

    一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字). TEXT(字符 ...

  7. ios Sqlite数据库增删改查基本操作

    2019独角兽企业重金招聘Python工程师标准>>> 研究了几天的数据库,终于把它给搞出来了.Sqlite是ios上最常用的数据库之一,大家还是有必要了解一下的.这是仿照网上的一个 ...

  8. Mybatis实现简单的数据库增删改查操作

    简介: MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Ma ...

  9. IntelliJ Idea SpringBoot 数据库增删改查实例

    . ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \\\\( ( )\___ | '_ | '_| | '_ \/ _` | \\\\ \\/ ___)| | ...

最新文章

  1. MongoDB数据库(一:基本操作)
  2. POJ2253 Frogger(最短路径)
  3. IM即时通讯:如何跳出传统思维来设计聊天室架构?
  4. 基于约束的SQL攻击
  5. linux内核I2C子系统学习(一)
  6. html bootstrap复选框全选,javascript+bootstrap+html实现层级多选框全层全选和多选功能代码实例...
  7. 如何理解面向对象建模语言UML?
  8. java中的@override
  9. EPIC《禅意花园》项目开放下载
  10. synchronized实现
  11. 嘿嘿,俺做长辈了!!!
  12. 如何卸载jdk_Java新手怎样安装JDK,手把手教你如何安装JDK
  13. 在vue项目中使用图片浏览组件v-viewer,支持旋转、缩放、翻转等操作
  14. 用户如何制作360度全景图?360度全景图有什么用?
  15. 北京生鲜小程序开发之万象优鲜生鲜配送系统源码
  16. LM334芯片到底是恒流源还是温度传感器?
  17. tar -d 选项 比较归档文件与文件系统中的内容
  18. 响铃:当AI翻译能识别“语境”,我们的“地球村”梦想就不远了
  19. HTTP POST方法的学习
  20. 基于c语言的图像边缘检测,基于C语言的医学图像处理的设计

热门文章

  1. 安装会声会影2018时出现Error1935错误的解决方法
  2. Theory: For loop(理论:For循环)
  3. Android Studio配置文件路径修改的方法
  4. fortran和matlab编程的区别,fortran和matlab
  5. PhpOffice——PHPWord导入导出水印模板替换
  6. 详解Java NIO,IO与NIO的区别
  7. javaEE基于ssm的学生宿舍在线报修管理系统
  8. linux串口输出系统日志,linux系统连接串口工具打印log
  9. 机器学习 鸢尾花分类的原理和实现(一)
  10. win7计算机电源设置在哪里设置,win7系统高级电源管理怎么打开?win7系统设置高级电源管理的方法...