i have been using a SQLite database, Theres a situation where i have a list which displays all the "name" field data of a table "table1". Now i have a button to insert data in "table1". The list is populated using a simple cursor adapter which is passed a cursor "cursor1" populated with the data. "cursor1" is prepared using the SQLite query - "SELECT * FROM table1". Now the moment i insert data, i need to update the list too.
My question is-
will the Adapter sense the database change automatically (i guess not)?
using cursor1.requery() is correct or should i use cursor1 = db.query("table1", null, null, null, null, null, null);
It would be helpful if you can throw some light on which 1 is better and in which situation. Coz for the situation which i explained above, the requery() command is not giving a valid result while the later 1 works fine. still cant understand what the problem could be.
will the Adapter sense the database
change automatically (i guess not)?
No, the Adapter will not sense the database change automatically.
using cursor1.requery() is correct or
should i use cursor1 =
db.query("table1", null, null, null,
null, null, null);
Use requery(). Here is a sample project from one of my books demonstrating the technique.
Related
Ok, so I have a database of an accounting app, which contains the information of various items purchased.
Said database looks like this:
Unsorted SQLite database
Upon running the query SELECT * FROM items ORDER BY totalPrice DESC; in SQLite Studio 3.1.1, the db sorts itself cleanly to this:
Updated database
However, trying to run mDatabase.rawQuery("SELECT * FROM items ORDER BY totalPrice DESC;") via the Java SQLite library results in nothing. The db isn't sorted at all. I've also tried running mDatabase.query("items", null, null, null, null, null, "totalPrice DESC;");
I'm at a complete loss as to what to do for this problem. Any help is appreciated.
The command doesn't seem to go through, the database remains the same as the top picture.
A SELECT statement does not change the database. It returns data from the database. The Cursor that you get back from rawQuery() or query() will have the rows in its result set sorted per the ORDER BY clause. The actual database, though, will remain untouched.
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 wrote a select query to access set of records from database by setting null for the argument 'orderBy' in the query(). I found that order of records returned by query() method when I run the application in mobile is completely different when I run the same sample application in tablet.
My Query:
Cursor cursor = database.query(true, tableName, downloadQueueTableColumnNames, selection, null, null, null, null, null);
Here, in the query orderBy field is null.
I hope someone to explain the reason behind this...
If you aren't ordering the results, they can be returned in any order. The same device doesn't have to give the same order if you call it twice in a row. If you want it in the same order every time, you must use an order by.
Hi I cant get unique rows tried this from the documentation:
public Cursor getcepaUnico(){
return database.query(true, "vino", new String[] {"_id", "cepa"}, null, null, null, null, "cepa", null);}
but shows duplicated rows even if the DISTINCT boolean is changed.
Also tried this:
public Cursor getCepaUnico() {
return database.rawQuery("select DISTINCT cepa from vinos", null);}
And the app crash after calling the method.
Setting distinct to true should have returned distinct results. Is it possible that your code which loops through the cursor is incorrect? You might want to post that also for review.
Regarding your rawQuery, you are using a different table name which is probably what is causing the crash. It should be "select DISTINCT cepa from vino" (not vinos) to match your query statement.
Not sure if this will solve your problem, but sometimes I just pull the db from the emulator (in the DDMS view in Eclipse) and run the query directly using an sqlite editor when my raw queries don't work; if the query shows what you want in the editor then use the query in the rawQuery method.
Firefox has a good sqlite editor.
I've successfully copied an existing SQLite database to android, as it was recommended here http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
and now when i'm writing
Cursor cur = db.db.query("student",
null, null, null, null, null, null);
The app breaks trying to create a cursor. I'm sure there is a "student" table.
Could you help me with suggesting any reasons of why this happens, please?
I just guessing, but db.db.query() looks wrong... you sure you didn't mean db.query() ?
A more collaborative answer than my comment. I would think you'll be fine by doing something like this tutorial does http://droidreign.com/2010/10/dev-tutorials-android-sqlite-database-basics/ . About half the way down is the part you are looking for.
Edit: I guess the problem with your query is that you ask for none of the columns (second parameter in your query). It should probably be at least one of your columns.