android sqlite database cursor - android

I have an android application which has a small database as a db file in the asset folder. The file is called nametoareamap.db.It has a single table named 'map'. The table has two columns(Names and Areas) as following:
Names Areas
Aaron A
Chris A
Helen B
Tim B
My application takes names as input from the user. Suppose some user had input: Aaron, Tim.
In this case, in terms of name there are two matches with the database. But they come from different areas. Aaron from A and Tim from B. I want to implement the following logic.
If match > = 2 && the area of the matches are same
{ i take a decision}
else
{i decide something else }
Can anyone kindly provide me the code required to do this with cursor and sqlite databases on Android. I have a database adapter already.Thanks in advance

Assuming the following table layout
CREATE TABLE name_area (
_id INTEGER PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
area TEXT NOT NULL,
UNIQUE(name, area)
)
And the following values
name area
---- ----
Aaron A
Chris A
Bunny A
Ron A
Burgundy B
Helen B
Tim B
Say you want to know if Aaron, Ron and Burgundy all are in the same area or not:
SELECT COUNT(*), area FROM name_area
WHERE name='Aaron' OR name='Ron' OR name='Burgundy' GROUP BY area
This would return two rows.
2 A
1 B
i.e. two of the are in the same area (A), one is in another (B):
Expressed as a Cursor you could check it like this:
Cursor cursor = ...; // Format your query & do the SELECT
try {
if (cursor.moveToNext()) {
int count = cursor.getCount();
if (count < 2) {
// Everyone is in the same area
int n = cursor.getInt(0);
// Now verify 'n' against the number of people you queried for
// if it doesn't match one or more didn't exist in your table.
} else {
// People are in different areas
int n = 0;
do {
n += cursor.getInt(0);
} while (cursor.moveToNext());
// Now verify 'n' against the number of people you queried for
// if it doesn't match one or more didn't exist in your table.
}
} else {
// Oops nothing was found.
}
} finally {
cursor.close();
}

Related

How to get the base alphabet of a foreign word and sort them accordingly like in device local contact?

For example, let's say that a local contact have multiple Japanese contacts with the name たなか, つなき, and てるてる. In the local contact these names will be sorted under the section index letter た.
For what I've read, it is possible to sort them via collate, but how do you sort them to index た or even know that they should go to the た index?
Thank you.
The API has a trick for returning that list of alphabet letters, and optionally also including counts per character.
See official docs for EXTRA_ADDRESS_BOOK_INDEX:
https://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#EXTRA_ADDRESS_BOOK_INDEX
Example code:
Uri uri = Contacts.CONTENT_URI.buildUpon()
.appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true")
.build();
Cursor cursor = getContentResolver().query(uri,
new String[] {Contacts.DISPLAY_NAME},
null, null, null);
Bundle bundle = cursor.getExtras();
if (bundle.containsKey(Contacts.EXTRA_ADDRESS_BOOK_INDEX_TITLES) && bundle.containsKey(Contacts.EXTRA_ADDRESS_BOOK_INDEX_COUNTS)) {
String sections[] = bundle.getStringArray(Contacts.EXTRA_ADDRESS_BOOK_INDEX_TITLES);
// sections will now contain an array of characters that represent the first letter of each available contact name
// optionally - if you need counts per letter - int counts[] = bundle.getIntArray(Contacts.EXTRA_ADDRESS_BOOK_INDEX_COUNTS);
}

Cursor column index changes from phone to phone

My application allows a user to call the most recent number that was called out. By hitting the "Call" button with an empty text box it will grab the latest Outgoing number in my ORM database. The issue however happens on only some phones.
When I pull my data I do so with the following code:
Dao<RecentCallsInfo, Integer> dao = null;
if (getActivity() instanceof MainActivity) {
MainActivity main = (MainActivity) getActivity();
dao = main.getDatabaseHelper().getRecentDataDao();
}
QueryBuilder<RecentCallsInfo, Integer> qb = dao.queryBuilder();
qb.orderBy(RecentCallsInfo.RECENT_COLUMN_ID, false);
qb.where().eq(RecentCallsInfo.RECENT_COLUMN_CALL_TYPE, "Outgoing");
// when you are done, prepare your query and build an iterator
CloseableIterator<RecentCallsInfo> iterator = dao.iterator(qb.prepare());
// get the raw results which can be cast under Android
AndroidDatabaseResults results = (AndroidDatabaseResults)iterator.getRawResults();
Cursor c = results.getRawCursor();
if(c.moveToFirst()){
if(!c.getString(RecentQuery.COLUMN_NUM).isEmpty()){
System.out.println("Recent Record: \n Name:" + c.getString(RecentQuery.COLUMN_NAME) +
"\nNum:" + c.getString(RecentQuery.COLUMN_NUM) + "\nNumType:" + c.getString(RecentQuery.COLUMN_NUM_TYPE)+
"\nCallType:" + c.getString(RecentQuery.COLUMN_CALL_TYPE));
etcalle.setText(c.getString(RecentQuery.COLUMN_NUM));
}
}
//RecentQuery.COLUMN_NAME = 5
//RecentQuery.COLUMN_NUM = 6
//RecentQuery.COLUMN_NUM_TYPE = 0
//RecentQuery.COLUMN_CALL_TYPE = 2
When I debug my c Cursor, it gives me these columns with these values
[recentNumberType, recentCallCost, recentCallType, recentCallerID, recentDate, recentName, recentNumber, _id]
Name:anthony
Num:(111) 111-1111
NumType:Mobile
CallType:Outgoing
When I get my c Cursor on another phone I get
[recentCallCost, recentCallType, recentCallerID, recentDate, recentName, recentNumber, recentNumberType, _id]
Name:(111) 111-1111
Num:Mobile
NumType:FREE
CallType:1867
So when I try and pull my data by index I get different values. Why does this happen? Both phones are Nexus 5 and on 4.4.2.
Any insight on this would be great. Thanks!
Columns can "change". For example, consider this case.
Version 1 of an app creates the T table, with columns A, C
In version 2, you want to change the structure of the database. So you add the B column to your "create table" scripts. In new devices, this table will have columns A, B, C.
However, in the onUpgrade() part, you just add column B (via ALTER TABLE), and the structure will end up as A, C, B.
In general it's a bad idea to use fixed ("integer") column indexes, unless you also specify which columns you want (i.e. not using SELECT *). Using getColumnIndex() (outside the loop) is safer.

