Android?Studio中使用SQLite數(shù)據(jù)庫實(shí)現(xiàn)登錄和注冊(cè)功能
引言
在 Android 應(yīng)用程序中,管理用戶數(shù)據(jù)至關(guān)重要。SQLite 是 Android 中廣泛使用的輕量級(jí)數(shù)據(jù)庫,非常適合存儲(chǔ)和管理用戶登錄和注冊(cè)信息。本文將指導(dǎo)你如何在 Android Studio 中使用 SQLite 數(shù)據(jù)庫實(shí)現(xiàn)登錄和注冊(cè)功能。
創(chuàng)建 SQLite 數(shù)據(jù)庫
首先,你需要?jiǎng)?chuàng)建一個(gè) SQLite 數(shù)據(jù)庫來存儲(chǔ)用戶數(shù)據(jù)。為此,請(qǐng)執(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)登錄功能,請(qǐng)執(zhí)行以下步驟:
- 在你的登錄活動(dòng)中,獲取用戶名和密碼輸入框的引用。
- 使用
DBHelper類獲取數(shù)據(jù)庫的可寫實(shí)例。 - 執(zhí)行 SQL 查詢以檢查輸入的用戶名和密碼是否與數(shù)據(jù)庫中的記錄匹配。
- 如果查詢結(jié)果不為空,則表示找到了匹配的記錄,并且顯示 "登錄成功" 的消息。
- 如果查詢結(jié)果為空,則顯示 "用戶名或密碼輸入錯(cuò)誤" 的消息。
實(shí)現(xiàn)注冊(cè)功能
要實(shí)現(xiàn)注冊(cè)功能,請(qǐng)執(zhí)行以下步驟:
- 在你的注冊(cè)活動(dòng)中,獲取用戶名和密碼輸入框的引用。
- 使用
DBHelper類獲取數(shù)據(jù)庫的可寫實(shí)例。 - 檢查用戶名和密碼是否為空。如果為空,則顯示 "用戶名或密碼未輸入" 的消息。
- 執(zhí)行 SQL 查詢以檢查輸入的用戶名是否已存在。
- 如果查詢結(jié)果為空,則表示用戶名不存在,可以進(jìn)行注冊(cè)。
- 使用
ContentValues對(duì)象創(chuàng)建包含用戶名和密碼的新記錄。 - 使用
insert()方法將新記錄插入到數(shù)據(jù)庫中。 - 顯示 "注冊(cè)成功" 的消息,并跳轉(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="請(qǐng)輸入用戶名" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入密碼"
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="注冊(cè)" />
</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();
}
}
});
// 處理注冊(cè)按鈕的點(diǎn)擊事件
TextView zhuce = findViewById(R.id.zhuce);
zhuce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 創(chuàng)建到 MainActivity2 類的意圖并啟動(dòng)該活動(dòng)
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}
});
}
}
注冊(cè)界面設(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="請(qǐng)輸入用戶名" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入密碼"
android:inputType="textPassword" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注冊(cè)" />
</LinearLayout>
注冊(cè)界面功能代碼
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);
// 處理注冊(cè)按鈕的點(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});
// 顯示 "注冊(cè)成功" 的 Toast 消息
Toast.makeText(MainActivity2.this,"注冊(cè)成功",Toast.LENGTH_SHORT).show();
// 創(chuàng)建到 MainActivity 類的意圖并啟動(dòng)該活動(dòng)
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
startActivity(intent);
}
}
});
}
}
通過使用 SQLite 數(shù)據(jù)庫,我們成功地實(shí)現(xiàn)了 Android 應(yīng)用程序中的登錄和注冊(cè)功能。本教程旨在為開發(fā)者提供一個(gè)循序漸進(jìn)的指南,幫助他們輕松地將這些功能集成到自己的應(yīng)用程序中。希望這篇文章能為廣大開發(fā)者帶來幫助,使他們能夠?yàn)橛脩籼峁┌踩覠o縫的登錄和注冊(cè)體驗(yàn)。
總結(jié)
到此這篇關(guān)于Android Studio中使用SQLite數(shù)據(jù)庫實(shí)現(xiàn)登錄和注冊(cè)功能的文章就介紹到這了,更多相關(guān)AndroidStudio用SQLite實(shí)現(xiàn)登錄和注冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊(cè)實(shí)現(xiàn)
- Android Studio連接MySql實(shí)現(xiàn)登錄注冊(cè)(附源代碼)
- Android Studio實(shí)現(xiàn)注冊(cè)頁面跳轉(zhuǎn)登錄頁面的創(chuàng)建
- Android?studio?利用共享存儲(chǔ)進(jìn)行用戶的注冊(cè)和登錄驗(yàn)證功能
- Android Studio實(shí)現(xiàn)QQ的注冊(cè)登錄和好友列表跳轉(zhuǎn)
- Android Studio+Servlet+MySql實(shí)現(xiàn)登錄注冊(cè)
相關(guān)文章
Android開發(fā)中ListView自定義adapter的封裝
這篇文章主要為大家詳細(xì)介紹了android開發(fā)中ListView自定義adapter的封裝,ListView的模板寫法,感興趣的小伙伴們可以參考一下2016-09-09
使用TransitionDrawable實(shí)現(xiàn)多張圖片淡入淡出效果
這篇文章主要為大家詳細(xì)介紹了使用TransitionDrawable實(shí)現(xiàn)多張圖片淡入淡出效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android 微信6.1 tab欄圖標(biāo)和字體顏色漸變的實(shí)現(xiàn)
本文主要對(duì)微信6.1 tab 欄顏色漸變效果的實(shí)現(xiàn)全過程進(jìn)行分析介紹,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2016-12-12
利用SurfaceView實(shí)現(xiàn)下雨與下雪動(dòng)畫效果詳解(Kotlin語法)
這篇文章主要給大家介紹了關(guān)于利用SurfaceView實(shí)現(xiàn)下雨與下雪動(dòng)畫效果的相關(guān)資料,需要一些基本的View知識(shí)和會(huì)一些基礎(chǔ)Kotlin語法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
在Android上實(shí)現(xiàn)HttpServer的示例代碼
本篇文章主要介紹了在Android上實(shí)現(xiàn)HttpServer的示例代碼,實(shí)現(xiàn)Android本地的微型服務(wù)器,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Android Moveview滑屏移動(dòng)視圖類完整實(shí)例
這篇文章主要介紹了Android Moveview滑屏移動(dòng)視圖類,很有實(shí)用價(jià)值,需要的朋友可以參考下2014-07-07
android獲得當(dāng)前view在屏幕中坐標(biāo)的方法
這篇文章主要介紹了android獲得當(dāng)前view在屏幕中坐標(biāo)的方法,涉及Android針對(duì)view坐標(biāo)相關(guān)屬性的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10

