欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?Studio中使用SQLite數(shù)據(jù)庫實(shí)現(xiàn)登錄和注冊功能

 更新時(shí)間:2024年06月24日 09:43:30   作者:可燃的冰和不可燃的心  
SQLite是一款輕型的數(shù)據(jù)庫,是遵守ACID的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它包含在一個(gè)相對小的C庫中,下面這篇文章主要給大家介紹了關(guān)于Android?Studio中使用SQLite數(shù)據(jù)庫實(shí)現(xiàn)登錄和注冊功能的相關(guān)資料,需要的朋友可以參考下

引言

在 Android 應(yīng)用程序中,管理用戶數(shù)據(jù)至關(guān)重要。SQLite 是 Android 中廣泛使用的輕量級數(shù)據(jù)庫,非常適合存儲和管理用戶登錄和注冊信息。本文將指導(dǎo)你如何在 Android Studio 中使用 SQLite 數(shù)據(jù)庫實(shí)現(xiàn)登錄和注冊功能。

創(chuàng)建 SQLite 數(shù)據(jù)庫

首先,你需要創(chuàng)建一個(gè) SQLite 數(shù)據(jù)庫來存儲用戶數(shù)據(jù)。為此,請執(zhí)行以下步驟:

  • 在你的項(xiàng)目中創(chuàng)建一個(gè)名為 DBHelper 的類,它擴(kuò)展自 SQLiteOpenHelper。
  • 在 DBHelper 類中,實(shí)現(xiàn) onCreate() 方法,該方法將在數(shù)據(jù)庫首次創(chuàng)建時(shí)調(diào)用。在 onCreate() 方法中,使用 execSQL() 方法創(chuàng)建名為 userInfo 的表,其中包含 uname(用戶名)和 psw(密碼)列。
  • 在 DBHelper 類中,還實(shí)現(xiàn) onUpgrade() 方法,該方法將在數(shù)據(jù)庫版本更改時(shí)調(diào)用。在這個(gè)方法中,你可以執(zhí)行必要的更新操作。

實(shí)現(xiàn)登錄功能

要實(shí)現(xiàn)登錄功能,請執(zhí)行以下步驟:

  • 在你的登錄活動中,獲取用戶名和密碼輸入框的引用。
  • 使用 DBHelper 類獲取數(shù)據(jù)庫的可寫實(shí)例。
  • 執(zhí)行 SQL 查詢以檢查輸入的用戶名和密碼是否與數(shù)據(jù)庫中的記錄匹配。
  • 如果查詢結(jié)果不為空,則表示找到了匹配的記錄,并且顯示 "登錄成功" 的消息。
  • 如果查詢結(jié)果為空,則顯示 "用戶名或密碼輸入錯(cuò)誤" 的消息。

實(shí)現(xiàn)注冊功能

要實(shí)現(xiàn)注冊功能,請執(zhí)行以下步驟:

  • 在你的注冊活動中,獲取用戶名和密碼輸入框的引用。
  • 使用 DBHelper 類獲取數(shù)據(jù)庫的可寫實(shí)例。
  • 檢查用戶名和密碼是否為空。如果為空,則顯示 "用戶名或密碼未輸入" 的消息。
  • 執(zhí)行 SQL 查詢以檢查輸入的用戶名是否已存在。
  • 如果查詢結(jié)果為空,則表示用戶名不存在,可以進(jìn)行注冊。
  • 使用 ContentValues 對象創(chuàng)建包含用戶名和密碼的新記錄。
  • 使用 insert() 方法將新記錄插入到數(shù)據(jù)庫中。
  • 顯示 "注冊成功" 的消息,并跳轉(zhuǎn)回登錄界面。

示例代碼

DBHelper.java

package com.example.loginandregister;

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

public class DBHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        //創(chuàng)建一個(gè)表
        String SQLstr = "create table userInfo(uname text,psw text)";
        db.execSQL(SQLstr);
        //往表里邊插入一個(gè)數(shù)據(jù)
        SQLstr = "insert into userInfo(uname,psw) values('admin','123456')";
        db.execSQL(SQLstr);
    }

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

    }
}

登錄界面設(shè)計(jì)

<?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">

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入用戶名" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入密碼"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登錄" />

    <TextView
        android:id="@+id/zhuce"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注冊" />

</LinearLayout>

登錄界面功能代碼

package com.example.login;

