I have created a database and all works fine. But How I can get out last five rows, which one's column value is for example 1.
The select and insert function have synchronized function, so the reading and inserting doesn't happen same time. There has more than 300 hundreds rows, but I only need get cursor object last 5 rows (so I get all columns in one row) which one's column value is 1.
Thanks for any helps!
SELECT * -- list of the columns you want
FROM table
WHERE column1 = 1 -- rows with column1 = 1
ORDER BY (ordering columns) -- columns by which you want to order
LIMIT 5 -- and get the last 5
I think you want to select the first five rows which have all have a certain column with a value of one. If so, using a WHERE and LIMIT statement should help:
SELECT * FROM tbl WHERE the_column = 1 LIMIT 5
You can Use the method database.query();
as follows:
Cursor cursor = database.query(false, TABLE,new String[] {COLUMNS_TO_SELECT},"YOUR_WHERE_CLAUSE", null, null, null, null /*ORDER BY*/, "5"/*YOUR_LIMIT*/);
Have a look at Documentation
Related
Scenario is like
Before inserting into sqlite db I have to check whether is it reached a particular number, say 10. I know it can be done by using 2 queries for get and insert.
Can it be done in 1 query in android and sqlite
INSERT INTO Customers (name, age)
SELECT 'MM', 20 WHERE (SELECT count(*) from Customers) < 10;
By use this query, we only insert new customer to database when total customers in database < 10
Considering the documentation for INSERT you can also insert a result of a select statement. So instead of inserting the values directly, you could assemble a select statement to only return your default values with coalesce if and only if a condition for the count yields true.
This is mostly an idea and theoretical approach, but worth trying.
To check how many rows in database, you need to make
select * from
query and get the cursor object. Cursor object have getCount method which return you size of cursor, its simply means to show all your records in table.
Cheers!!!
I am having some problems with ORDER BY in android sqlite.
I am using this query to reorder my listview :
Select * From tbl_name ORDER BY WithOrder asc
Where WithOrder is an Integer type column. The expected behavior I was hoping for was that sqlite will reorder the rows as ....8,9,10,11,12.... but it is reordering the list according to the first digit instead as ....10,11,8,9....
Please help me with reordering the table with ascending values of integer...I can not opt for any other column or datatype to reorder as I depend heavily on WithOrder for general calculations when the user reorders the list.
Thanks!
Parvaz Bhaskar
While numbers are sorted according to their numerical value, strings are sorted lexicographically, beginning with the first character.
In particular, the character 1 is less than the character 8, so any string beginning with 1 (such as 11) is sorted before any string beginning with 8.
Recreate your table so that the WithOrder column has type INTEGER.
Cursor c = SQLiteDatabase_OBJ.query("Table_name", null, null, null, null, null, "WithOrder ASC");
Try this
Try this ORDER BY CAST(WithOrder AS INTEGER) ASC
I have a database which has a table recording when a vehicle was last fuelled up. It has a date column of type integer.
I need to be able to determine which was the last fuel up date. I am using:
Cursor cursor = db.query(FUEL_USE_TABLE_NAME, LAST_FUEL_UP_DATE_CLAUSE,
REGISTRATION_NO_COLUMN + "=? ", new String[]{registrationNumber},
null, null, null);
where LAST_FUEL_UP_DATE_CLAUSE is MAX(date_time), where date_time is the name of the column in question.
This works fine when there are entries in the table, but whilst writing a unit test I expected the cursor's row count to be zero when there were no entries in the table, but instead I get a single row with value zero. I.e. the maximum value in the date_time column is zero, when in fact there are no values.
I'm happy to code around this (use 0 to signify no records instead of -1), but would like to know if this is expected behaviour, or am I doing something wrong.
It seems it is expected behavior.
See SQLite documentation here
Specifically it says:
max(X)
The max() aggregate function returns the maximum value of all values
in the group. The maximum value is the value that would be returned
last in an ORDER BY on the same column. Aggregate max() returns NULL
if and only if there are no non-NULL values in the group.
I want to make a query such that the result will be shown in indistinct descending order.
For example, assume column ID has six rows. I need an query that shows me the list of IDs indistinct descending from 6 to 1.
EDIT: Based on the first post's text, the question is how do display query results in descending order. For instance, given the IDs
ID
--
1
2
3
4
5
6
Desired results:
ID
--
6
5
4
3
2
1
You need to add an ORDER BY ID DESC to your select statement.
ORDER BY
Use following statement....
select * from YOUR_TABLE_NAME ORDER BY ID DESC;
You can write like this:
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME+"
WHERE "+STATUS+" = "+"'0'" + " ORDER BY id DESC LIMIT 10", null);
return cursor ;
/* In the most simple and basic way, you can write it as */
SELECT ID
FROM your_table_name
ORDER BY ID DESC;
/* This should work fine with your problem and should give you your desired output */
I have a database that stores the rank of an Item.
The rank is an absolute value that will be correct if all the items are taken into account.
If I only need a subset say four of this items it will give me something like:
Rank RowId in the whole Table
---------------
4 114
8 71
70 16
83 7
I now need an int specifying the rank only in the subset where the max rank is the number of items in the subset in my example 1,2,3,4.
Is there a way to achieve this in my sqlite query? I only need one of the ranks in my Activity. I thought of ordering the results of the query by rank and then somehow get the position of item I want to rank at that moment. But how would I achieve this with sqlite?
I tried to create a temporary table and insert the subset into it like this:
CREATE TABLE rank(ID);
INSERT INTO position SELECT ID from items WHERE ITEM_ID = 3 ORDER BY POSITION;
SELECT RowID from rank WHERE ID = 9;
DROP TABLE rank;
This is working in SQLite Manager and will return the correct number. But if I do this in Android in fails saying that there is no table rank while compiling query
07-07 13:35:46.150: ERROR/AndroidRuntime(2047): Caused by: android.database.sqlite.SQLiteException: no such table: rank: , while compiling: SELECT RowID from rank WHERE ID = 9
EDIT: have to agree with #Matt the only way I've been able to do this is to use the temp table approach.
For what it's worth here's what it looks like...
create temp table if not exists <temptable>(Id integer primary key, rank);
insert into temptable(rank) select <column> from <table>;
select * from temptable;
EDIT: Actually that returns the ID associated with the row which isn't sequential so you won't always get 1,2,3,4... I'll have to think of something else. Sorry.
Not sure if I've understood your question. You basically want this?
Id Value
---------------
1 4
2 8
3 70
4 83
So you want to add a pseudo-column as the id no matter what your subset contains?
If that's correct then this should do it...
SELECT RowId, <other columns>.... FROM <table> WHERE <where>
Apologies if I've misunderstood.
You could output your query (ordered by rank) into a temporary table with an auto increment ID.
If you need to read only one row from a subquery you can always execute a limit on it, by providing the offset of how many records to be skipped first, and how much to be returned
so if you want to get 25th row you tell to skip 24, and return 1
select * from (SELECT * FROM table order by rank) limit 24,1