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 = ?
Related
I have two tables in Realm database. User table has a column "Tag" and Detail table has a column "description". I want to perform a search in a way that when user type something, it should get searched first in user table and then in detail table and then show the combined result.
Thanks in advance!!
You cannot combine the result of two queries. Instead your data model should use relationships.
I have two tables in my SQLite database one is a table of everything (main), another is a "favorites" table (favorites) and I want to load a Cursor for my ListView and have a star greyed out or yellow if its in favorites. Both tables have the exact same columns and I was hoping to load a Cursor with these columns and an additional column favorite that can be a boolean or a 1 or 0. The favorite row is simply copied from the main table, so its contents will be the same. Then, when the star is tapped, update the favorites table and call notifyDataSetChanged() on my adapter and that should update the ListView.
I've tried using JOIN and loading tables with AS aliases but I'm having trouble. Can anyone help me with a SQLite query to achieve this? Thank you!
You can use a select like this
SELECT M.FieldA, M.FieldB,... M.Fieldx,
FROM MainTable AS M JOIN Favorites as F
ON M.Id = F.Id;
In this example Id is the matching field. Additionally you can replace M.FiledX by isnull(M.Fieldx, 'something') or any other function. But, essentially if M.Fieldx returns null, then it is not in the favorites.
Also it's worth to say that in your favorites table you don't need to duplicate the row, just save the ID of the record in your main table.
Using a LEFT JOIN I've been able to achieve what I want and added a column is_fav in the favorites table.
SELECT M.*, F.is_fav
FROM main AS M
LEFT JOIN favorites AS F ON M.title = F.title
This may seem odd to add that column but its used elsewhere.
The Android app that I am currently working on dynamically adds columns to an SQLite database. The problem I have is that I cannot figure out a way to remove these columns from the database.
If I add column A, B, C, D, and E to the database, is it possible to later remove column C?
I have done a lot of looking around and the closest thing I could find was a solution that requires building a backup table and moving all the columns (except the one to be deleted) into that backup table.
I can't figure out how I would do this, though. I add all the columns dynamically so their names are not defined as variables in my Java code. There doesn't seem to be a way to retrieve a column name by using Android's SQLiteDatabase.
SQLite has limited ALTER TABLE support that you can use to add a column to the end of a table or to change the name of a table.
If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.
For example, suppose you have a table named "t1" with columns names "a", "b", and "c" and that you want to delete column "c" from this table. The following steps illustrate how this could be done:
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(a,b);
INSERT INTO t1_backup SELECT a,b FROM t1;
DROP TABLE t1;
CREATE TABLE t1(a,b);
INSERT INTO t1 SELECT a,b FROM t1_backup;
DROP TABLE t1_backup;
COMMIT;
SQLite doesn't support a way to drop a column in its SQL syntax, so its unlikely to show up in a wrapper API. SQLite doesn't often support all features that traditional databases support.
The solutions you've identified make sense and are ways to do it. Ugly, but valid ways to do it.
You can also 'deprecate' the columns and not use them by convention in newer versions of your app. That way older versions of your app that depend on column C won't break.
Oh... just noticed this comment:
The app is (basically) an attendance tracking spreadsheet. You can add
a new "event" and then indicate the people that attended or didn't.
The columns are the "events".
Based on that comment you should just create another table for your events and link to it from your other table(s). You should never have to add columns to support new domain objects like that. Each logical domain object should be represented by its own table. E.g. user, location, event...
Was writing this initially. Will keep it if you're interested:
Instead of dynamically adding and removing columns you should consider using an EAV data model for that part of your database that needs to be dynamic.
EAV data models store values as name/value pairs and the db structure never needs to change.
Based on your comment below about adding a column for each event, I'd strongly suggest creating a second table in which each row will represent an event, and then tracking attendance by storing the user row id and the id of the event row in the attendance table. Continually piling columns onto the attendance table is a definite anti-pattern.
With regards to how to find out about the table schema, you can query the sqlite_master table as described in this other SO question - Is there an SQLite equivalent to MySQL's DESCRIBE [table]?
As per SQLite FAQ, there is only limited support to the ALTER TABLE SQL command. So, the only way you can do is that ou can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.
Also you can get the column name from the database using a query. Any query say "SELECT * FROM " gives you a cursor object. You can use the method
String getColumnName(int columnIndex);
or
String[] getColumnNames();
to retrieve the names of the columns.
I was wondering if it's possible (it should be) to query multiple tables simultaneously (several at once) in SQLite. Basically I have several tables that have the exact same columns, but the data in them is just organized by the table it's in. I need to be able to use SELECT to get data from the tables (I heard UNION could help), which matches a condition, then group the data by the table it's in.
In other words, would something like this be possible?
SELECT * FROM table1,table2,table3,table4,table5,table6 WHERE day=15 GROUP BY {table}
I'd rather not resort to having to query the tables individually as then I would have a bunch of Cursors that I'd have to manually go through and that would be difficult when I only have one SimpleCursorAdapter? Unless a SimpleCursorAdapter can have several Cursors?
Thanks.
EDIT: The structure of my tables:
Main Table - contains references to subtables in a column "tbls"
and meta-information about the data stored in the subtables
Subtable - contains reference to subsubtables in a column "tbls"
and meta-information about the data stored in the
subsubtables
Subsubtable - contains the actual entries
Basically these tables just make it easier to organize the hierarchical data structure. I suppose instead of having the subsubtables, I could keep the actual entries in the subtable but add a prefix, and have a separate table for the meta-information. It just seems it would be harder to delete/update the structure if I need to remove a level in this data set.
You can create view based on your tables, the query of your view is union of your tables.
create view test as select * from table1 union select * from table2
now you can filter data as you want
for more info check union & view
http://www.w3schools.com/sql/sql_union.asp
http://www.w3schools.com/sql/sql_view.asp
In the end, I decided to forgo having many subsubtables, and instead adding another column like Tim and Samuel suggested. It will probably be more efficient as well then chaining SELECTs with UNION.
Really all I want to do is say the query SELECT DISTINCT column FROM table but I can't figure out how to structure it in the enourmous query methods that are part of SQLiteDatabase
I'm just trying to get the names of all contacts in a table.
You seem to know the sql query you want to run. Have you tried using rawQuery()?
You will probably find this version of the SQLiteDatabase#query method most useful.