How to select a random row in DbFlow? - android

How to get a random row from a table in DBFlow?
In SQLite I would simply write the query as:
select * from table_name ORDER BY RANDOM() LIMIT 1;
Whats the equivalent for it? I couldn't find anything and OrderBy accepts only column values. Thank you.
My question is about DBFlow ORM for Android, not for SQLite.

Related

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.

Sorting in SQLite

I have a database and I want to sort it. I came across several code that selects a sorted data but does not modify the database in itself.
I came across..
So how do I sort a database?
You don't sort tables in relational database.
You create index on them. And use ORDER BY in your query. That way your query result will be sorted.
When creating database create index on column to have better query performance:
CREATE INDEX sorted_idx ON table_name(indexed_column ASC);
more about indexes:
http://www.sqlite.org/lang_createindex.html
Then in code select using ORDER BY
db.rawQuery("SELECT * FROM table_name ORDER BY indexed_column ASC", null);
If you mean by 'sort a database' to simple sort a table,
Create temp table as sorted select, eg you have unsorted table1 a you want to sort it by column1 then do this:
CREATE TABLE temp_table1
AS SELECT *
FROM table1
ORDER BY column1;
Drop the original table.
Rename temp_table1 to table1.

Limit number of rows returned from sqlite

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;

Sqlite Android Quick Question

I wanna pull a random entry in my database that has downloaded value as 0. How can I do this?
SELECT * FROM table WHERE downloaded=0 ORDER BY RANDOM() LIMIT 1;

How to efficiently copy row data from one Table to another in SQLite android

how do i efficiently copy a row data from one table to another table, where both of them are the same structure. i could go the much harder way of retrieving initial values from the row in the first table and then inserting to the second table. But i feel there is a more efficient way this can be done. Thank you
insert into table1 select * from table2
You can use this snippet code:
sqliteDataBase.execSQL("INSERT INTO table1 SELECT * FROM table2");

Categories

Resources