ORMLite joins queries and Order by

I'm tring to make join in two tables and get all columns in both, I did this:
QueryBuilder<A, Integer> aQb = aDao.queryBuilder();
QueryBuilder<B, Integer> bQb = bDao.queryBuilder();
aQb.join(bQb).prepare();
This equates to:
SELECT 'A'.* FROM A INNER JOIN B WHERE A.id = B.id;
But I want:
SELECT * FROM A INNER JOIN B WHERE A.id = B.id;
Other problem is when taking order by a field of B, like:
aQb.orderBy(B.COLUMN, true);
I get an error saying "no table column B".
When you are using the QueryBuilder, it is expecting to return B objects. They cannot contain all of the fields from A in B. It will not flesh out foreign sub-fields if that is what you mean. That feature has not crossed the lite barrier for ORMLite.
Ordering on join-table is also not supported. You can certainly add the bQb.orderBy(B.COLUMN, true) but I don't think that will do what you want.
You can certainly use raw-queries for this although it is not optimal.
Actually, I managed to do it without writing my whole query as raw query. This way, I didn't need to replace my query builder codes (which is pretty complicated). To achieve that, I followed the following steps:
(Assuming I have two tables, my_table and my_join_table and their daos, I want to order my query on my_table by the column order_column_1 of the my_join_table)
1- Joined two query builders & used QueryBuilder.selectRaw(String... columns) method to include the original table's + the columns I want to use in foreign sort. Example:
QueryBuilder<MyJoinTable, MyJoinPK> myJoinQueryBuilder = myJoinDao.queryBuilder();
QueryBuilder<MyTable, MyPK> myQueryBuilder = myDao.queryBuilder().join(myJoinQueryBuilder).selectRaw("`my_table`.*", "`my_join_table`.`order_column` as `order_column_1`");
2- Included my order by clauses like this:
myQueryBuilder.orderByRaw("`order_column_1` ASC");
3- After setting all the select columns & order by clauses, it's time to prepare the statement:
String statement = myQueryBuilder.prepare().getStatement();
4- Get the table info from the dao:
TableInfo tableInfo = ((BaseDaoImpl) myDao).getTableInfo();
5- Created my custom column-to-object mapper which just ignores the unknown column names. We avoid the mapping error of our custon columns (order_column_1 in this case) by doing this. Example:
RawRowMapper<MyTable> mapper = new UnknownColumnIgnoringGenericRowMapper<>(tableInfo);
6- Query the table for the results:
GenericRawResults<MyTable> results = activityDao.queryRaw(statement, mapper);
7- Finally, convert the generic raw results to list:
List<MyTable> myObjects = new ArrayList<>();
for (MyTable myObject : results) {
myObjects.add(myObject);
}
Here's the custom row mapper I created by modifying (just swallowed the exception) com.j256.ormlite.stmt.RawRowMapperImpl to avoid the unknown column mapping errors. You can copy&paste this into your project:
import com.j256.ormlite.dao.RawRowMapper;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.table.TableInfo;
import java.sql.SQLException;
public class UnknownColumnIgnoringGenericRowMapper<T, ID> implements RawRowMapper<T> {
private final TableInfo<T, ID> tableInfo;
public UnknownColumnIgnoringGenericRowMapper(TableInfo<T, ID> tableInfo) {
this.tableInfo = tableInfo;
}
public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException {
// create our object
T rowObj = tableInfo.createObject();
for (int i = 0; i < columnNames.length; i++) {
// sanity check, prolly will never happen but let's be careful out there
if (i >= resultColumns.length) {
continue;
}
try {
// run through and convert each field
FieldType fieldType = tableInfo.getFieldTypeByColumnName(columnNames[i]);
Object fieldObj = fieldType.convertStringToJavaField(resultColumns[i], i);
// assign it to the row object
fieldType.assignField(rowObj, fieldObj, false, null);
} catch (IllegalArgumentException e) {
// log this or do whatever you want
}
}
return rowObj;
}
}
It's pretty hacky & seems like overkill for this operation but I definitely needed it and this method worked well.

