SQLite: Multiple select statements vs one select statement - android

In my Android project, I have a SQLite table data containing two rows, id and name. It has +/- ten records. Note that records may have been deleted, so the ids could be non-sequential.
In an activity I need the corresponding names of +/- 100 ids (obviously with lots of duplicate values).
I could do this by executing the SQLite query SELECT name FROM data WHERE id = x a hundred times.
Another option is executing the SQLite query SELECT id,name FROM data one time, then store the ids and the names in an array, and then get the names of the ids by java (String name = namesArray[Arrays.binarySearch(idsArray, x)];
Which method should I use? Or is there even a better solution?

The second option is much faster.

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

Android - Parse Query - need to know if a string is contained within one of multiple columns in my database table

I have an Android application using parse as a backend. One of my tables in my parse database contains groups of users, one row in this table contains 6 columns of users to form one group.
I would like to return each row where the current user is contained in one of the columns.
My question is what is the most optimum way of achieving this?As it stands I can think of two ways:1. query each row in the groups table and loop through each cloumn looking for the current users username.
2. if possible - use a parse query that says if currenUser equals member 1 or member 2 or member 3 or member 4 or member 5 or member 6. But I don't know if this is possible
Any pointers or help would be appreciated?
For your ParseQuery<ParseObject> query object a whereEqualTo method is available query.whereEqualTo(key, value); try using your column name in place or key and your desired value in in place of value.
Using this you will only get the rows which contains your desired values. It just works as the where clause in SQL qyery. so you will not need to iterate over all the DB entries.

How to generate database with table of variable number of columns?

In my Android app, I need to temporarily store some data in a form of table such as follows:
id | column 1 | column 2 | ... | column n
The data are downloaded from a server whenever users press a button. However, the data table doesn't have a fix number of column (as well as row) every time user downloads it from the server. For example, the server may send data with 3 columns the first time. Then it might send data with 5 columns the second time, etc...
Given this scenario, I think the database is probably the right data structure to use. My plan is to create a database, then add and delete tables as necessary. So I have been reading various tutorials on Android database (one example is this one http://www.codeproject.com/Articles/119293/Using-SQLite-Database-with-Android#). It seems to me I cannot create new table with variable number of columns using the sqlite database. Is this correct? In the onCreate(SQLiteDatabase db) method, the "create table" command must be specified with known number of columns and their data types. I could provide several "create table" commands, each with different number of columns but that seems like very crude. Is there a way to create database tables with variable number of columns on the fly?
Another alternative probably using several hash tables, each storing one column of the data table. I'm seriously considering this approach if the database approach is not possible. Any better suggestion is welcomed.
There is no such thing as a variable number of columns in an SQLite data base. Also, adding and deleting tables dynamically seems like a horrible hack.
It sounds like you want to store an array of values associated with an id. I suggest you think in terms of rows, not columns. Use a table structure like (id, index, value); each array of values returned by the server results in as many rows as necessary to store the values.

Querying from multiple tables in Android SQLite?

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.

Retrieving a set of rows from the sqlite database

SELECT * FROM <TableName> WHERE <attribute(id)> IN <ArrayList type>
but theres an error
04-24 21:18:41.748:
ERROR/Error(29495):
android.database.sqlite.SQLiteException:
no such table: 1: , while compiling:
SELECT * FROM Main WHERE id IN [1]
basically i want to select those rows with Attribute(id) which are present in an ArrayList...
but the format of the ArrayList is not the same as the one reqd for this type of query(i guess)
and i think this is the reason, its not workin correctly
if i query it with:
SELECT * FROM <TableName> WHERE <attribute(id)> IN <Integer>
it shows me the correct result... but obv it only selects that particular id's row...
note: ArrayList is replaced with Integer... (which is the data-type of my attribute(id))
Thanks in advance :)
There is no such thing as IN <ArrayList type>.
You need to compute the enumerating string yourself by using JAVA code.
SELECT * FROM <TableName> WHERE _id IN ('1','2','3','4','5')
Also please note that on Android the primary key is recommended to be _id with underscore.
When working with apps, I prefer to let the List I'm using to contain the elements it's holding like:
List<ElementType> = ...
By doing this, you can simply ask each element for their id's. Pentium10's solution will do the job, but I prefer doing this as it gives more flexibility.

Categories

Resources