为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用具有信息的现有数据库创建登录活动

如何使用具有信息的现有数据库创建登录活动

幕布斯7119047 2022-07-27 09:10:31
我有现有的数据库,其中包含姓名、姓氏、电子邮件电话和密码,我想使用它来进行具有两个编辑文本(电子邮件或电话号码)和(密码)的登录活动。登录活动背后的代码是什么?登录(如果成功获取其他数据)---->ParentsMenu(使用数据)DatabaseHelper.javapackage edu.angelo.parentsportal;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import java.util.ArrayList;public class DatabaseHelper extends SQLiteOpenHelper {public static final String DATABASE_NAME = "Parents_Portal.db";public static final String TABLE_NAME = "Parents_Table";public static final String COL_0 = "ID";public static final String COL_1 = "NAME";public static final String COL_2 = "SURNAME";public static final String COL_3 = "EMAIL_ADDRESS";public static final String COL_4 = "PHONE_NUMBER";public static final String COL_5 = "PASSWORD";public DatabaseHelper(Context context) {    super(context, DATABASE_NAME, null, 1);}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("create table " + TABLE_NAME +"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, EMAIL_ADDRESS TEXT, PHONE_NUMBER TEXT, PASSWORD TEXT)");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);onCreate(db);}public boolean insertData(String name, String surname, String email_address, String phone_number, String password){    SQLiteDatabase db = this.getWritableDatabase();    ContentValues contentValues = new ContentValues();    contentValues.put(COL_1,name);    contentValues.put(COL_2,surname);    contentValues.put(COL_3,email_address);    contentValues.put(COL_4,phone_number);    contentValues.put(COL_5,password);    long result = db.insert(TABLE_NAME, null , contentValues);    if (result == -1) {        return false;    }    else {        return true;    }}
查看完整描述

2 回答

?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

Add this function to your DatabaseHelper.java


public boolean userExistance(String emailOrPhone, String pwd){


        String sql = "select * from " + TABLE_NAME + " where ("+ COL_3 +" = " + emailOrPhone +" OR "+ COL_4 +" = "+ emailOrPhone+ ") AND " + COL_5 + " = " + pwd;

        SQLiteDatabase mydb = this.getWritableDatabase();

        Cursor cursor = mydb.rawQuery(sql, null);

        if(cursor.getCount() > 0)

            return true;

        else

            return false;


    }


Then add call this function from your LoginActivity.java


DatabaseHelper mydb;

mydb = new DatabaseHelper(LoginActivity.this);

boolean exists = mydb.userExistance();

if(exists)

{

    Intent intent = new Intent(Login.this, Parent_Home.class);

    Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();

    startActivity(intent);

    finish();

}


查看完整回答
反对 回复 2022-07-27
?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

在您的课程中添加以下方法DatabaseHelper。


public ArrayList<ParentModel> getParentData(String emailOrPhone,String pwd){

ArrayList<ParentModel> list = new ArrayList<>();

String sql = "SELECT * FROM " + TABLE_NAME+" WHERE ("+COL_3+"= "+emailOrPhone+" OR "+COL_4 +" = "+emailOrPhone+") AND "+COL_5 +" = "+pwd;

SQLiteDatabase mydb = this.getWritableDatabase();

Cursor cursor = mydb.rawQuery(sql, null);

if (cursor.moveToFirst()) {

    do {

        ParentModel parentModel = new ParentModel();

        parentModel.setID(cursor.getString(0));

        parentModel.setName(cursor.getString(1));

        parentModel.setSurname(cursor.getString(2));

        parentModel.setEmail(cursor.getString(3));

        parentModel.setPhone_number(cursor.getString(4));

        parentModel.setPassword(cursor.getString(5));

        list.add(parentModel);

    }

    while (cursor.moveToNext());

}

   return list;

}

并使用如下代码:


if (mydb.getParentData(editTextEmailPhone.getText().toString(),editTextPassword.getText().toString()).size()>0) 

{

progressDialog.dismiss();

SharedPrefs.saveSharedSetting(this, "CaptainCode", "false");

Intent intent = new Intent(Login.this, Parent_Home.class);

Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();

startActivity(intent);

finish();

}

不要忘记初始化DatabaseHelper mydb。


查看完整回答
反对 回复 2022-07-27
  • 2 回答
  • 0 关注
  • 80 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信