Realization of addition, deletion, modification and query of Android address book

The general process of the code function: use the SQL database to implement the functions of adding, deleting, modifying and querying the data!

activity_main.xml: layout file, which is the layout of the background image of the address book, four buttons and a TextView (used to display the data location)!

<?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:background="@drawable/bg"
    android:orientation="vertical"
    tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="250dp"
        android:textSize="30dp"

        android:text="姓名:"/>
    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
android:hint="请输入你的姓名"
        android:layout_marginTop="250dp"

        />
</LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:textSize="30dp"

            android:text="电话:"/>
        <EditText
            android:id="@+id/et_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:hint="请输入你的电话"

            />
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_marginLeft="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_add"
            android:text="添加"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_query"
            android:text="查询"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_updata"
            android:text="修改"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_delete"
            android:text="删除"
            />
    </LinearLayout>
    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="400dp"/>
</LinearLayout>

MyDBHelper: Java file used to import a data table

package com.example.dbtestapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MyDBHelper extends SQLiteOpenHelper {

    public MyDBHelper( Context context) {
        super(context, "itcast.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table information(id integer primary key autoincrement,name verchar(20),phone verchar(20))");//创建information数据表
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

    }
}

MainActivity: Java files are used to implement the logic and functions of functions

package com.example.dbtestapp;

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.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
MyDBHelper myDBHelper;
    private String TAG = "MainActivity";
private EditText mEtName,mTvPhone;
private Button mBtnAdd,mBtnQuery,mBtnUpdata,mBtnDelete;
private TextView mTvShow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
myDBHelper=new MyDBHelper(this);
mTvShow=findViewById(R.id.tv_show);
        mEtName=findViewById(R.id.et_name);
        mTvPhone=findViewById(R.id.et_phone);
        mBtnAdd=findViewById(R.id.btn_add);
        mBtnQuery=findViewById(R.id.btn_query);
        mBtnUpdata=findViewById(R.id.btn_updata);
        mBtnDelete=findViewById(R.id.btn_delete);
        mBtnAdd.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
        mBtnUpdata.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String name,phone;
        SQLiteDatabase db;
        ContentValues values;
        switch (v.getId()){
            case R.id.btn_add://添加功能
               name=mEtName.getText().toString();//获取编辑栏上的内容
               phone=mTvPhone.getText().toString();
               db=myDBHelper.getWritableDatabase();//添加数据用到的是写入方法
               values=new ContentValues();
               values.put("name",name);
               values.put("phone",phone);
               db.insert("information",null,values);//insert将values值放入数据库中
                Toast.makeText(this,"信息已添加",Toast.LENGTH_SHORT).show();
                db.close();//数据库每一个功能实现完毕之后不要忘记close
                break;
            case R.id.btn_query://查询功能
db=myDBHelper.getReadableDatabase();//从数据库中查询数据用到读入方法
Cursor cursor=db.query("information",null,null,null,null,null,null);
if (cursor.getCount()==0){
    mTvShow.setText("");//getcount得到的数据为0将在TextView去放空
    Toast.makeText(this,"没有数据",Toast.LENGTH_SHORT).show();
}else{
    cursor.moveToFirst();//如果有得到数据,将其放在第一个位置
    mTvShow.setText("Name:"+cursor.getString(1)+"; Tel:"+cursor.getString(2));
}
while (cursor.moveToNext()){//追加格式,换行
    mTvShow.append("\n"+"Name:"+cursor.getString(1)+"; Tel:"+cursor.getString(2));
}
cursor.close();
db.close();
                break;
            case R.id.btn_updata://修改功能
db=myDBHelper.getWritableDatabase();//写入方法
values=new ContentValues();
values.put("phone",phone=mTvPhone.getText().toString());
db.update("information",values,"name=?",new String[]{mEtName.getText().toString()});//数据库修改
Toast.makeText(this,"信息已修改",Toast.LENGTH_SHORT).show();
db.close();
                break;
            case R.id.btn_delete:
db=myDBHelper.getWritableDatabase();
db.delete("information",null,null);//数据库删除
Toast.makeText(this,"信息已删除",Toast.LENGTH_SHORT).show();
mTvShow.setText("");
                break;
        }

    }
}

 

 

 

 

 The code flow and some codes have been explained in the text and code segments. If you don’t understand, you can study and discuss together. If there are problems and flaws in the content, please be honest, and I will definitely accept it later and learn to correct it. Thank you!

Guess you like

Origin blog.csdn.net/Abtxr/article/details/124937904