samedi 28 mars 2015

Android SQLite fetch user_id

How can you fetch the user_id from the sqlite database?


I have a user table created like this (just a simple one):



private static final String CREATE_TABLE_USER =
"CREATE TABLE " + TABLE_USER +
"("+
COLUMN_USER_ID + " integer primary key autoincrement," +
COLUMN_USER + " TEXT NOT NULL, " +
COLUMN_PASSWORD + " TEXT NOT NULL" +
");";


I want to get the user's information throughout the whole application (using shared preferences).


At my log in page, I only have 2 textFields: username and password. I can save these values as a variable, and put them in my createSession(String name, String email, Long userId) method. However, since I dont have an user_id textfield, I can't fetch the user_id .. that is needed for the createSession() method.



public void createSession(String name, String email, Long userId){

// Storing name in pref
editor.putString(KEY_NAME, name);

// Storing password in pref
editor.putString(KEY_PASSWORD, email);

//Storing user id in pref
editor.putLong(KEY_ID, userId);

// commit changes
editor.commit();
}


I tried making a query to receive the ID, and return it to the log in page (where it will be put in the createSession() method)



public Long getUserId(String username, String password) throws SQLException
{
Cursor mCursor = database.rawQuery("SELECT user_id FROM user WHERE username=? AND password=?", new String[]{username,password});
if (mCursor != null) {
if(mCursor.getCount() > 0)
{
Long userId = mCursor.getLong(1);
}
}
return null;
}


But it won't fetch the userId for me .. anyone have a suggestion why this would be?


Aucun commentaire:

Enregistrer un commentaire