Android Search Query and Sort in order - android

Anyone has an idea how this is done? I have a regular search query below that displays matching data in a listview, i tried putting COLUMN NAME + " ASC" at the order part, but i got errors.
public Cursor getSearch(String query)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, KEY_NAME + " LIKE ?",
new String[] {"%"+ query+ "%" }, null, null, null,
null);
if(c != null)
{
c.moveToFirst();
}
return c;
}

The 'Order by' clause should be the 8th argument in the query call:
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, KEY_NAME + " LIKE ?",
new String[] {"%"+ query+ "%" }, null, null, COLUMN_NAME + " ASC", null);
See: Documentation

Related

SQLite in Android: when I pass any name that contains a quote, it throws and 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[]).

filter a listview populated from SQLite

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?

retrieving rows with multiple column values sqlite

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);

more than one selection arguments in SQLite for android

how can I add more than one selection argument?
for example this is one argument selection
inputNumber + "= ?"
new String[]{String.valueOf(numberToCheck)}
full cursor code
Cursor cursor = sqLiteDatabase.query(INSPECTION_PLAN_TRANSACTION,
projection, inputNumber + "= ?", new String[]{String.valueOf(numberToCheck)},
null, null, null, null);
how can i add another condition to the sql statement? like for example
input_number = numberToCheck AND name = "XXX"
input_number is an integer column and name is a column of Strings
sqLiteDatabase.query(INSPECTION_PLAN_TRANSACTION,
projection, input_number + "= ? AND "+ name"= ?", new String[]{String.valueOf(numberToCheck), "XXX"},
null, null, null, null);
Try this:
Cursor cursor = sqLiteDatabase.query(INSPECTION_PLAN_TRANSACTION,
projection, inputNumber + "= ? AND "+name+ " = ?", new String[]{String.valueOf(numberToCheck,nametocheck)},
databse.query(String dbName, String[] columnNames, String whereClause, String[] selectionArgs, String[] groupBy, String[] having, String[] orderBy)
Cursor cursor = database.query(YOUR_TABLE_NAME ,
new String[] {NAME_OF_COLUMNS_WHICH_IS_TO_BE_SELECT} ,
input number + "='" + numberToCheck + "' AND " +
name + "='XXX'",
null, null, null, null);

SQLiteDatabase - How to use where clause?

public Cursor fetchTimetable() {
return mDb.query(DATABASE_TABLE_TIMETABLE, new String[] {TIMETABLE_ROWID, TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_ROOM, TIMETABLE_LECTURER, TIMETABLE_TIME}, null, null, null, null, null);
}
For example if i wanted TIMETABLE_MODULECODE = 123. How would i do this, i have read its the first null. I have tried this below but still doesnt work
public Cursor fetchTimetable() {
String a = "TIMETABLE_CODE = ADD";
return mDb.query(DATABASE_TABLE_TIMETABLE, new String[] {TIMETABLE_ROWID, TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_ROOM, TIMETABLE_LECTURER, TIMETABLE_TIME}, a, null, null, null, null);
}
My working example with where args (it's more clear with using it):
String [] settingsProjection = {
DBContract.Settings._ID,
DBContract.Settings.COLUMN_NAME_USER_ID,
DBContract.Settings.COLUMN_NAME_AUTO_LOGIN
};
String whereClause = DBContract.Settings.COLUMN_NAME_USER_ID+"=?";
String [] whereArgs = {userId.toString()};
Cursor c = db.query(
DBContract.Settings.TABLE_NAME,
settingsProjection,
whereClause,
whereArgs,
null,
null,
null
);
db.query
(
TABLE_NAME,
new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO },
TABLE_ROW_ID + "=" + rowID,
null, null, null, null, null
);
TABLE_ROW_ID + "=" + rowID, here "=" is the where clause
What exactly does "still doesn't work" mean?
The following code should work just fine:
public Cursor fetchTimetable() {
String[] columnNames = new String[] {TIMETABLE_ROWID, TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_ROOM, TIMETABLE_LECTURER, TIMETABLE_TIME};
String whereClause = "TIMETABLE_MODULECODE=123";
return mDb.query(DATABASE_TABLE_TIMETABLE, columnNames, whereClause, null, null, null, null);
}
the right syntax is
String[] projection= {column names};
String[] where={"values for where clause"}; //this must be array
public Cursor fetchTimetable() {
return mDb.query(TABLE_NAME, projrction, "columname"+"=?", where, null, null, null);
}
The simple way:
return mDb.rawQuery("SELECT * FROM myTable WHERE column1 = "+ someValue, null);
You can use Use this query:
SQLiteDatabase db = this.getReadableDatabase();
//Cursor cursorr = db.rawQuery(Query, null);
Cursor cursorr = db.rawQuery("select * from " + DATABASE_TABLE_EHS + " where " + TASK_ID + "='" + taskid + "'" , null);
if (cursorr.moveToFirst())
{
do {
// your code like get columns
}
while (cursorr.moveToNext());
}
}
If you're checking against a string value you'll want to wrap the string in quotes like below:
dbHelper.COL_EMAIL +"='"+givenEmail+"'",

Categories

Resources