【问题标题】:Android: How to call and save userID as a session from SQLite database?Android:如何从 SQLite 数据库调用用户 ID 并将其保存为会话?
【发布时间】:2013-11-10 13:21:35
【问题描述】:

在我的应用程序中,我试图弄清楚如何保存用户 ID 会话并在用户关闭应用程序之前使用它。

我知道在 PHP 中你可以只使用 $_SESSION['userID'] = $user_id

我想在 Android SQLite 中运行类似这样的查询

SELECT user_id, username, password
FROM users
**WHERE user_id = $_SESSION[user_id]**

这是来自 php,有什么方法可以在 android 中做到这一点吗?我听说过共享偏好,但我不确定它是如何工作的?

抱歉,我刚开始学习 Android 编程。我有点菜鸟。如果你能在这里帮助我,我将不胜感激。

编辑:这个用户 ID 是我将从 SQLite 数据库调用并在用户登录时将其保存为会话的列。

【问题讨论】:

    标签: php android mysql sqlite session


    【解决方案1】:

    我正在使用 AppPreferences 在我的 android 应用程序中做类似的事情,这是我创建的一个类的示例,用于保存电子邮件以供以后在应用程序中使用

    public class AppPreferences {
    public static final String ApplicationPreferences = "com.akada.danceworksonline.studio.MainActivity";
    public String putEmail = "email";
    private SharedPreferences sharedPrefs;
    private SharedPreferences.Editor prefsEditor;
    
    public AppPreferences(Context context) {
    
        this.sharedPrefs = context.getSharedPreferences(ApplicationPreferences,
                0);
        this.prefsEditor = sharedPrefs.edit();
    
    }
    
    public String getEmailPref() {
        return sharedPrefs.getString(putEmail, "");
    }
    
    public void saveEmail(String text) {
        prefsEditor.putString(putEmail, text);
        prefsEditor.commit();
    }
    

    无论您在哪里调用数据库,都会有类似的内容。

    AppPreferences _appPrefs = new AppPreferences(getApplicationContext());
    

    然后您可以执行 _appPrefs.saveEmail(textviewEmail) 之类的操作,或者您可以得到您的价值。

    【讨论】:

    • 谢谢!我得到了我想要的东西;但是,我该如何使用 SQLite 数据库来实现呢?
    • 我没有使用 SQLite,所以你自己在那里。如果我提供帮助,我将不胜感激:)
    • 对不起,我没有足够的声望来添加投票。 :(
    【解决方案2】:

    首先您必须声明全局共享首选项:

    SharedPreferences sp;
    

    然后在 onCreate 你 delcare sp:

    sp = getSharedPreferences("Session", 0); //"Session" is the file name it MUST be the same in all activities that require shared preferences. 
    

    因此,基本上当用户登录时,您会检查他是否已登录,如果是,则使用以下命令将他的 ID 保存在共享首选项中:

    SharedPreferences.Editor e = sp.edit();
                    e.putInt("ID", ID-Value-that-you-used-it-to-check-for-ID);
                    e.commit();
    

    在 onCreate 的登录类中,添加第二个块来检查用户是否已登录并且他从未注销过。如果他是的话,就带他去下一个活动。

    if (cID != 0)
        {
            Intent iLogged = new Intent(this, The-Next-Class.class);
            finish();
            startActivity(iLogged);
        }
    

    每当您想检索刚刚添加的值时...

    int ID = sp.getInt("ID", 0);
    

    要注销用户并删除会话,请使用以下命令:

    sp.edit().clear().commit();
            Intent iLogout = new Intent(this,   Login.class);
            finish();
            startActivity(iLogout);
            break;
    

    编辑 2:

    你要做的是……

    1) 从共享首选项中检索 ID.. 现在如果 UserID 列是 int 然后使用。

    int ID = sp.getInt("ID", 0); // 0 is default value which means if couldn't find the value in the shared preferences file give it a value of 0.
    

    2) 您必须检查,如果 ID != 0 则将值传递给查询...

    SELECT user_id, username, password
    FROM users
    WHERE user_id = ID
    

    【讨论】:

    • 此用户 ID 是我将从 SQLite 数据库调用并在用户登录时将其保存为会话的列。您知道如何继续这样做吗?
    • 检查编辑部分希望你明白,如果没有添加代码来帮助你更多
    • 如何将用户的user_id列声明为sql数据库中的int? e.putInt("ID", 不确定这部分);见上面的代码
    • user_id 是在注册页面期间自动递增的 id。所以让我们说:第一个注册用户将有:user_id:1 用户名:rager 密码:rager 第二个注册用户将有:user_id:2 用户名:osama Espil 密码:osama 等等......然后让我们说 user_id 1(我)标志通过登录活动,我想在共享首选项中保存 user_id: 1 这个用户 对不起,如果我之前不够清楚。只是为了确保您所描述的内容是否符合我在上面这篇文章中列出的内容。
    • 是的,它适用于您,我已经修改了适合您应用程序的答案。如果您在添加块时仍然遇到困难,请提供您的代码并帮助您解决问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 2023-04-03
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    相关资源
    最近更新 更多