I'm developing an android application for a store, which provides many functions. One of these is a function that allows the customer to search a product with some criteria (price,size,type... like in the picture ).
I guess I should work with SqliteDatabase , but I have no idea how I can make this multi-criteria search interface , so the user can query the database.
It's simple. after setup your database, you can use SQL queries and JOIN types with WHERE critaria and finally achive proper data, use Cursor class to iterate through results.
see:
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
http://www.w3schools.com/sql/sql_where.asp
http://www.w3schools.com/sql/sql_and_or.asp
http://www.w3schools.com/sql/sql_join_inner.asp
Before handling it, you should design your database properly.
Suppose,
List of tables -
1) product_type(_id<should be primary key to link another table>,
type_id<integer>, type_name<text>
2) product_price(_id<should be primary key to link another table>,
type_id<integer>, price<double>
3) product_type_size(_id<should be primary key to link another table>, type_id<integer>, size<text>
And create views as your requirement -
product_search(join your table properly)
And run your queries as requirement and match with the value of the view.
Go ahead--
Related
i am really stuck at this point of my android app development.
What i need is a way to save a changing amount of int or string-values (in a sql database). Yet im not even sure if this is the right approach, but let me explain:
In the app i am currently working on, you are able to create certain "events". Users should be able to apply for such events.
I have an external database with 2 tables:
first one for users - every user has a unique ID
second one for events - every event has a unique ID
I need each event to know what users applied for it. And i need each user to know what events they applied for.
I was thinking to save the Event-IDs in the User-Table and vice versa.
I just dont know how to do that since the amount of applicants/ID's can change. Is there a way to save Arrays in the database which can easily be edited (e.g. +/- one ID) and read?
Is this even the right way? I am very happy for any advise!
Thanks in advance!
What you seem to want is a many-to-many relationship. A user can be part of many events, and an event can have many users. That requires an additional table though:
Table: User Columns: UserId, Name, ...
Table: Event Columns: EventId, Name, ...
Table: UserEvents Columns: UserId, EventId, ...
In the new table, UserEvents, you would store the UserId's and EventId's like this:
UserEvents
UserId EventId
1 1
2 1
1 2
This means that if you selected UserId 1, the query would return EventId 1 & 2. If you selected EventId 1 the query would return that UserId 1 & 2 would be attending.
This is the standard and recommended way to deal with many-to-many. It's very flexible and can easily be scaled.
You could either use a Compound key (Composite Key) for this table, or create a column specifically as a Primary Key. The code below can be used, and manipulated, to create both your table and Compound/Composite key (I'm guessing on data types).
CREATE TABLE UserEvents
(
[UserId] INT NOT NULL,
[EventId] INT NOT NULL
CONSTRAINT PK_UserEvents PRIMARY KEY NONCLUSTERED ([UserId], [EventId])
)
I would add a third table (e.g. UserEvents) to store which events a user has applied for, along with other relevant attributes (e.g. ApplicationTime, ApplicationStatus). This association would have a foreign key relationship back to the related tables and resolve the many-to-many relationship between users and events.
What you have there is called a "many-to-many" relationship between to tables which can only be resolved by the introduction of a third table between your two tables that stores the associations.
This table would contain the User-ID and the Event-ID as foreign keys (and maybe additional information).
I am designing an Android app with a fairly complex (normalized) database structure. My question is, what is the best design pattern for working with my data?
The database has a table Books, a table Authors, a table Collections (of books), and a junction table Books_Collections that relates the two based on an integer ID. I say that it is fairly complex because I want to display a list of books & authors in a specific a collection in a ListView in format "Book Title (Author Name)", so I need to be able to perform the following query (I will abbreviate the table names A, B, C, and B_C):
SELECT B.title, A.name
FROM B
JOIN A ON B.author_id = A.id
JOIN B_C ON B_C.book_id = B.id
WHERE B_C.collection_id = [variable]
I have seen some people advising developers to wrap their Sqlite databases in a Content Provider to easily take advantage of the Loaders introduced in Honeycomb. But is this really feasible for such complex queries? (If so, how?) or would it be better to just use DAO since I am not sharing my data with other apps? And if using DAO, should a custom AsyncTaskLoader be created to connect the data to a ListView?
EDIT
One more important detail: I want to change the background color of the list items based on a boolean variable in Books marking whether or not the book has been read, and a context menu will allow users to delete books from the list, so the list needs to adjust instantly to data changes.
Yes, is doable and easily done
I'm no expert but im followin the pattern used by google on his Google I/O app
https://code.google.com/p/iosched/
Check the provider package where u have the 3 classes needed for using the pattern
ScheduleDatabase.java
(definition of sqlite3 database)
ScheduleProvider.java
(Content provider atttached to this database)
ScheduleContract.java
(Contract defined to expose the provider)
Personally, in the case of join
in the Database File i define
interface Tables {
String TABLE1= "table1";
String TABLE2= "table2";
// JOINS
String TABLE1_JOIN_TABLE2 = "table1"
+ "LEFT OUTER JOIN table2 ON table1.table_id= table2.tableid";
}
and then in your provider map a provider entity to this join.
I ended up using database helpers/adapters and creating a custom AsyncTaskLoader. I wasn't able to find a ton of examples of custom ones, and there's not great documentation on it, but it's not too hard to figure out. Works like a charm.
Ok, I have a database with id column as timestamp
I made an activity list from the db.
I want to manage the db (delete rows) using the list, but the thing is I don't want to
View the whole timestamp, in every row I'll put only the time with some info and
I want to group the list ,as in contacts grouped by alphabet, by the date.
First, how can I make group in an activity list? (Making groups to the output list not the db)
Second, what is the best way to implement this? When user chooses an item and confims delete
I should delete it from the db but I have only patial timestamp...
(My only link to the db is the timestamp - I don't actually know where to store it in the list and I don't want to put it as a string in the text view, do a substring to get it back - is there another way to do this?)
I tried to search tthe web for some examples but I only found a simple ones.
Thnx :-)
?
I think what you're trying to do is create a database of tasks identified by a timestamp. You probably don't want to use a timestamp as a unique ID for the row. Instead, use an integer and qualify it as "PRIMARY KEY" when you create the database.
group the list? I'm not sure why you want to do this in the structure of the database. It's more common to group the list in the output, and leave the db itself in as flat a structure as possible.
Retrieve the primary key when you display a list of tasks. When the user clicks a task, use the primary key to choose the task to delete. You don't have to display the primary key; it serves as a behind-the-scenes "link" between the displayed info and the db row.
http://www.vogella.com/articles/AndroidListView/article.html
I should use cursor adapter for managing db.
And this one for grouping a list:
http://code.google.com/p/android-amazing-listview/
Thnx for the efforts
this is more of a question of theory than anything else. I am writing an android app that uses a pre-packaged database. The purpose of the app is solely to search through this database and return values. Ill provide some abstract examples to illustrate my implementation and quandary. The user can search by: "Thing Name," and what I want returned to the user is values a, b, and c. I initially designed the database to have it all contained on a single sheet, and have column 1 be key_index, column 2 be name, column 3 be a, etc etc. When the user searches, the cursor will return the key_index, and then use that to pull values a b and c.
However, in my database "Thing alpha" can have a value a = 4 or a = 6. I do not want to repeat data in the database, i.e. have multiple rows with the same thing alpha, only separate "a" values. So what is the best way to organize the data given this situation? Do I keep all the "Thing Names" in a single sheet, and all the data separately. This is really a question of proper database design, which is definitely something foreign to me. Thanks for your help!
There's a thing called database normalization http://en.wikipedia.org/wiki/Database_normalization. You usually want to avoid redundancy and dependency in the DB entities using a corresponding design with surrogate keys and foreign keys and so on. Your "thing aplpha" looks like you want to have a many-to-many table like e.g. one or many songs belong/s to the same or different genres. You may want to create dictionary tables to hold your id,name pairs and have foreign keys referencing these tables. In your case it will be mostly a read-only DB so you might want to consider creating indexes with high FILLFACTOR percentage don't think sqlite allows it to do though. There're many ways to design the database. Everything depends on the purpose of DB. You can start with a design of your hardware like raids/file systems/db block sizes to match the F-System's block sizes in order to keep the I/O optimal and where to put your tablespaces/filegroups/indexes to balance the i/o load. The whole DB design theory/task is really a deep subject which is not to be underestimated nor is a matter of few sentences in the answer of stackoverflow. :)
without understanding your data better here is my guess at what you are looking for.
table: product
- _id
- name
table: attribute
- product_id
- a
In my application ,am work with a large database.Nearly 75000 records present in a table(totally 6 tables are there).i want to get a data from three different table at a time.i completed that.but the search process was slow.how can i optimise the searching process?
You might want to consider using the full-text search engine and issuing SELECT...MATCH queries instead. Note that you need to enable the FTS engine (it's disabled by default) and create virtual tables instead of regular tables. You can read more about it here.
Without being able to see the table structure (or query) the first thing I'd suggest is adding some indexes to the tables.
Lets say you have a few tables like:
Author
id
last_name
first_name
Subject
id
name
Book
id
title
author_id
subject_id
and you're wanting to get all the information about each of the books that an author with last_name="Smith" and first_name="John" wrote. Your query might look something like this:
SELECT * FROM Book b
LEFT JOIN Subject s
ON s.id=b.subject_id
LEFT JOIN Author a
ON a.id=b.author_id
WHERE a.last_name='Smith'
AND a.first_name='John';
There you'd want the last_name column in the Author table to have an index (and maybe first_name too).