import android.content.Intent;
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;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 導(dǎo)入必要的包
        import android.database.sqlite.SQLiteDatabase;

        // 添加必要的權(quán)限
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

        // 創(chuàng)建 DBHelper 類的實(shí)例,用于管理名為 "test.db" 的 SQLite 數(shù)據(jù)庫
        DBHelper dbHelper = new DBHelper(MainActivity.this,"test.db",null,1);

        // 獲取數(shù)據(jù)庫的可寫實(shí)例
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        // 獲取用戶名和密碼輸入框的引用
        EditText unametxt = findViewById(R.id.username);
        EditText pswtxt = findViewById(R.id.password);

        // 處理登錄按鈕的點(diǎn)擊事件
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 獲取用戶名和密碼輸入框中的文本
                String uname = unametxt.getText().toString();
                String psw = pswtxt.getText().toString();

                // 執(zhí)行 SQL 查詢,以檢查輸入的用戶名和密碼是否與數(shù)據(jù)庫中的記錄匹配
                Cursor cursor =  db.query("userInfo",new String[]{"uname,psw"},"uname=? and psw=?",new String[]{uname,psw},null,null,null);

                // 檢查查詢結(jié)果是否為空
                if(cursor.moveToFirst()) {
                    // 如果結(jié)果不為空,則表示找到了匹配的記錄,并且顯示 "登錄成功" 的 Toast 消息
                    Toast.makeText(MainActivity.this,"登錄成功",Toast.LENGTH_SHORT).show();
                } else {
                    // 如果結(jié)果為空,則顯示 "用戶名或密碼輸入錯(cuò)誤" 的 Toast 消息
                    Toast.makeText(MainActivity.this,"用戶名或密碼輸入錯(cuò)誤",Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 處理注冊按鈕的點(diǎn)擊事件
        TextView zhuce = findViewById(R.id.zhuce);
        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 創(chuàng)建到 MainActivity2 類的意圖并啟動該活動
                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });
    }
}

注冊界面設(shè)計(jì)

<?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">

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入用戶名" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入密碼"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注冊" />

</LinearLayout>

注冊界面功能代碼

package com.example.login;

import android.content.Intent;
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.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        // 導(dǎo)入必要的包
        import android.database.sqlite.SQLiteDatabase;

        // 添加必要的權(quán)限
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

        // 創(chuàng)建 DBHelper 類的實(shí)例,用于管理名為 "test.db" 的 SQLite 數(shù)據(jù)庫
        DBHelper dbHelper = new DBHelper(MainActivity2.this,"test.db",null,1);

        // 獲取數(shù)據(jù)庫的可寫實(shí)例
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        // 獲取用戶名和密碼輸入框的引用
        EditText unametxt = findViewById(R.id.username);
        EditText pswtxt = findViewById(R.id.password);

        // 處理注冊按鈕的點(diǎn)擊事件
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 獲取用戶名和密碼輸入框中的文本
                String uname = unametxt.getText().toString();
                String psw = pswtxt.getText().toString();

                // 執(zhí)行 SQL 查詢,以檢查輸入的用戶名是否已存在
                Cursor cursor =  db.query("userInfo",new String[]{"uname"},"uname=?",new String[]{uname},null,null,null);

                // 檢查查詢結(jié)果是否為空
                if(cursor.moveToFirst()) {
                    // 如果結(jié)果不為空,則表示用戶名已存在,并且顯示 "用戶名已存在" 的 Toast 消息
                    Toast.makeText(MainActivity2.this,"用戶名已存在",Toast.LENGTH_SHORT).show();
                } else {
                    // 如果結(jié)果為空,則表示用戶名不存在,并且執(zhí)行 SQL 插入語句,將用戶名和密碼插入數(shù)據(jù)庫
                    String SQLstr = "insert into userInfo(uname,psw) values(?,?)";
                    db.execSQL(SQLstr,new String[]{uname,psw});

                    // 顯示 "注冊成功" 的 Toast 消息
                    Toast.makeText(MainActivity2.this,"注冊成功",Toast.LENGTH_SHORT).show();

                    // 創(chuàng)建到 MainActivity 類的意圖并啟動該活動
                    Intent intent = new Intent(MainActivity2.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        });
    }
}

通過使用 SQLite 數(shù)據(jù)庫,我們成功地實(shí)現(xiàn)了 Android 應(yīng)用程序中的登錄和注冊功能。本教程旨在為開發(fā)者提供一個(gè)循序漸進(jìn)的指南,幫助他們輕松地將這些功能集成到自己的應(yīng)用程序中。希望這篇文章能為廣大開發(fā)者帶來幫助,使他們能夠?yàn)橛脩籼峁┌踩覠o縫的登錄和注冊體驗(yàn)。

總結(jié)

到此這篇關(guān)于Android Studio中使用SQLite數(shù)據(jù)庫實(shí)現(xiàn)登錄和注冊功能的文章就介紹到這了,更多相關(guān)AndroidStudio用SQLite實(shí)現(xiàn)登錄和注冊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論