need help with androids sql query - java

I have an error in that m
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
Cursor myCursor=db.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,MONEY_FIGURE,SPENDING_DETAILS,DATE}
, KEY_ROWID , null, null, DATE+"="+currentDateTimeString, KEY_ROWID, null);
The query function by the androids api:
query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
What I am trying to do here is to say. give me all the data based on where the date equals
currentDateTimeString .
But I get an error on that statement..
dont ask me what the error is..when i remove the call to the method holding that query statement the error doesnt appear..so i guess it is an sqlexception
SOLVED MY ME:
Cursor myCursor=db.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,MONEY_FIGURE,SPENDING_DETAILS,DATE}
,DATE+"='"+currentDateTimeString+"'" , null, null, null , KEY_ROWID, null);
I am trying to do this
SELECT MONEY_FIGURE,SPENDING_DETAILS,DATE
FROM SpendingManagmentTable
WHERE Date=# currentDateTimeString

If the column type is not number then have to use with ''
date='1/1/2011' not date=1/1/2011
Cursor c = db.query(DATABASE_TABLE, new String[] {{KEY_ROWID,MONEY_FIGURE,SPENDING_DETAILS,DATE},DATE+"='"+currentDateTimeString+"'", null, null, null, null);

Related

Using CursorLoader to retrieve only UNIQUE values from SQLiteDatabase [duplicate]

I want to return a cursor only with distinct values of a column.
The column 'Groups' has more items but with only 2 values: 1,2,1,1,1,2,2,2,1
String[] FROM = {Groups,_ID};
public Cursor getGroups(){
//......
return db.query(TABLE_NAME,FROM,null,null,null,null,null);
}
will return a cursor containing {1,2,1,1,1,2,2,2,1} but I would like to contain just {1,2}.
You can have an sql query like this,
public Cursor usingDistinct(String column_name) {
return db.rawQuery("select DISTINCT "+column_name+" from "+TBL_NAME, null);
}
you can use distinct argument while making query like this:
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Follow this doc for more clearity.
You can use below example query, as you have to give column name for distinct
Cursor cursor = db.query(true, YOUR_TABLE_NAME, new String[] { COLUMN_NAME_1 ,COLUMN_NAME_2, COLUMN_NAME_3 }, null, null, COLUMN_NAME_2, null, null, null);
COLUMN_NAME_2 - name of the column for distinct.
remember to add GROUP BY column names
Use boolean true iin the distinct argument, For example :
public Cursor query (**true**, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);

Android ContactsProvider: How to get name and phone number in one row for specific contact?

I want to retrieve a cursor where in one row contains both name and phone number. Is the way to do this pretty, without any loops?
Uri uri = ContactsContract.Data.CONTENT_URI;
getContentResolver().query(uri,
new String[] {ContactsContract.Data._ID, Phone.NUMBER, StructuredName.GIVEN_NAME}, null, null, null)
the code i use to get my contacts is:
Cursor cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
String Contactos =" CONTACTOS DEL TELEFONO "+cursor.getCount()+"\n";
while (cursor.moveToNext()) {
String name =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contactos+="Nombre: "+name+"\n"+
"Telefono: "+phoneNumber+"\n"+
"----------------------------\n";
}
then add the Contactos to a textview, and that's all!
see ya!

Android SQLite get all rows where column contains string always returns -1

textAs the title describes I'm trying to get all rows from a table where strings of a column contains a string anywhere in it. But they way I'm doing it now always return -1 rows.
This is how i'm doing it now but I have no idea of what I might be doing wrong. Suggestions?
Cursor cursor = database.query(DBHelper.TABLE_RECENT, null, DBHelper.COLUMN_TEXT+ " LIKE '%"+club_id+"%'", null, null, null, null);
Just write cursor.moveToFirst(); before getting data from cursor or just under query line.
Hope it will help you.
I guess, you're mis interpreting the structure of query. the second parameter is what you want to select. such as "select column1, column2 etc. Here's a line from my code, and it works for me.
Cursor cursor = db.query(table_employees, new String[] {KEY_ID, KEY_NAME,KEY_DEPARTMENT, KEY_PHONE_NUMBER, KEY_SALARY}, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
here "new string[] {key_id.....", the second parameter, is column names, that you wanted to select.
OR
alternately, you may simply do as:
String query = "Select * from "+DB_TABLE+ "Where" +column1 + "=" + t +"and"+ column2 + "=" +tt
db.rawQuery(query);
if you do something like,
String query = "Select * FROM Product where name LIKE '%" + Text + "%'"
Log.d("search query", query);
Cursor c = db.rawQuery(query, null);
c.moveToFirst();

How to LIMIT the fetching of the rows in the database [duplicate]

This question already has answers here:
Using the LIMIT statement in a SQLite query
(3 answers)
Closed 10 years ago.
I have a query like this:
Cursor _cursor = mbd.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_SYLLABICATION, KEY_WORD, KEY_PARTOFSPEECH, KEY_MEANING, KEY_SPEAKWORD},
null, null, null, null, null, null);
I just simply want to limit the rows to fetch from the database.
According to the documentation, you pass the LIMIT clause as the eighth parameter to query:
Cursor _cursor = mdb.query(SQLITE_TABLE,
new String[] { KEY_ROWID, KEY_SYLLABICATION,
KEY_WORD, KEY_PARTOFSPEECH,
KEY_MEANING, KEY_SPEAKWORD },
null, null, null, null, null,
"10");
Try this
cursor = resolver.query(STORAGE_URI, projection,
Media.BUCKET_DISPLAY_NAME + "=?",
new String[] { folderName },
" _id asc limit " + num);
or try this
final String orderBy = Constants.TABLE_CONVERSATION_FIELD_DATE + " DESC";
final String limit= 10;
return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);

