Maybe someone can help me before i lose my mind. I want to sort a SQL database.
SQLiteDatabase db = getWritableDatabase();
db.query(TABLE_NAME, new String[]{COLUM_MAME_1, COLUM_MAME_2, COLUM_MAME_3}, null, null, null, null, COLUM_MAME_3 + " ASC");
Q: db.query... manipulates or should manipulate the database? Cause I get no error and nothing happens to the database. If not? is there a simple way to do it?
Roland
In relational databases, tables do not have a guaranteed order.
To view the rows in a specific order, you must sort them whenever you are querying them.
Related
I plan to add a big local database to my application. What part of the db will be loaded in memory when I do the request? All tables from the DB or only the table that I request?
you don't have to worry about memory usage when using sqlite.
you can choose what data you want to access with a Cursor. that way you can access a specific table if you want, and get only it's data.
example:
Cursor c = db.query(TABLE_NAME, null, null, null, null, null,null);
c.moveToFirst();
this is a way to get a specific table's data.
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.
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'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 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.