In an Android application, I got a query response from the database and I created a cursor object for it (for example, a contact list).
I want to delete some rows of this database response and create a new cursor that matches the database answer but there are no extra rows.
In fact, I want to add some code like a proxy in the application, which means that it gets the query answers, Edit it and then get it to to the new cursor object. Can you guide me? What should I do?
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " LIMIT 5");
// remove some results
Cursor newCursor= answers with new results
Related
Is it possible to retrieve all the phone numbers with just one query(it's ok even if it's a bit slow)?
I found many topics that suggest to make query for the contact list and then a query for each contact but that's really slow.
The only solutions that I found with one query, retrieve just the main number. I need all the numbers of each contact plus the type.
Thanks.
try this
Cursor cursor = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, Phone.DISPLAY_NAME + " ASC");
I want to write a Where clause for Sqlite db & my query is as follows,
Cursor cursor = database.query(table_name,new String[]{COLUMN_1,COLUMN_2,COLUMN_3}, COLUMN_1='1', null, null, null, null);
//COLUMN_1='1' is my WHERE Clause & its datatype is text
I am not able to execute this query & its giving Nullpointer exception immediately after this statement.
I dont know the reason I think there is some problem with text datatype.
I have spent almost half a day searching for the solution but disappointed.
PS: I've also tried using ,
Cursor cursor = database.query(table_name,new String[]{COLUMN_1,COLUMN_2,COLUMN_3}, COLUMN_1=?, new String[] {'1'}, null, null, null);
LogCat:
But Same problem.
Do you initialize the database? Do you have anything like this:
database = new DBAdapter(this);
Also you need to open the database before the query.
database.open();
I have found the problem, the database was not opened & hence giving NPE. Thank you guys for your patience.
i want to get contact name and phone number in a list view using cursors and simple cursor adaptor. I have seen codes which loops over all the contacts in the database. Is there an efficient way to do this by merging cursors and using simple cursor adaptor to make a list view?
You get everything about contacts in this Cursor
Cursor cursor = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
But the to get the name or id from this cursor you have to loop through there is no other possibility.
I'm having some strange behaviour.
If the database does not exists, and i execute the following code in my Activity:
listOpenHelper = new ListOpenHelper(ManageListActivity.this);
db = listOpenHelper.getReadableDatabase();
Cursor cursor = db.query(ListOpenHelper.TABLE_NAME, null, null, null, null, null, BaseColumns._ID + " DESC");
The database is create and the table LIST is created, no problem here.
The problem is when i try to execute a similiar block in other Activity:
productListOpenHelper = new ProductListOpenHelper(ProductListActivity.this);
db = productListOpenHelper.getReadableDatabase();
Cursor cursor = db.query(ProductListOpenHelper.TABLE_NAME, null, null, null, null, null, ProductListOpenHelper.NAME + " ASC");
In this case, i get the Exception "android.database.sqlite.SQLiteException: no such table: list: , while compiling: SELECT * FROM list ORDER BY _id DESC"
If i erase the database and execute first the above block, and after the first block, the error will be in the productlist table.
I need to create all my tables in the first execution?
I like to create the tables when the user enter in each of the Activities, there is some good way to do this?
Thanks!
You have two different databases, correct? If not, you probably shouldn't have two different helper classes.
Also, creating helpers as you are may not be ideal. See blog post:
http://www.touchlab.co/blog/single-sqlite-connection/
Please post the code for your helper classes and why you have two different classes.
why you are using two different helpers? i don't think its a good way... You can create the tables in the initially and you can obtain the db whenever you want, with a single helper.
I have this query in SQLite that I want to return a value based on the date passed. The date is in the database as a String. I know that there is data in the database with that date, but it just returns nothing. I dont know why. When I make the query without comparisson parameters, it returns all the data normally, but using the WHERE clause in the query method, it stops returning. Maybe its just a matter of the SQL syntax. Anyways, here is the statement:
Cursor cursor = db.query(TABLE, new String[] {"_id", "value"}, "current_date = ? ", new String[] {date}, null, null, null);
EDIT
I've made this query in SQLite Database Browser, and it doesn't return any values. Even if its there.
I've seen that this is quite funny and confused. I've tested on Database Browser and also on phone, when I pass the current day, the 'Today' day, it gets the data, but when I pass another day that isnt today, it returns nothing. Very, very weird.
And in Database Browser, I've selected the column that contains the date and it returns only the 'Today' date, even if before there are another dates.
if your date is stored in database in format 'dd-mm-yyyy',then try using it like-
Calendar cal=Calendar.getInstance();
Cursor cursor = db.query(TABLE, new String[] {"_id", "value"}, "date = ? ", String.format("%1$td-%1$tm-%1$tY",cal), null, null, null);
For other formats,please refer http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
The column name was 'current_date' and this is a SQLite function name. So it didn't get the other dates, just the current date.