I am using this code to filter my listview:
Cursor fetchCountriesByName(String inputText) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
if (inputText == null || inputText.length () == 0) {
String[] columns = new String[] {DBHelper.ID,DBHelper.FNAME,DBHelper.LNAME,DBHelper.ADDRESS };
cursor = db.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);
}
else {
cursor = db.query(true, TABLE_NAME, new String[] {DBHelper.ID,
DBHelper.FNAME,DBHelper.LNAME},DBHelper.ADDRESS+ " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
I got an error
java.lang.IllegalArgumentException: column 'patient_address' does not exist
But the column names are same. There is a column called patient_address
What's the reason of this error? Please guide.
Try this
cursor = db.query(true, TABLE_NAME, new String[] {DBHelper.ID,
DBHelper.FNAME,DBHelper.LNAME},DBHelper.ADDRESS+ " like '%?%'", new String[]{inputText },
null, null, null, null);
the parameter is error.
Related
Adapter.java
public String getID(String i) throws SQLException
{
db=DBHelper.getReadableDatabase();
String ij="No Track Found";
Cursor mCursor =
db.query(true, TABLE, new String[] {KEY_ID}, KEY_NAME + "=" + "'"+i+"'", null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
if (mCursor.moveToFirst())
{
ij=mCursor.getString(0);
}
return ij ;
}
The Question is when I pass any song's name that contain "'"(e.g. alexander's blade) , it throws and error. Otherwise all fine.
You should use the selectionArgs parameter:
db.query(true, //distinct
TABLE, //table
new String[] {KEY_ID}, //columns
KEY_NAME + "=?", //selection
new String[] {i}, //selectionArgs
null, //groupBy
null, //having
null, //orderBy
null //limit
);
Using selectionArgs also help to prevent SQL Injection
You need to call your method like this:
Cursor c = bd.query(Constantes.TABELA_APLICATIVO, COLS, "urlandroid = ?", new String[]{url}, null, null, null);
The caracter "?" represents the values on Third parameter(String[]).
i added a filter to my listview ( populated from sqlite ). i am using this method to fetch the records from sqlite
public Cursor fetchServicesByName(String inputText, String c) throws SQLException {
db = this.getReadableDatabase();
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = db.query(TABLE_SERVICE, new String[] {KEY_ID,KEY_NAME,
KEY_ADRESSE, KEY_EMAIL, KEY_NUMTEL, KEY_WEBSITE,KEY_IMAGE}, KEY_TYPE + "=?",
new String[] { c },null, null, null, null);
}
else {
mCursor = db.query(true, TABLE_SERVICE, new String[] {KEY_ID,KEY_NAME,
KEY_ADRESSE, KEY_EMAIL, KEY_NUMTEL, KEY_WEBSITE,KEY_IMAGE},
KEY_NAME + " like '%" + inputText + "%'" , null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
this is working, but what i want to do is to filter by inputText and KEY_TYPE
i used this (instead of the last db.query ) but it didn't return anything
mCursor = db.query(TABLE_SERVICE, new String[] {KEY_ID,KEY_NAME,
KEY_ADRESSE, KEY_EMAIL, KEY_NUMTEL, KEY_WEBSITE,KEY_IMAGE},
KEY_NAME + " like ? and" + KEY_TYPE + " like ? " , new String[] { inputText, c},
null, null, null, null);
You need to check if your inputtext and keytype are null!
Aris your routine working when you fill out your inputtext and c vallue?
In my code there is a function that fetches rows from database with specific column value. Here it is KEY_DESC. I also need to add the additional condition of specific value for KEY_DATE. How can I do that?
fetchEventByName()
public Cursor fetchEventByName(String inputText,String inputText1) throws SQLException {
//Log.w(TAG, inputText);
SQLiteDatabase db = this.getReadableDatabase();
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_DESC, KEY_EVENT, KEY_DATE },
null, null, null, null, null);
}
else {
mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_DESC, KEY_EVENT, KEY_DATE},
KEY_DESC + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Did you try something like this?
mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_DESC, KEY_EVENT, KEY_DATE},
KEY_DESC + " like '%" + inputText + "%' or "+KEY_DATE+"="+WHAT_EVER, null,
null, null, null, null);
I want to retrieve notes from database where notes are (not null), I wrote the following code:
public Cursor getmynotesss(int id){
Cursor mCursor = database.query(true, NOTES_TABLE, new String[] {
NOTE_TEXT, PAGE_NO,BOOK_ID},BOOK_ID + "=? AND "+NOTE_TEXT+" IS NOT NULL OR "+NOTE_TEXT+" != ''" ,
new String[] { String.valueOf(id) }, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
but it still gives me the null notes.
what is the wrong in my code?
It looks like the OR condition should be AND :
"=? AND "+NOTE_TEXT+" IS NOT NULL AND "+NOTE_TEXT+" != ''"
i have solve my problem like this
SELECT pk_video_master_id AS _id,
video_title,
video_added_date,
video_thumbnail FROM tbl_video_master
WHERE NOT video_detail_last_view ='' // this is return not null values and work for me
ORDER BY video_detail_last_view ASC";
so you can try like this
Cursor mCursor = database.query(true, NOTES_TABLE,new String[] {NOTE_TEXT,AGE_NO,BOOK_ID},BOOK_ID + "=? AND NOT "+NOTE_TEXT+" = ''" ,new String[] {String.valueOf(id) }, null, null, null, null);
try
Cursor mCursor = database.query(true, NOTES_TABLE, new String[] {
NOTE_TEXT, PAGE_NO,BOOK_ID}, NOTE_TEXT+" IS NOT NULL AND "+BOOK_ID + "=?" ,
new String[] { String.valueOf(id) }, null, null, null, null);
and not equals in SQLITE is "<>" not "!="
try replacing
IS NOT NULL
with
<>``
I receive this exception "bind or column index out of range" in this line:
Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);
I want to check if the given id exists in the database, but always the query throw this exception and I don't know what is wrong. I tried with this and the exception it's the same:
Cursor cursor = db.query(TABLE_NAME, null, ID+" = ?", new String[] { id }, null, null, null);
This is the method I've programmed. If there is something wrong here, please, tell me:
public boolean existsAcc(String id)
{
//Check the parameters
if (id == null)
{
throw new IllegalArgumentException(
"The id parameter cannot be null");
}
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);
if (!cursor.moveToFirst())
{
cursor.close();
return false;
}
cursor.close();
return true;
}
I don't know why Chirag deleted his answer, but he was rigth. Changing
Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);
to
Cursor cursor = db.query(TABLE_NAME, null, ID + " = '" + id + "'", null, null, null, null);
does the trick.