Android Studio 中使用 SQLite 数据库实现登录和注册

[复制链接]
发表于 2024-6-20 14:19:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
引言
在 Android 应用步伐中,管理用户数据至关重要。SQLite 是 Android 中广泛使用的轻量级数据库,非常得当存储和管理用户登录和注册信息。本文将指导你如安在 Android Studio 中使用 SQLite 数据库实现登录和注册功能
创建 SQLite 数据库
首先,你需要创建一个 SQLite 数据库来存储用户数据。为此,请执行以下步骤:

  • 在你的项目中创建一个名为 DBHelper 的类,它扩展自 SQLiteOpenHelper。
  • 在 DBHelper 类中,实现 onCreate() 方法,该方法将在数据库初次创建时调用。在 onCreate() 方法中,使用 execSQL() 方法创建名为 userInfo 的表,其中包含 uname(用户名)和 psw(暗码)列。
  • 在 DBHelper 类中,还实现 onUpgrade() 方法,该方法将在数据库版本更改时调用。在这个方法中,你可以执行须要的更新操作。
实现登录功能
要实现登录功能,请执行以下步骤:

  • 在你的登录运动中,获取用户名和暗码输入框的引用。
  • 使用 DBHelper 类获取数据库的可写实例。
  • 执行 SQL 查询以查抄输入的用户名和暗码是否与数据库中的记载匹配。
  • 如果查询效果不为空,则表示找到了匹配的记载,而且显示 "登录成功" 的消息。
  • 如果查询效果为空,则显示 "用户名或暗码输入错误" 的消息。
实现注册功能
要实现注册功能,请执行以下步骤:

  • 在你的注册运动中,获取用户名和暗码输入框的引用。
  • 使用 DBHelper 类获取数据库的可写实例。
  • 查抄用户名和暗码是否为空。如果为空,则显示 "用户名或暗码未输入" 的消息。
  • 执行 SQL 查询以查抄输入的用户名是否已存在。
  • 如果查询效果为空,则表示用户名不存在,可以进行注册。
  • 使用 ContentValues 对象创建包含用户名和暗码的新记载。
  • 使用 insert() 方法将新记载插入到数据库中。
  • 显示 "注册成功" 的消息,并跳转回登录界面。
示例代码
DBHelper.java
  1. package com.example.loginandregister;
  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. public class DBHelper extends SQLiteOpenHelper {
  6.     public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
  7.         super(context, name, factory, version);
  8.     }
  9.     @Override
  10.     public void onCreate(SQLiteDatabase db) {
  11.         //创建一个表
  12.         String SQLstr = "create table userInfo(uname text,psw text)";
  13.         db.execSQL(SQLstr);
  14.         //往表里边插入一个数据
  15.         SQLstr = "insert into userInfo(uname,psw) values('admin','123456')";
  16.         db.execSQL(SQLstr);
  17.     }
  18.     @Override
  19.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  20.     }
  21. }
复制代码
登录界面计划
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">
  6.     <EditText
  7.         android:id="@+id/username"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:hint="请输入用户名" />
  11.     <EditText
  12.         android:id="@+id/password"
  13.         android:layout_width="match_parent"
  14.         android:layout_height="wrap_content"
  15.         android:hint="请输入密码"
  16.         android:inputType="textPassword" />
  17.     <Button
  18.         android:id="@+id/btn"
  19.         android:layout_width="match_parent"
  20.         android:layout_height="wrap_content"
  21.         android:text="登录" />
  22.     <TextView
  23.         android:id="@+id/zhuce"
  24.         android:layout_width="match_parent"
  25.         android:layout_height="wrap_content"
  26.         android:text="注册" />
  27. </LinearLayout>
