dsa122870 2013-11-10 03:27
浏览 69
已采纳

Android:如何从SQLite数据库中调用和保存userID作为会话?

In my application, I am trying to figure out how to save userID session and use it until the user closes the app.

I know in PHP you can just use the $_SESSION['userID'] = $user_id

I want to run a query something like this in Android SQLite

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

This is from php, is there some way I can do this in android? I heard about shared preferences but I am not sure how it works?

Sorry, I just started learning about Android programming. I am a bit of a noob. I'll appreciate if you could help me out here.

EDIT: this userID is a column that I will be calling from SQLite database and saving it as a session when the user logs in.

  • 写回答

2条回答 默认 最新

  • dongpo5264 2013-11-10 03:59
    关注

    First you have to declare global shared preferences:

    SharedPreferences sp;
    

    then in onCreate you delcare sp:

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

    So basically when the user login you check whether he is logged in, if it is true, then save his ID in the shared preferences using:

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

    and in the login class in onCreate, you add the second block to check whether the user is logged in and he has never logged out. and simply if he is then take him to the next activity.

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

    whenever you want to retrieve the value you just add...

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

    and to log out the user and delete the session use the following:

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

    Edit 2:

    What you got to do is...

    1) retrieve the ID from the shared preferences.. now if the UserID column is int then use.

    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) you have to check, if the ID != 0 then pass the value to the query...

    SELECT user_id, username, password
    FROM users
    WHERE user_id = ID
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)