天天看點

[Android Studio]Android 資料存儲--SQLite資料庫存儲📋筆記目錄💯實戰演練--基于SQLite資料庫的通訊錄實作資料的增删改查🚩結尾

 🟧🟨🟩🟦🟪 Android Debug🟧🟨🟩🟦🟪

Topic 

釋出安卓學習過程中遇到問題解決過程,希望我的解決方案可以對小夥伴們有幫助。

[Android Studio]Android 資料存儲--SQLite資料庫存儲📋筆記目錄💯實戰演練--基于SQLite資料庫的通訊錄實作資料的增删改查🚩結尾

📋筆記目錄

💯實戰演練--基于SQLite資料庫的通訊錄實作資料的增删改查

1,建立程式

2,放置界面控件

3,編寫界面互動代碼

4, 核心方法講解

5,資料庫的建立及初始化

6,運作程式

🚩結尾

💯實戰演練--基于SQLite資料庫的通訊錄實作資料的增删改查

1,建立程式

建立一個名為Directory的應用程式,指定報名為cn.example.directory。

2,放置界面控件

在activity_main.xml布局檔案中放置三個TextView控件,分别用于顯示"姓名"文本、“電話”文本以及顯示儲存的姓名和電話資訊,兩個EditText控件分别用于顯示姓名的輸入框與電話的輸入框,四個Button控件分别用于顯示添加按鈕、查詢按鈕、修改按鈕以及删除按鈕。

[Android Studio]Android 資料存儲--SQLite資料庫存儲📋筆記目錄💯實戰演練--基于SQLite資料庫的通訊錄實作資料的增删改查🚩結尾
<?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"
    tools:context=".MainActivity"
    android:padding="16dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="130dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓 名:"
            android:textSize="18sp" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et_name"
            android:hint="請輸入姓名:"
            android:textSize="16sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="電 話:"
            android:textSize="18sp"/>

        <EditText
            android:id="@+id/et_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="請輸入手機号碼:"
            android:textSize="16sp"/>
    </LinearLayout>

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

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/btn_add"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="#B9B9FF"
            android:text="添加"
            android:textSize="18sp"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/btn_query"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="#DCB5FF"
            android:text="查詢"
            android:textSize="18sp"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/ben_update"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="#E6CAFF"
            android:text="修改"
            android:textSize="18sp" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/btn_delete"
            android:layout_weight="1"
            android:background="#ACD6FF"
            android:text="删除"
            android:textSize="18sp"/>

    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_show"
        android:layout_marginTop="25dp"
        android:textSize="20sp"/>

</LinearLayout>
           

3,編寫界面互動代碼

在MainActivity中編寫邏輯代碼,實作聯系人資訊的添加、查詢、修改以及删除功能,由于通訊錄界面上的添加、查詢、修改、删除按鈕需要設定點選事件,是以将MainActivity實作OnClickListener接口,并重寫Onclick方法,在該方法中實作這四個按鈕的點選事件。

package com.example.directory;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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 {

    MyHelper myHelper;
    private EditText mEtName;
    private EditText mEtphone;
    private TextView mTvShow;
    private Button mBtnAdd;
    private Button mBtnQuery;
    private Button mBtnUpdate;
    private Button mBtnDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myHelper = new MyHelper(this);
        init();
    }

    private void init() {
        mEtName = findViewById(R.id.et_name);
        mEtphone = findViewById(R.id.et_phone);
        mTvShow = findViewById(R.id.tv_show);
        mBtnAdd = findViewById(R.id.btn_add);
        mBtnQuery = findViewById(R.id.btn_query);
        mBtnUpdate = findViewById(R.id.ben_update);
        mBtnDelete = findViewById(R.id.btn_delete);
        mBtnAdd.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
        mBtnUpdate.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 = mEtphone.getText().toString();
                db = myHelper.getWritableDatabase();
                values = new ContentValues();
                values.put("name",name);
                values.put("phone",phone);
                db.insert("information",null,values);
                Toast.makeText(this,"資訊已添加",Toast.LENGTH_SHORT).show();
                db.close();
                break;
            case R.id.btn_query:
                db = myHelper.getReadableDatabase();
                Cursor cursor = db.query("information",null,null,null,null,null,null);
                if (cursor.getCount() == 0){
                    mTvShow.setText("");
                    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.ben_update:
                db = myHelper.getWritableDatabase();
                values = new ContentValues();
                values.put("phone",phone = mEtphone.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 =myHelper.getWritableDatabase();
                db.delete("information",null,null);
                Toast.makeText(this,"資訊已删除",Toast.LENGTH_SHORT).show();
                mTvShow.setText("");
                db.close();
                break;
        }

    }
}
           