复制代码
登录界面功能代码
  1. package com.example.login;
  2. import android.content.Intent;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11. import androidx.appcompat.app.AppCompatActivity;
  12. public class MainActivity extends AppCompatActivity {
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.         // 导入必要的包
  18.         import android.database.sqlite.SQLiteDatabase;
  19.         // 添加必要的权限
  20.         <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  21.         // 创建 DBHelper 类的实例,用于管理名为 "test.db" 的 SQLite 数据库
  22.         DBHelper dbHelper = new DBHelper(MainActivity.this,"test.db",null,1);
  23.         // 获取数据库的可写实例
  24.         SQLiteDatabase db = dbHelper.getWritableDatabase();
  25.         // 获取用户名和密码输入框的引用
  26.         EditText unametxt = findViewById(R.id.username);
  27.         EditText pswtxt = findViewById(R.id.password);
  28.         // 处理登录按钮的点击事件
  29.         Button btn = findViewById(R.id.btn);
  30.         btn.setOnClickListener(new View.OnClickListener() {
  31.             @Override
  32.             public void onClick(View v) {
  33.                 // 获取用户名和密码输入框中的文本
  34.                 String uname = unametxt.getText().toString();
  35.                 String psw = pswtxt.getText().toString();
  36.                 // 执行 SQL 查询,以检查输入的用户名和密码是否与数据库中的记录匹配
  37.                 Cursor cursor =  db.query("userInfo",new String[]{"uname,psw"},"uname=? and psw=?",new String[]{uname,psw},null,null,null);
  38.                 // 检查查询结果是否为空
  39.                 if(cursor.moveToFirst()) {
  40.                     // 如果结果不为空,则表示找到了匹配的记录,并且显示 "登录成功" 的 Toast 消息
  41.                     Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
  42.                 } else {
  43.                     // 如果结果为空,则显示 "用户名或密码输入错误" 的 Toast 消息
  44.                     Toast.makeText(MainActivity.this,"用户名或密码输入错误",Toast.LENGTH_SHORT).show();
  45.                 }
  46.             }
  47.         });
  48.         // 处理注册按钮的点击事件
  49.         TextView zhuce = findViewById(R.id.zhuce);
  50.         zhuce.setOnClickListener(new View.OnClickListener() {
  51.             @Override
  52.             public void onClick(View v) {
  53.                 // 创建到 MainActivity2 类的意图并启动该活动
  54.                 Intent intent = new Intent(MainActivity.this,MainActivity2.class);
  55.                 startActivity(intent);
  56.             }
  57.         });
  58.     }
  59. }
复制代码
注册界面计划
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">
  6.     <EditText
  7.         android:id="@+id/username"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:hint="请输入用户名" />
  11.     <EditText
  12.         android:id="@+id/password"
  13.         android:layout_width="match_parent"
  14.         android:layout_height="wrap_content"
  15.         android:hint="请输入密码"
  16.         android:inputType="textPassword" />
  17.     <Button
  18.         android:id="@+id/btn"
  19.         android:layout_width="match_parent"
  20.         android:layout_height="wrap_content"
  21.         android:text="注册" />
  22. </LinearLayout>
复制代码
注册界面功能代码
  1. package com.example.login;
  2. import android.content.Intent;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10. import androidx.appcompat.app.AppCompatActivity;
  11. public class MainActivity2 extends AppCompatActivity {
  12.     @Override
  13.     protected void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.activity_main2);
  16.         // 导入必要的包
  17.         import android.database.sqlite.SQLiteDatabase;
  18.         // 添加必要的权限
  19.         <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  20.         // 创建 DBHelper 类的实例,用于管理名为 "test.db" 的 SQLite 数据库
  21.         DBHelper dbHelper = new DBHelper(MainActivity2.this,"test.db",null,1);
  22.         // 获取数据库的可写实例
  23.         SQLiteDatabase db = dbHelper.getWritableDatabase();
  24.         // 获取用户名和密码输入框的引用
  25.         EditText unametxt = findViewById(R.id.username);
  26.         EditText pswtxt = findViewById(R.id.password);
  27.         // 处理注册按钮的点击事件
  28.         Button btn = findViewById(R.id.btn);
  29.         btn.setOnClickListener(new View.OnClickListener() {
  30.             @Override
  31.             public void onClick(View v) {
  32.                 // 获取用户名和密码输入框中的文本
  33.                 String uname = unametxt.getText().toString();
  34.                 String psw = pswtxt.getText().toString();
  35.                 // 执行 SQL 查询,以检查输入的用户名是否已存在
  36.                 Cursor cursor =  db.query("userInfo",new String[]{"uname"},"uname=?",new String[]{uname},null,null,null);
  37.                 // 检查查询结果是否为空
  38.                 if(cursor.moveToFirst()) {
  39.                     // 如果结果不为空,则表示用户名已存在,并且显示 "用户名已存在" 的 Toast 消息
  40.                     Toast.makeText(MainActivity2.this,"用户名已存在",Toast.LENGTH_SHORT).show();
  41.                 } else {
  42.                     // 如果结果为空,则表示用户名不存在,并且执行 SQL 插入语句,将用户名和密码插入数据库
  43.                     String SQLstr = "insert into userInfo(uname,psw) values(?,?)";
  44.                     db.execSQL(SQLstr,new String[]{uname,psw});
  45.                     // 显示 "注册成功" 的 Toast 消息
  46.                     Toast.makeText(MainActivity2.this,"注册成功",Toast.LENGTH_SHORT).show();
  47.                     // 创建到 MainActivity 类的意图并启动该活动
  48.                     Intent intent = new Intent(MainActivity2.this,MainActivity.class);
  49.                     startActivity(intent);
  50.                 }
  51.             }
  52.         });
  53.     }
  54. }
复制代码
通过使用 SQLite 数据库,我们成功地实现了 Android 应用步伐中的登录和注册功能。本教程旨在为开辟者提供一个循规蹈矩的指南,资助他们轻松地将这些功能集成到自己的应用步伐中。希望这篇文章能为广大开辟者带来资助,使他们能够为用户提供安全且无缝的登录和注册体验。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表