android - 显示具有特定登录ID的数据库中的数据?

时间:2017-01-14 19:35:37

标签: android android-fragments

我是android新手。我正在使用有两个表的passwordsaver应用程序。第一个表是显示标题,电子邮件和密码的数据。第二个表是添加新用户然后他们可以登录的用户。

我想根据他的身份证明每个用户特定的标题,电子邮件和密码数据?

代码

public class Constants
{
    //id
    static final String ROW_ID="id";
    private static final String ROW_ID_USER="u_id";

    //COLUMNS USER
    static final String USER_EMAIL = "user_email";
    static final String USER_PASSWORD = "user_password";

    //COLUMNS Data
    static final String TITLE = "title";
    static final String EMAIL = "email";
    static final String PASSWORD = "password";

    //DB NAME & Version
    static final String DB_NAME="my_DB";
    static final int DB_VERSION='3';

    //TABLE USER
    static final String TB_NAME_USER="u_TB";

    //TABLE DATA
    static final String TB_NAME="d_TB";

    //CREATING USER TABLE
    static final String CREATE_TB= "CREATE TABLE "
            + TB_NAME + "(" + ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + TITLE + " TEXT, " + EMAIL + " TEXT, "
            + PASSWORD + " TEXT, " + ROW_ID_USER + " INT, "
            + "FOREIGN KEY(" + ROW_ID_USER + ") REFERENCES "
            + TB_NAME_USER + "(id) " + ")";

    static final String CREATE_TB_USER = "CREATE TABLE "
            + TB_NAME_USER + "(" + ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + USER_EMAIL + " TEXT, " + USER_PASSWORD + " TEXT "
            + ")";

    //INSERT for data
    public long add(String title, String email, String pass)
    {
        try
        {
            ContentValues cv=new ContentValues();
            cv.put(Constants.TITLE, title);
            cv.put(Constants.EMAIL,email);
            cv.put(Constants.PASSWORD,pass);

            return db.insert(Constants.TB_NAME,Constants.ROW_ID,cv);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

        return 0;
    }

    //update for data
    public long UPDATE(int id, String email, String pass)
    {
        try
        {
            ContentValues cv=new ContentValues();
            cv.put(Constants.EMAIL,email);
            cv.put(Constants.PASSWORD, pass);
            return db.update(Constants.TB_NAME,cv,Constants.ROW_ID+" =?",new String[]{String.valueOf(id)});
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    //newUserSignUp
    public long insert(String email,String password)
    {
        ContentValues contentValue = new ContentValues();
        contentValue.put(Constants.USER_EMAIL, email);
        contentValue.put(Constants.USER_PASSWORD, password);
        long id = db.insert(Constants.TB_NAME_USER, Constants.ROW_ID, contentValue);
        return id;
    }

    //deleteData
    public boolean delete(int id)
    {
        try
        {
            int res = db.delete(Constants.TB_NAME,Constants.ROW_ID+" =?",new String[]{String.valueOf(id)});
            if (res>0)
            {
                return true;
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return false;
    }

    //RETRIEVE Data
    public Cursor getAllPlayers()
    {
        String[] columns={Constants.ROW_ID,Constants.TITLE,Constants.EMAIL,Constants.PASSWORD};

        return db.query(Constants.TB_NAME,columns,null,null,null,null,null);
    }

    //getSigleEntry for User Login
    public String getSingleEntry(String  email)
    {
        String[] selectionArgs ={email};
        Cursor cursor = db.query(Constants.TB_NAME_USER,null,Constants.USER_EMAIL + " =?",selectionArgs,null,null,null,null);

        if (cursor.getCount()<1)
        {
            cursor.close();
            return "NOT EXIST";
        }

        cursor.moveToFirst();
        String password = cursor.getString(cursor.getColumnIndex(Constants.USER_PASSWORD));
        cursor.close();
        return password;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试这个。

public long add(String title, String email, String pass,long id)
{
    try
    {
        ContentValues cv=new ContentValues();
        cv.put(Constants.TITLE, title);
        cv.put(Constants.EMAIL,email);
        cv.put(Constants.PASSWORD,pass);
        cv.put("user_id",id);

        return db.insert(Constants.TB_NAME,null,cv);
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }

    return 0;
}

}

当您插入表格数据时,您也会像这样传递ID

 public Cursor getAllPlayers(long id)
{
    String[] columns={Constants.ROW_ID,Constants.TITLE,Constants.EMAIL,Constants.PASSWORD};
    String[] selectionArgs={String.valueOf(id)};

        return db.query(Constants.TB_NAME,columns,"user_id" + " =?",selectionArgs,null,null,Constants.ROW_ID+" Desc");

}

根据u_id使用此查询获取数据并传递id

{{1}}