4, 核心方法講解

在上述代碼中,一開始建立了一個init()方法,用于初始化界面控件并設定添加,查詢,修改,删除按鈕的點選監聽事件。

[Android Studio]Android 資料存儲--SQLite資料庫存儲📋筆記目錄💯實戰演練--基于SQLite資料庫的通訊錄實作資料的增删改查🚩結尾

 通過SQLiteDatabase類的insert()方法将姓名和電話資訊添加到資料庫中。

 通過SQLiteDatabase類的query()方法将資料庫中的姓名和電話資訊查詢出來,并顯示在界面中。

 通過SQLiteDatabase類的update()方法修改資料庫中的姓名和電話資訊。

 通過SQLiteDatabase類的delete()方法删除資料庫中的姓名和電話資訊。

 通過SQLiteDatabase類的execSQL()方法建立表information。

5,資料庫的建立及初始化

package com.example.directory;

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

import androidx.annotation.Nullable;

class MyHelper extends SQLiteOpenHelper {

    public MyHelper(Context context) {
        super(context, "incast.db", null, 1);
    }

   // 資料庫第一次被建立時調用該方法
    @Override
    public void onCreate(SQLiteDatabase db) {
        //初始化資料庫的表結構,執行一條建表的SQL語句
        db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20), phone VARCHAR(20))");
    }
    //當資料庫的版本号增加時調用
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
           

6,運作程式

運作上述程式:

輸入兩條聯系人資訊,點選添加按鈕運作,結果如圖所示:

[Android Studio]Android 資料存儲--SQLite資料庫存儲📋筆記目錄💯實戰演練--基于SQLite資料庫的通訊錄實作資料的增删改查🚩結尾

點選查詢按鈕,會發現添加的聯系人資訊在界面中顯示:

[Android Studio]Android 資料存儲--SQLite資料庫存儲📋筆記目錄💯實戰演練--基于SQLite資料庫的通訊錄實作資料的增删改查🚩結尾

重新輸入一個人的聯系電話,點選修改按鈕,然後再進行查詢,會發現聯系人電話已經修改成功:

[Android Studio]Android 資料存儲--SQLite資料庫存儲📋筆記目錄💯實戰演練--基于SQLite資料庫的通訊錄實作資料的增删改查🚩結尾

點選删除按鈕,會将資料庫中所有聯系人進行删除:

[Android Studio]Android 資料存儲--SQLite資料庫存儲📋筆記目錄💯實戰演練--基于SQLite資料庫的通訊錄實作資料的增删改查🚩結尾

🚩結尾

至此,檔案存儲的相關知識已講解完成,該知識所用到的核心技術是利用I/O流來進行檔案讀寫操作,其中,Context類中提供的openFileInput()和OpenFileOutput()方法的用法,一定要掌握。 

[Android Studio]Android 資料存儲--SQLite資料庫存儲📋筆記目錄💯實戰演練--基于SQLite資料庫的通訊錄實作資料的增删改查🚩結尾

🎁歡迎各位→點贊👍 + 收藏⭐️ + 留言📝​

🌈寫給讀者:很高興你能看到我的文章,希望我的文章可以幫助到你,祝萬事順意🏳️‍🌈