I'm having trouble sorting my database alphabetically. I assume I need to sort the database here:
public static Cursor getAllRecords() {
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ITEM},
null, null, null, null, null, null);
}
I've tried replacing the last null with " DESC" which didn't work.
Am I doing this in the wrong spot?
Use something like KEY_ITEM + " DESC" for the orderBy parameter:
public static Cursor getAllRecords() {
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ITEM},
null, null, null, null, KEY_ITEM + " DESC"); // note the missing last null for 'limit'
}
Try this -
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ITEM}, null, null, null, null, KEY_ITEM+ " ASC");
or
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ITEM}, null, null, null, null, KEY_ITEM+ " DESC");
Related
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.
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[]).
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);
Based on this tutorial, there is a function called "fetchCountriesByName" to filer the listview content by comparing "KEY_NAME" with inputted country name. Only matching "KEY_NAME" record will be displayed.
http://www.mysamplecode.com/2012/07/android-listview-cursoradapter-sqlite.html
public Cursor fetchCountriesByName(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION}, null, null, null, null, null);
}
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
KEY_NAME + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
My question is how to fetch from multiple field which similar to inputted text? I want the inputted text not just compare with KEY_NAME, but also compare with KEY_REGION. I had added the "KEY_REGION" in the bold area, but it doesn't work. How can I fetch KEY_REGION & KEY_NAME?
public Cursor fetchCountriesByName(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
null, null, null, null, null);
}
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
KEY_NAME+ KEY_REGION + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Use the boolean logical operators AND or OR.
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
KEY_NAME + " like '%" + inputText + "%' AND "+ KEY_REGION + " like '%" + inputText + "%'", null,
null, null, null, null);
Try this:
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
KEY_NAME+" like '%?%' OR "+KEY_REGION+" like '%?%'", new String[]{inputText, inputText},
null, null, null, null);
The bound variables (?) will protect you from SQL injection attacks. You will also want to consider changing KEY_ROWID to _ID, as this is what ListView expects.
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
<>``