Android——Sqlite database——to realize the function of adding, querying, modifying and deleting contact information

 Master the use of common layouts and basic controls
 Master the use of interface image display
 Master the creation and basic operation of SQLite database

Build the Activity interface through linear layout and relative layout, write logic code in MainActivity, run the program, enter two contact information and click "Add", "Query", "Modify" and "Delete" buttons respectively. Realize the functions of adding, querying, modifying and deleting contact information. After the operation is successful, the corresponding operation will be displayed through the pop-up message content. The following interface is for reference only, and another interface can be designed according to the function.

activity_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:id="@+id/ll1">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:layout_marginLeft="10dp" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et1"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll1"
        android:id="@+id/ll2">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话:"
            android:layout_marginLeft="10dp" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et2"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/ll2"
        android:layout_marginTop="10dp"
        android:id="@+id/ll3">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#5522"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            android:text="添加"
            android:textColor="#000"
            android:id="@+id/addBtn"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#5522"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:text="查询"
            android:textColor="#000"
            android:id="@+id/selectBtn"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#5522"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:text="修改"
            android:textColor="#000"
            android:id="@+id/updateBtn"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#5522"
            android:layout_weight="1"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="5dp"
            android:text="删除"
            android:textColor="#000"
            android:id="@+id/deleteBtn"/>
    </LinearLayout>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et3"
        android:layout_below="@+id/ll3"
        android:hint="请输入要查询或修改或删除的姓名:"
        android:layout_marginLeft="10dp"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#5522"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/et3"
        android:layout_marginTop="5dp"
        android:textColor="#000"
        android:text="条件查询"
        android:id="@+id/conditional_query_btn"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/conditional_query_btn"
        android:id="@+id/showInfo"
        android:layout_marginLeft="10dp" />
</RelativeLayout>

I added a condition input box to the original interface, so that operations can be performed by the user's name when querying, modifying and deleting conditions. 

MainActivity.java