Why is my getColumnIndex() returning -1?

Ok, so here is my database create statement:
create table entry (
_id integer primary key autoincrement,
date integer not null,
checknum integer,
payee text not null,
amount integer not null,
category text,
memo text,
tag text
);
After the datbase is created, and I make a call like:
mChecknum = cursor.getColumnIndex("checknum");
mChecknum is -1. I have pulled the database from the device and used SQLite Browser on it, and the checknum field is there.
Block around statement in question:
mDbHelper.open();
Cursor cursor = mDbHelper.fetchAll();
startManagingCursor(cursor);
COL_DATE = cursor.getColumnIndex("date");
Log.v("Main:123", "COL_DATE: " + String.valueOf(COL_DATE) );
COL_CHECKNUM = cursor.getColumnIndex("checknum");
Log.v("Main:125", "COL_CHECKNUM: " + String.valueOf(COL_CHECKNUM) );
COL_PAYEE = cursor.getColumnIndex("payee");
COL_DATE returns 1 and COL_PAYEE returns 2. Why is COL_CHECKNUM being ignored/passed over?
In my situation getColumnIndex was returning error on the column which had name which contained the "." symbol i.e.:
int colIndex = cursor.getColumnIndex("Item No.");
and despite the column "Item No." was inside the cursor (the debugger Watch showed it) trying to get the column index failed.
In my fetch/fetchAll database functions, I forgot to update the query with the new column.
For example, before i added the new column, my fetchAll code looked like:
public Cursor fetchAll() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_DATE,
KEY_PAYEE, KEY_AMOUNT, KEY_CATEGORY, KEY_MEMO, KEY_TAG},
null, null, null, null, KEY_DATE + " desc");
}
After adding the new column to the database, my fetchAll function looks like:
public Cursor fetchAll() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_DATE,
KEY_CHECKNUM, KEY_PAYEE, KEY_AMOUNT, KEY_CATEGORY, KEY_MEMO, KEY_TAG},
null, null, null, null, KEY_DATE + " desc");
}

Categories