Limit number of rows returned from sqlite - android

How can I limit the number of rows returned using activity.managedQuery()?
I am pretty sure there isn't a limit function in sqlite. I also do not want to use SQLiteDatabase.query() because I am using URI to get my queries.

Of course there is LIMIT in SQLite. See the SQLite docs. For example:
SELECT col1, col2 FROM mytable WHERE foo = 'bar' LIMIT 42;

Related

Sqlite order by rows order

I am using this query
"select * from SomeTable group by SomeColumn"
It is returns list with accenting order, but i need to same order like in database.
For example the order in database is:
p
a
s
But result is:
a
i
p
Sample
The result need to be like distinct by CityEN but with all columns and order like 1.Paris 2.Amsterdam 3.Istanbul
In Sqlite, each row of a table has a unique rowid, which you can use for sorting.
select * from SomeTable group by SomeColumn order by rowid;
In your statement, add this line to sort the results:
order by min(rowid)
Your query does not enforce any order with ORDER BY clause so no assumption about row order should be made. If you want specific order add i.e. ORDER BY SomeColumn. See docs about all available order options: https://www.sqlite.org/lang_select.html#orderby
By the rules of SQL, you can't count on getting records back in any specific order without specifying an ORDER BY clause in your SQL query.
In practice servers sometimes return values in the order in which they're inserted, in the order of the first index created, or in the order of the primary key--but you can't count on this behavior, and in fact I've seen the behavior change between database maintenance windows or after the database version is upgraded. You definitely wouldn't want to count on a DB engine to give you back records in any particular order if you write a SELECT statement without an ORDER BY clause.
The only real way to get your records back in the order you inserted them is to create a timestamp column and then sort on it during the SELECT. If you don't want to worry about populating that column on INSERT, have that column auto-populate itself with a timestamp (depending on your DB engine).

How to check number of rows in db before inserting

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!!!

Query FTS table MIN

I'm trying to get the lowest _id from my fts table with this query:
SELECT MIN(_id) FROM fts WHERE tbl_no=2 AND parent_id=6
The result I'm getting is 10. However the smallest _id is 9 and it fits the selection arguments.
If I instead use
SELECT _id FROM fts WHERE tbl_no=2 AND parent_id=6
and select the 1st row, I get the correct result: 9.
Does have something to do with the table being virtual (FTS)? I recently transfered from multiple tables to a single FTS and am experiencing this.
Am I guaranteed to get the results I want with the 2nd query, considering the table never updated and it's sorted by default.
Notes: I am running this on Android (tried rawQuery and query). I have the table in front of me and I know it's correct:
Is _id a numeric or a string?
With string comparison, '10' < '9'.
Try:
SELECT MIN(CAST(_id AS UNSIGNED)) FROM fts WHERE tbl_no=2 AND parent_id=6
To check. I would not use this in production however as it won't be able to use an index.
In FTS tables, all columns store string values, and the string '10' is lexicographically smaller than '9'.
Furthermore, MIN(SomeColumn) is not a full-text search query, and thus is not very efficient.
For a unique integer ID in FTS tables, you should use the internal docid column.

SQLite too many terms in compound SELECT

In my Android SQLite databese query I have an INSERT INTO statement followed by about 600 ('data1'),('data2')... tags, generated by code.
After db.exec(sql) I got this error: "too many terms in compound SELECT (code1); while compiling INSERT INTO.. "
Is there any way to increase this limitation?
The limit SQLITE_MAX_COMPOUND_SELECT cannot be raised at runtime,
So you need to split your inserts into batches of 500 rows each. This will be more efficient than inserting one row per query. For e.g.
BEGIN TRANSACTION
INSERT INTO tablename (data1,data2) VALUES ("data1","data2")
INSERT INTO tablename (data1,data2) VALUES ("data1","data2")
INSERT INTO tablename (data1,data2) VALUES ("data1","data2")
...
END TRANSACTION
Also see Insert Multiple Rows in SQLite

SQLite Insertion Order vs Query Order?

Will the order of rows returned by a query will be the same as the order in which the rows were inserted into the table, of SQLite database?
If Yes, Is this behaviour consistent?
If No, Can this be enforced?
I have a requirement of storing approx 500 rows of data, and which requires sorting/ordering from time to time. The data is in proper order, before the insertion.
Given the small number of rows in your table, this is probably what you need:
SELECT * FROM yourtable ORDER BY ROWID
For more information on ROWID, see these two links:
SQLite Autoincrement and ROWIDs and the INTEGER PRIMARY KEY
Even if the order may be consistent in one scenario, there is afaik no guarantee.
That is why SQL has the ORDER BY operator:
SELECT foo,bar FROM Table FOO WHERE frobnitz LIKE 'foo%' ORDER BY baz ASC;
Will the order of rows returned by a
query will be the same as the order in
which the rows were inserted into the
table, of SQLite database?
No, you can't count on that. All query optimizers have a lot of freedom when it comes to speeding up queries. One thing they're free to do is to return rows in whatever order is the fastest. That's true even if a particular dbms supports clustered indexes. (A clustered index imposes a physical ordering on the rows.)
There's only one way to guarantee the order of returned rows in a SQL database: use an ORDER BY clause.

Categories

Resources