Android SQLite fetch user_id -
how can fetch user_id sqlite database?
i have user table created (just 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 user's information throughout whole application (using shared preferences).
at log in page, have 2 textfields: username , password. can save these values variable, , put them in createsession(string name, string email, long userid) method. however, since dont have user_id textfield, can't fetch user_id .. needed 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 query receive id, , return log in page (where put in createsession() method)
public long getuserid(string username, string password) throws sqlexception { cursor mcursor = database.rawquery("select user_id user username=? , password=?", new string[]{username,password}); if (mcursor != null) { if(mcursor.getcount() > 0) { long userid = mcursor.getlong(1); } } return null; }
but won't fetch userid me .. have suggestion why be?
you forgot move cursor first record. pointing record -1.
so change this
if (mcursor != null) {
to
if (mcursor != null && mcursor.movetofirst()) {
and... column 1 not exist.
change
long userid = mcursor.getlong(1);
to
long userid = mcursor.getlong(0);
Comments
Post a Comment