• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps SELECT answer FROM forum WHERE level = 'newbie'

beedge

Lurker
Jul 20, 2010
2
0
Hey all.

My background is as a php developer. I Learnt Java in college but that was all of 10 years ago and I have gotten a tad rusty since then,

Im trying to get my head around android and I have been doing ok until now... but.....

I am trying to execute an sql query to the sqlite database,

in PHP no problem, $sql = "select from table where `username` = '$username' and `password` = '$password'

however I am stumped trying to achieve this android.

what I have is:

Cursor cur = db.query("escar_users", null, orgWhere, null, null, null, null);

but its the where clause that has gotten me stuck

my string orgWhere = "username = "+sUserName+" AND password = "+sPassWord

where sUsername and sPassWord are input variables captured from a login screen., but it crashes every time.

please. am I doing something obviously wrong here?

Thanks in advance, beedge.

 
Although I have no idea why the crash is occurring since I cannot see the stack trace. When searching a varchar type field you should have single quotes (') around the value.
This should provide you with a valid WHERE clause.

string orgWhere = "WHERE username = '" + sUserName + "' AND password = '" + sPassWord + "'";

(notice the single quotes before the double quotes where necessary).
 
Upvote 0
Check the DDMS output to see why it is crashing (click DDMS in the upper right hand corner of Eclipse, or if it doesn't show, click the little box with the + sign and pick DDMS).

In general you should use the where arguments parameter instead of concatenating your sUsername and sPassword into your where string. If you have special characters (which the password probably does) it will do bad things when searching the database.

I don't know if you can have the columns option be null either, that should contain the columns you want to return

instead try:

String columns[] = {whatever_your_column_name(s)_are};
String args[] = {sUserName, sPassWord};
String orgWhere = "username=? AND password=?"
Cursor cur = db.query("escar_users", columns, orgWhere, args, null, null, null);
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones