retrieving integer values from SQLite database in android -


in app i`m trying give different welcome message each type of users here code in db helper class

public int logincheck(string a,string b) {    int ty;     sqlitedatabase db=this.getwritabledatabase();     cursor c = db.rawquery("select * users user_name = '"+a+"'and user_password = '"+b+"';", null);     ty= c.getint(c.getcolumnindex("user_type"));      if (c.getcount()<=0)     {         c.close();         db.close();         return 2;     }     else     c.close();            db.close();            return ty;   } 

it should check user type whether 0 or 1 ,, , according return value welcome message appears,what got retrieves number of column , retrieves 6 , "user_type" sixth column

you need move first row first:

public int logincheck(string a,string b) {     int ty;     sqlitedatabase db=this.getwritabledatabase();     cursor c = db.rawquery("select * users user_name = '"+a+"' , user_password = '"+b+"';", null);      if (c.movetofirst()) {         ty = c.getint(c.getcolumnindex("user_type"));     } else {         ty = 2;     }      c.close();     db.close();      return ty; } 

cursor.movetofirst return false if wasn't able move first row (e.g. cursor empty), no need c.getcount() check

=====

the android way of doing (not using rawquery unless have to):

public int logincheck(string a,string b) {     int ty;     sqlitedatabase db=this.getwritabledatabase();     cursor c = db.query("users", new string[] { "user_type" }, "user_name = ? , user_password = ?", new string[] {a, user_password}, null, null, null);      if (c.movetofirst()) {         ty = c.getint(0);     } else {         ty = 2;     }      c.close();     db.close();      return ty; } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -