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.
Related
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 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.
I have 2 tables A and B.
Table A contains names and table B contains selected names.
Now I would like to perform the following query on these tables using greendao, Please let me know if it is possible and if it is not are there any alternatives (maybe a raw query).
select *
from A inner join B
on A.nameid = B.nameid
Also, Table A columns: id, nameid, name
and Table B columns: id, nameid, name, rating
I think this might help.
You can use the raw query as a fake join. And you get all you want in the Query object
Query query = ATableDao.queryBuilder().where(
new StringCondition("nameid IN " +
"(SELECT nameid FROM B_Table )").build();
Since "nameid" doesn't seems to be a unique identifier in your sample. I won't suggest to use Relations to solve this issue. If you are try to use Relations, you can find my previous answer here.
Try this:
List<ATableObj> listATableObj = ATableDao.queryRawCreate(", BTable BT
WHERE BT.nameid = T.nameid").list();
If you use greendao this works differntly:
Instead of your query you select rows from table a (or b) and if you need a field of b (or a) you call getB() (or getA()) to get the corresponding row of that table.
If you have rows in table a that have no match in table b and you have rows in table b that have no match in a and you onlly want to select everything that has matches uin both tables, you would have to do a raw query to filter the rows of a (or b).
I have a database with tables Teachers,Subjects and Teachers_Subjects in my android sqlite database.My tables have structure as shown in the image.
I need to query tables to get all subjects rows that are related to a single teacher. Initially I have the _id of the teacher.Using teachers _id I need to find the subjects.According to me first I need to find all the rows in Teachers_Subjects Table related to a Teacher and then make other query using the resulted rows and Subjects Table with JOIN statement to get all rows related to that teacher.I wanted to know is there any better way/query to accomplish this?If not then what should be the query for the solution mentioned above?
SELECT Subjects.*
FROM Teachers_Subjects JOIN Subjects
ON Teachers_Subjects.subject_id = Subjects._id
WHERE Teachers_Subjects.teacher_id = ?
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");