package com.example.shiyan5;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button add, select, update, delete, conditionalQueryBtn;
    private EditText name, telNumber, conditionalQueryEt;
    private TextView showInfo;
    private SQLiteDatabase db;
    private MyDbHelper myDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initVeiw();
        add.setOnClickListener(this);
        select.setOnClickListener(this);
        update.setOnClickListener(this);
        delete.setOnClickListener(this);
        conditionalQueryBtn.setOnClickListener(this);
        conditionalQueryEt.setOnClickListener(this);
        myDbHelper = new MyDbHelper(this, "MyDatabase.db", null, 669);
    }

    private void initVeiw() {
        add = findViewById(R.id.addBtn);
        select = findViewById(R.id.selectBtn);
        update = findViewById(R.id.updateBtn);
        delete = findViewById(R.id.deleteBtn);
        name = findViewById(R.id.et1);
        telNumber = findViewById(R.id.et2);
        showInfo = findViewById(R.id.showInfo);
        conditionalQueryEt = findViewById(R.id.et3);
        conditionalQueryBtn = findViewById(R.id.conditional_query_btn);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.addBtn:
                db = myDbHelper.getWritableDatabase();
                String userName = name.getText().toString();
                String telephoneNumber = telNumber.getText().toString();
                //SQLiteDatabase的insert方法来添加
                /* ContentValues contentValues = new ContentValues();
                contentValues.put("userName", userName);
                contentValues.put("telephoneNumber", telephoneNumber);
                db.insert("user", null, contentValues);*///返回的是插入记录的索引号,也就是第多少条。
                db.execSQL("insert into user(username,telephoneNumber) values(?,?)", new Object[]{userName, telephoneNumber});
                db.close();
                break;
            case R.id.selectBtn:
                db = myDbHelper.getReadableDatabase();
                //Cursor: 结果集,有一个游标,游标会指向结果集中的某一条记录。
                Cursor cursor = db.query("user", new String[]{"userName", "telephoneNumber"}, null, null, null, null, null, null);
                showInfo.setText("查询结果:\n");
                if (cursor.getCount()>0) {
                    Toast.makeText(this, "查询成功!!", Toast.LENGTH_SHORT).show();
                    while (cursor.moveToNext()) {
                        showInfo.append("姓名:" + cursor.getString(0) + "电话号码:" + cursor.getString(1) + "\n");
                    }
                } else {
                    Toast.makeText(this, "一条数据都没有呢~~", Toast.LENGTH_SHORT).show();
                }
                cursor.close();
                db.close();
                break;
            case R.id.conditional_query_btn:
                db=myDbHelper.getReadableDatabase();
                String conditionalQuery=conditionalQueryEt.getText().toString();
                Cursor cursor1=db.query("user",new String[]{"userName", "telephoneNumber"},"userName=?",new String[]{conditionalQuery},null,null,null,null);
                //Cursor cursor1 =db.rawQuery("select userName,telephoneNumber from user where userName=?",new String[]{conditionalQuery});
                showInfo.setText("查询结果:\n");
                if (cursor1.getCount()>0) {
                    Toast.makeText(this, "查询成功!!", Toast.LENGTH_SHORT).show();
                    while (cursor1.moveToNext()){
                        showInfo.append("姓名:" + cursor1.getString(0) + "电话号码:" + cursor1.getString(1) + "\n");
                    }
                }else{
                    Toast.makeText(this, "没找到呢~~", Toast.LENGTH_SHORT).show();
                }
                cursor1.close();
                db.close();
                break;
            case R.id.updateBtn:
                db=myDbHelper.getWritableDatabase();
                ContentValues contentValues1=new ContentValues();
                String userName1 = name.getText().toString();
                String telephoneNumber1= telNumber.getText().toString();
                String conditionalQuery2=conditionalQueryEt.getText().toString();
                contentValues1.put("userName", userName1);
                contentValues1.put("telephoneNumber", telephoneNumber1);
                int a=db.update("user",contentValues1,"userName=?",new String[]{conditionalQuery2});
                if (a>0) {
                    Toast.makeText(this, "更改成功,共更改了"+a+"条数据", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this, "更改失败", Toast.LENGTH_SHORT).show();
                }
                //db.execSQL("update user set userName=?,telephoneNumber=? where userName=?",new Object[]{userName1,telephoneNumber1,conditionalQuery2});
                db.close();
                break;
            case R.id.deleteBtn:
                db=myDbHelper.getWritableDatabase();
                String conditionalQuery1=conditionalQueryEt.getText().toString();
                //db.execSQL("delete from user where userName=?",new Object[]{conditionalQuery1});
                int i=db.delete("user","userName=?",new String[]{conditionalQuery1});//返回删除的条数。
                if(i>0)
                {
                    Toast.makeText(this, "删除成功,共删除了"+i+"条数据", Toast.LENGTH_SHORT).show();
                }else
                {
                    Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
                }
                db.close();
                break;
        }
    }

    class MyDbHelper extends SQLiteOpenHelper {


        public MyDbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL("create table user(user_id integer primary key autoincrement,userName varchar(10),telephoneNumber varchar(20))");
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
    }
}

 I have written two methods for adding, deleting, modifying and checking. The first is db.execSQL() to directly use SQL statements to operate, and the second is to use the method of adding, deleting, modifying and checking that comes with SQLiteDatabase: db.insert() db.query( ) db. update() db. delete().

Ha ha

Hahaha

I’m thinking about the tide, what is static braking?
The fog in the mountains and forests
, can it be used as a cover-up?
The trajectory of the moon and the stars is the same as that of the army.
Is Fenglin Volcano the most important thing to use?
In the end, it must be me who strategizes, and
you finally give up resistance.
I look up at the sunset,
you bow your head and leave sadly
Listen to me, winning or losing is the rule of the military,
you don’t have to worry about
it because I’m only suitable

Guess you like

Origin blog.csdn.net/qq_64628470/article/details/130274555