I am trying do a sign up process. Where i have a method inside the DbAdaptor where i check if the username exists.
public Boolean checkUsername(String username) throws SQLException {
Cursor mCursor = db.query(TABLE_USERS, new String[] { ID,
KEY_NAME, KEY_USERNAME}, KEY_USERNAME + "="
+ username, null, null, null, null, null);
if (mCursor != null) {
return true;
}
return false;
}
From the Edit text i sent a value "harsha" as username to test it. but i am getting the following error
http://variable3.com/files/screenshots/2010-12-26_1215.png
the code inside the activity is this
DBAdapter db = new DBAdapter(RegisterActivity.this);
db.open();
if (db.checkUsername(username))
Toast.makeText(RegisterActivity.this, "Found",
Toast.LENGTH_LONG).show();
else
Toast.makeText(RegisterActivity.this, "Not Found",
Toast.LENGTH_LONG).show();
db.close();
you need to send the harsha as 'harsha' with single quote
public Boolean checkUsername(String username) throws SQLException {
Cursor mCursor = db.query(TABLE_USERS, new String[] { ID,
KEY_NAME, KEY_USERNAME}, KEY_USERNAME + "="
+"'"+username+"'", null, null, null, null, null);
if (mCursor != null) {
return true;
}
return false;
You're checking the cursor and not the moveToNext() result. The cursor is valid but does not return a result set.
Related
I use this function to get specific data from SQlite:
SearchRes getSresultByName(String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_WIKIRES, new String[] { KEY_ID,
KEY_SNAME, KEY_SJSON }, KEY_SNAME + "=?",
new String[] { name }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
SearchRes searchres = new SearchRes(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2));
return searchres;
}
And it works just fine, I need to create similar function to test if value is exist in the table, so I tried this:
boolean checkIfExist(String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_WIKIRES, new String[] { KEY_ID,
KEY_SNAME, KEY_SJSON }, KEY_SNAME + "=?",
new String[] { name }, null, null, null, null);
if (cursor == null)
return false;
else
return true;
}
But I always get TRUE. Can you help me figure out what is the problem?
you should not be checking if cursor is null you should be checking if anything exists in the cursor
if(cursor.moveToFirst()){
return true
}else{
return false;
}
You can try Cursor getCount() to check the number of rows in the result of the query.. Following is the sample code:
boolean checkIfExist(String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_WIKIRES, new String[] { KEY_ID,
KEY_SNAME, KEY_SJSON }, KEY_SNAME + "=?",
new String[] { name }, null, null, null, null);
if (cursor.getCount() > 0)
return true;
else
return false;
}
answer by #tyczj is also good..
I'm trying to check for a valid user. But I'm getting the following Exception:
no such column: ABC(Code 1):, while compiling: SELECT _id, username, password
FROM MyTable1 WHERE username=ABC
The code is as follows:
public int checkUser(String userName, String password) {
int check = -1;
int id = 2;
String psword = new String();
Cursor cursor = database.query(TABLE_NAME, allColumns, COLUMN_USER_NAME + "=" + userName, null, null, null, null);
cursor.moveToFirst();
Log.v(TAG, "After CURSOR!!");
psword = cursor.getString(cursor.getColumnIndex(COLUMN_PASSWORD));
Log.v(TAG, "pasword: " + psword);
if (psword.equals(password)) {
check = 1;
}
return check;
}
What am I doing wrong?? Please help
If username is varchar/text type then
Cursor cursor = database.query(TABLE_NAME, allColumns, COLUMN_USER_NAME
+ "=" + '"+userName+"', null, null, null, null);
This is sample login function
public boolean checkUser(String username, String password) throws SQLException
{
String whereClause = " USERNAME = '"+userName+"' ";
try {
open();// dbopen
Cursor cursor = db.query(TABLE_NAME, allColumns,whereClause, null, null, null, null);
}catch(Exception e){
}
}
or
public boolean checkUser(String username, String password) throws SQLException
{
Cursor mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});
if (mCursor != null) {
if(mCursor.getCount() > 0)
{
return true;
}
}
return false;
}
You should add quotes around userName. You want the rows that match the string value "ABC" or 'ABC' I don't remember which quotes. If you don't put quotes you are matching with the column named ABC.
And you should use prepares statements instead. That would avoid you many problems.
when i perfom search method its given me run time error
error:- 05-04 14:04:00.227: E/AndroidRuntime(4559): Caused by:
android.database.sqlite.SQLiteException: no such column: ww (code 1): ,
while compiling: SELECT DISTINCT faculty, deparment, name, officeNumber,
phoneNumber, emailAddress, officeHour FROM teacherInfo WHERE name=ww
I need to search on teacher name when user enter the name and display all information about this teacher such as name ,faculty,deparment,phone,email,officenumber,officehour
thiss getRecord method in DBAdapter.java
public Cursor getRecord(String n1) throws SQLException
{
Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
deparmentc, namec, officeNumberc,Phonec ,emailAddressc,officeHourc},namec + "=" + n1, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
and this search method in Information.java
public void search(){
db.open();
Cursor c = db.getRecord(n);
if (c.moveToFirst()){
t1.setText(c.getString(0));
t2.setText(c.getString(1));
t3.setText(c.getString(2));
t4.setText(c.getString(3));
t5.setText(c.getString(4));
t6.setText(c.getString(5));
t7.setText(c.getString(6));
}
else
Toast.makeText(this, "this Dr not found", Toast.LENGTH_LONG).show();
db.close();
}
this Oncreate method in DBAdapter.java
public void onCreate(SQLiteDatabase db)
DATABASE_CREATE =
"create table if not exists teacherInfo (teacherNumber INTEGER primary key autoincrement,"
+ "name TEXT not null, faculty TEXT,deparment TEXT,officeNumber INTEGER,officeHour TEXT, emailAddress TEXT,phoneNumber TEXT, location INTEGER );";
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
deparmentc, namec, officeNumberc,Phonec ,emailAddressc,officeHourc},namec + "= ?", new String[] { n1 }, null, null, null, null);
Strings have to be in quotes.
The problem is that you missed the quotes of the string!
Better use the String arguments like that:
public Cursor getRecord(String n1) throws SQLException
{
Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
deparmentc,namec,officeNumberc,Phonec,emailAddressc,officeHourc},
namec + "= ? ",
new String[] {ww}, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
By that way you avoid any errors and also string special characters are auto escaped!
Your query isn't sanitized. It should be:
SELECT DISTINCT faculty, deparment, name, officeNumber,
phoneNumber, emailAddress, officeHour FROM teacherInfo WHERE name="ww"
You are entering wrong query. Change it to ->
Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
deparmentc, namec, officeNumberc,Phonec ,emailAddressc,officeHourc},"name = " + n1, null, null, null, null, null);
Any one help me. I want to print some suggestion if Cursor value return as null.
This is method of helper class for display data for corresponding ID
public Cursor display(int id) {
// TODO Auto-generated method stub
DBHelper=new DatabaseHelper(context);
db=DBHelper.getReadableDatabase();
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_ISBN}, KEY_ROWID + "=" + id, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
and this is my activity class from where iam calling display()
public void onClick(View v) {
// TODO Auto-generated method stub
int id=Integer.parseInt(inputid.getText().toString());
DBAdapter dbadapter = new DBAdapter(v.getContext());
Cursor c=dbadapter.display(id);
if(c!=null)
Toast.makeText(this, "Name: " + c.getString(1) , Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Enter Valid ID", Toast.LENGTH_LONG).show();
}
Simply use this rawQuery :
public Cursor getSubDirectory(String parentId, String orderby, String order){
String sql = "SELECT * FROM "+DOCUMENTS+ " where "+DatabaseConstant.key_DOCUMENT_PARENT_ID +" = "+parentId+" ORDER BY "+orderby +" "+ order ;
return myDataBase.rawQuery(sql, null);
}
I am trying to do a check if the User-name entered by the user exists in the DB.
public Cursor checkUsername(String username) throws SQLException {
Cursor mCursor = db.query(true, TABLE_USERS, new String[] { ID,
KEY_NAME, KEY_USERNAME}, KEY_USERNAME + "="
+ username, null, null, null, null, null);
if (mCursor != null) {
return true;
}
return false;
}
When i return true or false i am getting an Error saying
Type mismatch: cannot convert from boolean to Cursor
i just want to return true or false from the DBAdaptor back to the Activity.
Your function returns a Cursor
public Cursor checkUsername()
Either change it to return a boolean, or return a cursor.
Try reformatting your sql query and provide the 'where' clause as one of the parameters in the database call :
public Cursor getRoute(long rowIndex)
{
String where = KEY_ID + "=" + rowIndex;
return db.query(TBL_ROUTES, null, where, null, null, null, null);
}
Also, remember to close your cursor when you are finished with it, otherwise you will get other exceptions.