SQLite two tables matching logic - android

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.

Related

Android Sqlite Database Formation

In my application I take entry from user as :-
Name:- -------------------------
Favorite Fruit:- a)
b)
c)
--------- Add More ----------
Then I want to save it into a Sqlite database. Now I provide user to type in edittext search like this:-
Search Fruit:- Apple,Banana
And then I want to query my database and Print the name of those who like atleast Apple and Banana.
Now my issue is how do I make my database columns to achieve results faster.
Should I make two columns Name and FruitsLiked or something else. Because if I make only two colums then how do I search into database.
Thanks
Create one table n two columns. First column is for name , second one comma separated list of fruits.
Then as db query use like keyword based query

One to many relationship in android sqlite

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 to copy all the row data from one table to another in android?

I want to copy all the rows and columns from one table to another. I know the way using Cursor, The cursor gets all the data and i then taking it into the ArrayList and after doing the same thing reverse to insert it into another table.
But is there any simple and fast way to copy one table to another. If Yes,then please let me know..
You can copy the contents of one table and use it to populate another table as long the structure of the tables are same. INSERT INTO Destination SELECT * FROM Source;

Can you delete columns in an SQLite database?

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.

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