Database query in android - android

In my application i have to retrieve data from database to display in a listview.My requirement is i have to retrieve data from database with "groups" as household and "category" as income.
My query is as follows:
public Cursor gethouseholdTitle(String grps) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[]
{
KEY_ROWID,
KEY_INCOME,
KEY_DESC,
KEY_QUANTITY,
KEY_TOTAL,
KEY_CATEGORY,
KEY_RECURR,
KEY_DATE,
KEY_GROUP
//KEY_PUBLISHER
},
KEY_GROUP + "=" + grps,
KEY_CATEGORY + "=" + "'Income'",
null,
null,
null,
null,
null
);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
But it shows error:
The method query(boolean, String, String[], String, String[], String, String, String, String) in the type SQLiteDatabase is not applicable for the arguments (boolean, String, String[],
String, String, null, null, null, null, null).
As i new to this,please help me.

Looking at the logic, seems like you need to change
KEY_GROUP + "=" + grps, KEY_CATEGORY + "=" + "'Income'",
to
KEY_GROUP + "=" + grps + " and " + KEY_CATEGORY + "=" + "'Income'",
Hope this helps...

Related

building a logical condition query with else and if android sqlite

I'm using filtering and the only filter that is implemented is a search by key_name, I'd like to also add other keys.
public static Cursor fetchCountriesByName(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor c = null;
if (inputText == null || inputText.length () == 0) {
c = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_COUNTRY, KEY_REGION, KEY_PHONE},
null, null, null, null, null);
}
else {
c = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_COUNTRY, KEY_REGION, KEY_PHONE},
KEY_NAME + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (c != null) {
c.moveToFirst();
}
return c;
}
when i tried to use else { if ( it requested a (c != null), when i tried to modify, KEY_NAME + " like '%" + inputText + "%'", null, KEY_COUNTRY + " like '%" + inputText + "%'",KEY_REGION + " like '%" + inputText + "%'"); was saying illegalargumentexception.
How to do it right?
Thanks.
you forgot to add condition like ANDor OR in your query
check below example
db.query(true, DATABASE_TABLE,new String[] {KEY_ROWID,
KEY_NAME, KEY_COUNTRY, KEY_REGION, KEY_PHONE},
KEY_NAME + " LIKE ? AND ", KEY_COUNTRY + " LIKE ? AND ", KEY_REGION + " LIKE ?",
new String[] {"%"+inputText+"%" ,"%"+inputText+"%" ,"%"+inputText+"%"},
null, null, null, null);
here i have used AND you can replace it as your need.

Error in database class in data fetching query

I am developing a app in android and here i want to fetch data from table according to rowId and subjectId but I am getting this error,
The method query(boolean, String, String[], String, String[], String, String, String, String) in the type SQLiteDatabase is not applicable for the arguments
(boolean, String, String[], String, String, null, null, null, null)
And the here's that query,
public Cursor getText(long rowId, long subId) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_id, KEY_ques,KEY_correctans,KEY_wrongans1,KEY_wrongans2,KEY_wrongans3,KEY_subject,KEY_subjectid }, KEY_id + "="
+ rowId, KEY_subjectid + "=" + subId,null,null, null,null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
I want to fetch table data as where subId = KEY_subjectid and rowId = KEY_id
If any thing is not clear in this question please ask me. And if its clear please help me..
Thanks in Advance.
Change the query to following:
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_id, KEY_ques,KEY_correctans,KEY_wrongans1,KEY_wrongans2,KEY_wrongans3,KEY_subject,KEY_subjectid }, KEY_id + "="
+ rowId + " AND " + KEY_subjectid + "=" + subId,null, null,null, null,null);
The 5-th parameter needs to be a String array, not a string the way you have. The 4-th parameter is the WHERE SQLite clause.
Do this way
public Cursor getText(long rowId, long subId) throws SQLException
{
Cursor mCursor = db.rawQuery("select * from "+DATABASE_TABLE+" where KEY_subjectid="+subId+" and KEY_id="+rowId+"",null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}

How to return multiple entries with same value from SQLite

This is what my database looks like, how would I return all of the rows that have the same string in the KEY_ALCOHOL column? After I get all of the rows, I need to randomly pick one and then display it.
I tried this:
DATABASE HELPER .JAVA
public Cursor getAlcohol(String alcohol) throws SQLException
{
Cursor mCursor =
myDataBase.query(true, DB_TABLE, new String[] {
KEY_ROWID,
KEY_ALCOHOL,
KEY_TYPE,
KEY_BRAND,
KEY_PRICE
},
KEY_ALCOHOL + "=" + alcohol,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
MAIN ACTIVITY .JAVA
myDbHelper.openDataBase();
Cursor c = myDbHelper.getAlcohol("Liquor");
if (c.moveToFirst())
{
do {
DisplayTitle(c);
} while (c.moveToNext());
}
myDbHelper.close();
}
public void DisplayTitle(Cursor c)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"ALCOHOL: " + c.getString(1) + "\n" +
"TYPE: " + c.getString(2) + "\n" +
"BRAND: " + c.getString(3) + "\n" +
"PRICE: " + c.getString(4),
Toast.LENGTH_LONG).show();
}
I was testing to see if this would simply return all of the rows with "Liquor" in the KEY_ALCOHOL column but instead it gave me a null pointer.
EDIT
Got it working!!
Here is what i came up with and it works!! Let me know if anything is wrong or you see a better way of doing it! Thanks everyone!
myDbHelper.openDataBase();
Cursor Test = myDbHelper.getAlcohol("Liquor");
int test7 = Test.getCount();
test9 = r.nextInt(test7);
Cursor c = myDbHelper.getTitle(test9);
if (c.moveToFirst())
DisplayTitle(c);
else
Toast.makeText(this, "No title found",
Toast.LENGTH_LONG).show();
myDbHelper.close();
I hope this works
public Cursor getAlcohol(String alcohol) throws SQLException
{
Cursor mCursor =
myDataBase.query(true, DB_TABLE, new String[] {
KEY_ROWID,
KEY_ALCOHOL,
KEY_TYPE,
KEY_BRAND,
KEY_PRICE
},
KEY_ALCOHOL + "=?",
new String[] { alcohol },
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Look at structure
Cursor SQLiteDatabase.query(String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy,
String limit)
Your 'selection' was wrong. It resulted in WHERE KEY_ALCOHOL + "=" + alcohol = null since argument is separate.

Setting multiple Selections args in SQLite database query

How would i go about querying more than one selection arg? For example here is how my database is formatted
Here is the code i used to search with just one seletion arg:
public Cursor getType(String type) throws SQLException
{
Cursor mCursor =
db.query(true, DB_TABLE, new String[] {
KEY_ROWID,
KEY_ALCOHOL,
KEY_TYPE,
KEY_BRAND,
KEY_PRICE
},
KEY_TYPE + "=?",
new String[] { type },
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
But this only searches by KEY_TYPE, how would I set it so it searches by KEY_TYPE, KEY_ALCOHOL, and KEY_PRICE?
This may help you
public Cursor getType(String type) throws SQLException
{
Cursor mCursor =
db.query(true, DB_TABLE, new String[] {
KEY_ROWID,
KEY_ALCOHOL,
KEY_TYPE,
KEY_BRAND,
KEY_PRICE
},
KEY_TYPE + "=?" + " AND " + KEY_ALCOHOL + "=?" " AND " + KEY_PRICE + "=?",
new String[] { type,alcohol,price },
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Figured it out!! Thanks to #Rajendra for giving me some code to build off of! You can only add Strings into the selections args so i did this:
public Cursor getTest(String alcohol, String type, long price) throws SQLException
{
Cursor mCursor =
myDataBase.query(true, DB_TABLE, new String[] {
KEY_ROWID,
KEY_ALCOHOL,
KEY_TYPE,
KEY_BRAND,
KEY_PRICE
},
KEY_ALCOHOL + "=?" + " AND " + KEY_TYPE + "=?" + " AND " + KEY_PRICE + "<=" + price,
new String[] { alcohol, type},
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
And it works!!

Android SQLiteDatabase query with LIKE

I have 3 names, Allakhazam, Beatbox and Cunning in my NAMES Table.
public Cursor fetchNamesByConstraint(String filter) {
mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,
KEY_NAME }, KEY_NAME + " LIKE ?",
new String[] { filter }, null, null, null,
null);
return mCursor;
}
I call the function with "A" as the filter, but my cursor is returning a 0 count when it should at least return me a 1. Anyone can see what's wrong with the code?
this statement will return all the records whose keyname equals string specified by string, if you use wild card, then you can get desired results. Like:
mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,
KEY_NAME }, KEY_NAME + " LIKE ?",
new String[] { filter+"%" }, null, null, null,
null);
Will Lists all the records starting with word in filter.
mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,
KEY_NAME }, KEY_NAME + " LIKE ?",
new String[] {"%"+ filter+ "%" }, null, null, null,
null);
Will Lists all the records containing word in filter.
public java.util.Vector<Products> getsearch(String subcategory,String searchby)
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor = db.query(
TABLE_PRODUCTS,
new String[] { SUBCATEGORY, MAIN_CATEGORY, PRODUCT_ID, PRODUCT_NAME, BRAND, PACKAGE_SIZE, PRICE },
SUBCATEGORY + " LIKE '%" + subcategory + "%'",
null, null, null, null, null);
}
Can you try this.....code.......
public static final String KEY_ROWID="row";
public static final String KEY_NAME="name";
public Cursor fetchNamesByConstraint(String filter) {
Cursor cursor=mDb.query(true, DATABASE_NAMES_TABLE, null,"row LIKE '%"+filter+"%' or name LIKE '%"+filter+"%'",null, null, null, null);
}
You should give the filter with wildcards ;)
"A%"
You can use Raw Queries.
public Cursor fetchNamesByConstraint(String filter) {
String query = "SELECT * FROM " + TABLE_NAME + " WHERE " +
COLUMN_NAME + " LIKE '%" + filter + "%'" ;
SQLiteDatabase db = this.getReadableDatabase();
cursor = db.rawQuery(query, null);
return mCursor;
}
public Cursor fetchNamesByConstraint(String filter) {
mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,
KEY_NAME }, KEY_NAME + " LIKE ?",
new String[] {"%"+ filter +"%"}, null, null, null,
null);
return mCursor;
}
The rest is up to our desing, cursor is commanly same
public ArrayList<AboneDataList> getAllPasssiveAboneByDate(String mDate) {
ArrayList<AboneDataList> foodList = new ArrayList<>();
AboneDataList food;
SQLiteDatabase database = dbHelper.getReadableDatabase();
final String kolonlar[] = {DBHelper.COLUMN_A_ID,
DBHelper.COLUMN_A_ABONE_NO,
DBHelper.COLUMN_A_NAME,
DBHelper.COLUMN_A_IS_STUFF,
DBHelper.COLUMN_A_COUNTRY,
DBHelper.COLUMN_A_CITY,
DBHelper.COLUMN_A_TOWN,
DBHelper.COLUMN_A_ADDRESS,
DBHelper.COLUMN_A_PHONE,
DBHelper.COLUMN_A_MOBILE,
DBHelper.COLUMN_A_DATE_START,
DBHelper.COLUMN_A_DATE_END,
DBHelper.COLUMN_A_COUNT,
DBHelper.COLUMN_A_IS_ACTIVE,
DBHelper.COLUMN_A_CARGO,
DBHelper.COLUMN_A_YURT_ID,
DBHelper.COLUMN_A_JOURNAL,
DBHelper.COLUMN_A_MAIL,
DBHelper.COLUMN_A_BUSINESS,
DBHelper.COLUMN_A_BIRTH,
DBHelper.COLUMN_A_GENDER,
DBHelper.COLUMN_A_ABONE_TYPE,
DBHelper.COLUMN_A_MEDENI_TYPE};
//String whereClause = DBHelper.COLUMN_A_JOURNAL + " = ? AND " + DBHelper.COLUMN_A_IS_ACTIVE + " = ? ";
//final String whereArgs[] = {String.valueOf(mJournal),isActive};
//Cursor cursor = database.query(DBHelper.TABLE_ABONE, kolonlar, whereClause, whereArgs,
// null, null, DBHelper.COLUMN_A_ID + " ASC");
Cursor cursor = database.rawQuery("select * from " + DBHelper.TABLE_ABONE + " WHERE "
+ DBHelper.COLUMN_A_DATE_END + " LIKE '%" + mDate + "%'", null);
//Cursor cursor = database.query(DBHelper.TABLE_ABONE, kolonlar, null, null, null, null, DBHelper.COLUMN_A_NAME + " ASC");
while (cursor.moveToNext()) {
food = new AboneDataList();
food.setId(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_A_ID)));
food.setAbone_no(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_A_ABONE_NO)));
food.setName(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_NAME)));
food.setIs_stuff(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_IS_STUFF)));
food.setCountry(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_COUNTRY)));
food.setCity(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_CITY)));
food.setTown(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_TOWN)));
food.setAddress(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_ADDRESS)));
food.setPhone(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_PHONE)));
food.setMobile(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_MOBILE)));
food.setDate_start(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_DATE_START)));
food.setDate_end(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_DATE_END)));
food.setCount(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_COUNT)));
food.setIs_active(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_IS_ACTIVE)));
food.setCargo(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_CARGO)));
food.setYurt_id(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_A_YURT_ID)));
food.setJournal(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_JOURNAL)));
food.setMail(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_MAIL)));
food.setBusiness(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_BUSINESS)));
food.setBirth(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_BIRTH)));
food.setGender(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_GENDER)));
food.setAbone_type(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_ABONE_TYPE)));
food.setMedeni_hal(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_MEDENI_TYPE)));
foodList.add(food);
}
database.close();
cursor.close();
return foodList;
}

Categories

Resources