In one of my app i have database table like
Table 1 :Menu
column
ID,
MenuName,
MeuID
and Table 2 Menuproduct
column
menuId,
menuprice
I need it like
i header of expandble list
menu name
and in child price. like that
how to make it possible? as i have to pass MenuId to get menu price
Please help
Thanks
You need to use join query to get the detail from sqlite database
If you want the details based on MenuID then your query will be like following
SELECT Menu.MenuID, Menu.MenuName, menuprice
FROM Menu
LEFT OUTER JOIN Menuproduct ON Menu.MenuID = Menuproduct.menuId;
http://www.tutorialspoint.com/sqlite/sqlite_using_joins.htm
you need to store the result as custom object in array list.Checkout following
exapandible listview
Related
I am having trouble figuring out what to do with my tables.
Scenario -
Recipe Book!
I have a home page with an add button, when clicked on that I add a meal, when meal save button is clicked I add a recipe. Now Here is where it gets tricky.
On my recipe add page I want to be able to Add an ingredient then have the option (Press + button) to add another ingredient, but I am unsure on how to go about this....
Tables Currently:
Meal Table
e.g Meal id, meal name
Recipe table
e.g Recipe id, recipe name, Ingredients
Now do I have to make a table called Ingredients with 20 columns for a user to be able to put in 20 ingredients or is there a simpler way??
Any help would be great.
EDIT: Silly question
By basic Database Principles, multi valued attributes (ingredients in your case) should be placed in a different table with the (ingredient id, recipe id) as the primary key. In this table you can have 3 columns namely recipe id, ingredient id and ingredient name. I hope this is what you wanted to achieve.
You have a couple of options. You can link your ingredients to another table or you can serialize your ingredients list into a single value for storage and deserialize it on retrieval.
hello friends i have 2 tables like below
tab_a --> "unit_info_id" ,"unit_id" ,"p_id" ,"p_bathroom" ,"p_bedroom" ,"p_size" ,"p_rent" ,"p_isrent" ,"u_note" ,"r_id" , "isactive"
tab_b--> "rent_id" ,"unit_info_id" ,"t_id" ,"rent_amount" ,"rent_note" ,"rent_date" ,"rent_time" , "r_id"
and my query is as below
SELECT a.* ,
(SELECT SUM(rent_amount)
FROM tab_b
LEFT JOIN tab_a ON tab_b.unit_info_id = tab_a.unit_info_id
WHERE tab_b.rent_date
between '2015-02-03' and '2015-12-03'
GROUP BY tab_b.unit_info_id
) AS "TotalIncome"
FROM tab_a a ,tab_b b
WHERE a.r_id=1 group by a.unit_info_id
when i run above query it gave me duplicate value in totalIncome alias like below image
as per my current data i have rent_amount only for unit_id =1 , in unit_id=2 and unit_id=3 there are no any data in my tab_2 table but when i run above query it gave me output something like above image i need totalIncome as per particular unitwise so any idea how can i solve it?
EDIT
Tab_a content
Tab_b contant
It's because you add SUM as an additional row so the SUM always appears at the end as TotalIncome since you GROUP BY by ID. What behavior do you expect?
OK, I didn't get you at first. You have to rewrite that SELECT. Can you post the content of tab_b as you did with tab_a?
Check if this works
SELECT tab_a.*, SUM(tab_b.rent_amount) AS TotalIncome FROM tab_a INNER JOIN tab_b ON tab_b.unit_info_id=tab_a.unit_info_id WHERE tab_b.rent_date between '2015-02-03' AND '2015-12-03' AND tab_a.r_id='1' GROUP BY tab_a.unit_info_id
Also keep in mind you have typos in what you have originally posted.
I'm new to android. I want to retrieve employee details from database and want to display those data into table layout in weekly basis. for that i want to create table layout with fixed header column like employee name, employee id, employee work status on first week,second week,third week,fourth week etc. and dynamic rows means number of employees is unknown.
How to create this?
First of all you have to create Static Headers using Table Layout and than create Dynamic Table Layout inside your Database query which is retrieving your data on weekly basis.
And if you want add First week and Second week Separate header on table than use some trick.
Try this.
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.
So my setup is that I have a database with two tables. My main input class is a basic form which has a spinner to select a category. Category is the second table, the spinner is powered by this database table and the main table has a foreign key of this table.
I have the spinner populating correctly etc but when storing the selected item from the spinner it is storing the cursor position which looks like:
android.database.sqlite.SQLiteCursor#435b9ba0.
rather than the actual contents of it which should be the name from the database. The code I am using is:
String fkstring = mSpinner.getSelectedItem().toString();
If anyone could let me know how to solve this that would be great.
use .getString(0) to get a string, .getString(0) will get the first column .getString(1) the second etc etc.
String fkstring = mSpinner.getSelectedItem().getString(0);
Try String fkstring = (String) mSpinner.getSelectedItem();