Recursive query with ordered values in SQLite Android

I have one group table with a recursive relation, so each record has a parent_id. Given a group, I need to get all the student (each belong to a group) names in all its subgroups, but ordered by student name.
Do you know if there is any "easy" way to do it? If I have to do multiple queries, then I should order the results of the different Cursors, but Cursor has no orderBy().
Any ideas? Thank you so much!
As SQLite does not support recursive queries I implemented the select with two steps:
First, I have a method called getRecursiveDiningGroupIdsAsString() that retreives all the group ids recursively whose parent id is the one you pass by parameter. The result is a String in the form of: "(2, 3, 4)" so you can later use it in an IN clause. The method looks like:
public String getRecursiveDiningGroupIdsAsString(int depth, long diningGroupId) {
Cursor childDiningGroups = mDatabase.query(
"group",
new String[] {"_id"},
"parent_id = "+diningGroupId,
null, null, null, null
);
String recursiveDiningGroupIds = "";
while (childDiningGroups.moveToNext()) {
long childDiningGroupId = childDiningGroups.getLong(childDiningGroups.getColumnIndex("_id"));
recursiveDiningGroupIds += getRecursiveDiningGroupIdsAsString(depth+1, childDiningGroupId);
}
recursiveDiningGroupIds += diningGroupId;
if (depth > 0) {
recursiveDiningGroupIds += ", ";
} else {
recursiveDiningGroupIds = "("+recursiveDiningGroupIds+")";
}
return recursiveDiningGroupIds;
}
Once I have the group ids I need, I just do a simple query using the ids returned by the previous method and that is it!
Hope it helps!

Android: having basic problems with sqlite database

I am having some trouble implementing a sqlite database in my simple android application:
a user is displayed a list of animals in a Listview.Upon selecting an animal the user is brought to an activity "Animal",which will display a picture of the animal and give them options to
view Animal Bio
Back
All very simple so far, right?
I have working the database, which will populate the listView of animals.Database currently looks like
Table Animal-
_ID,
Name
Table Biography-
_ID,
Bio
This is where I would welcome any helpful advice on my problem, or on how to improve my implementation.
Currently populating the DB as follows
long populateDB(){
String[] animalName = {"Lion" "Zebra", "Tiger", "Gorilla",...};
String[] animalBios = {"Found in the "...}
ContentValues animalNameVals = new ContentValues();
ContentValues animalBioVals = new ContentValues();
long[] rowIds = new long[animalName.length];
// Populate the animal table
for(int i = 0; i < animalName.length; i++){
animalNameVals.put(KEY_ANIMALNAME, animalName[i]);
rowIds[i] = db.insert(ANIMAL_TABLE, null, animalNameVals);
}
// Populate the Bio table
for(int j = 0; j < bios.length; j++){
animalBioVals.put(KEY_BIO, bios[j]);
rowIds[j] = db.insert(BIOS_TABLE, null, animalBioVals);
}
return rowIds[0];
}
And had planned on being able to tell database which animal on list was selected by passing extras with the intent, eg if position on listItemClick == 1, pass in tiger and retrieve tiger bio from db.
Problems:
Then on the Animal activity page is getExtra() == tiger, telling the activity that tiger was selected from the list and to load this bio from the DB..well, I cannot see an efficient method of implementation for this idea and am struggling to do so.
My second headache comes from adding the bio to the application from the Db.Originally I had a test bio hardcoded in a string, shown in a TextView.Is there a way to retrieve a string from a cursor and add it to the TextView id?I understand I will need some adapter, what I do not understand is why cant it be as simple as setResource(R.id.bio) = bio.
Thanks you for reading and any help is much appriciated.
First problem: First of all, I'm not sure why you don't have the column Bio in the Animal-table? As no Bio would fit to any other animal than itself, you can safely do this. By doing this you can query the database upon selection and pass the entire object (including name of animal and bio) to the next Activity and use this to get your information. If this was somewhat unclear, let me know and I'll try to explain it better.
Second problem: You can get values from tables (there of also Strings) using a Cursor. To get the String you can do something like this where cursor is the Cursor with your result from the database:
String bio;
// Move Cursor to its first element
if(cursor.moveToFirst()) {
// Make sure the cursor is not null
if(cursor != null) {
bio = cursor.getString(cursor.getColumnIndex("Bio")));
}
}
Sidenote: If I read the code correctly, it seems that you use long for ID's? The usual thing to go about ID's is integers as far as I know.

Categories

Resources