Office Hours — Today, June 30

Friday, June 27

Mark M.
has entered the room
Jun 30
3:55 PM
Mark M.
turned on guest access
4:00 PM
Gabriel C.
has entered the room
Mark M.
hello, Gabriel
how can I help you today"
er, today?
Gabriel C.
Hello Mark
Well i've just begun to program, so i am very new to all this
so i just wanted to make sure you knew that. Albeit im new, i still have a specific objective
So right now my problem is how i would go about storing data on my app
I've looked at all the available databases on the Android Developers website but I still can't seem to pin point the right one
Mark M.
I am not quite certain what you mean by "all the available databases on the Android Developers" Web site
usually, if an app is going to use a database, it creates its own
Gabriel C.
I mean i looked at Shared Preferences, SQLite
4:05 PM
Mark M.
ah, OK
Gabriel C.
so If i had multiple Strings, like say a thousand, and each of these Strings held about a paragraph of words
and I wanted to create a kind of retrieval system in my code where the user would answer a few questions and based on their choices they would get one of those specific strings
Which database or data storage system should i use?
Mark M.
off the cuff, that feels like SQLite
it definitely is not SharedPreferences
you could create your own files outside of a database if you prefer
Gabriel C.
Would they still be imported onto the app and the user wouldn't have to have internet to access them?
Mark M.
that's a separate issue
where are these strings coming from?
(along with the other data associated with them for your retrieval system)
Gabriel C.
I'll be writing all of them pretty much
Mark M.
yes, but you don't live on everyone's phones :-)
(at least, I hope not)
are you saying that you are intending to ship the strings with the app?
Gabriel C.
touche. Yeah, i was hoping that was a thing
Mark M.
well, there are various ways of shipping data with an app
and various ways of retrieving data within an app
4:10 PM
Mark M.
and they are not necessarily closely tied together
Gabriel C.
that works. Well basically what I wanted to make was a quote generator type thing
Mark M.
for example, you could package the strings in a SQLite database on your development machine
then package that database with the app, using SQLiteAssetHelper
I have a section on SQLiteAssetHelper in the book
Gabriel C.
Sweet, thank you. I'll study that.
So for the database though, doesn't it have to be structured a specific way in order for it to be retrieved properly
Mark M.
well, SQLite is a relational database
rows and columns
Gabriel C.
That could work
Mark M.
my assumption is that you would have various columns that would tie into your decision-making criteria for which strings are relevant (e.g., category)
in addition to a column for the string itself
Gabriel C.
yeah!
Mark M.
SQLite also supports full-text searching, if you wanted to do keyword searches within the strings
the APK edition of my book, for example, uses SQLite full-text search for its search support
4:15 PM
Gabriel C.
thats dope. So for the retrieval system/set up in the code if i wanted them to chose a topic (actor/actress) and then chose some attributes of the quote (funny, inspiring, spiritual, heartfelt, etc) would i make each one of those a column in SQLite to be retrieved whenever they ask for that type?
Mark M.
that sounds about right
you'd have a topic column and a type column, to associate the values for those attributes with the quote
then run a query to retrieve all quotes matching a user
er, a user's chosen topic and type
Gabriel C.
can I allow them multiple types?
Mark M.
yes, though it makes your database structure more complicated
Gabriel C.
or maybe a hierarchy of types (pick 3, the first is most important)
Mark M.
oh, I see
yes, that too is possible
Gabriel C.
just difficult?
Mark M.
it will probably require some fancy footwork in the SQL or else some post-processing of the query results in Java
4:20 PM
Gabriel C.
So would it make it simpler to have all the Strings in the SQL, have them choose the topic to retrieve that set (war-quotes) and then have the cursor kind of sift through to match the specific attributes of what they chose to look for?
Mark M.
more generally, have the SQL query return all rows that could be candidates, and perhaps take advantage of ORDER BY to put them in a deterministic order, then finesse the results further in Java
it may be that what you wind up doing is only getting the actual string when you're done
so you make one query to get attributes of candidates, sift through them in Java to find the One True Quote, then query the database to get the text of the quote
Gabriel C.
That sounds right. could i run you through like table of what i wanted the user to go through and then could you just confirm what you just said was the best way and then point me in the right direction to keep studying and working?
Mark M.
I can try
Gabriel C.
sweet
So in the options menu they would have already selected like 3 topics they wanted to come up (actor/actress, spiritual, war-quotes)
4:25 PM
Gabriel C.
after that they would have a list of attributes they want their quote to convey (funny, nice, heart-felt, and all they). They could choose as many as they want but it would try and find the one with the most attributes that they chose (don't have to use all of them)
then after that it would search the database and pull one quote for each topic and display it on the screen
Mark M.
by "selected like 3 topics", is "actor/actress" a "topic", or is the name of an actor/actress a "topic"?
Gabriel C.
actor/actress is the topic
like the general set, im thinking that would be one of the ways to group all the quotes said by actors and actresses in the SQL
Mark M.
is a quote tied to just one topic? or can a quote have multiple topics?
Gabriel C.
One topic i believe.
but it can have the same attributes all across the borad
Mark M.
so, a topic has a 1:N relationship to a quote (one topic has N quotes, one quote has one topic)
Gabriel C.
Yes
Mark M.
and attributes have an M:N relationship to quotes (an attribute has M quotes, a quote has N attributes)
Gabriel C.
Yes
4:30 PM
Mark M.
a classic "normalized" data model for this would have three database tables
one table would be for the quotes
one table would be for the attributes
one table would be for the "join data" tying quotes to attributes
you could argue that there would be a fourth table, for the topics, but if there are no other properties of a topic other than its name, you could skip this
the quotes table would have some sort of unique identifier (such as the SQLite ROWID), the quote itself, the topic for that quote, and any other metadata you want for quotes
the attributes table would have some sort of unique identifier (such as the SQLite ROWID, or the attribute name itself), plus any other metadata you want for attributes
the quotes-attributes join table would have rows tying a quote ID to an attribute ID
you would query the quotes and attributes data to retrieve all the quotes in the selected topics and having at least one of the chosen attributes
and you'd sift through the results in Java
there may be ways of offloading more of the analysis to SQL, but that might get tricky
Gabriel C.
This sounds great
Mark M.
so that you don't load all of the strings (memory-intensive), your initial queries would get the quote IDs, the attributes for each quote, and any other metadata you need
once you chose the quote, you'd go back to the database to retrieve the string
bear in mind that this is a "back of a napkin" analysis of your data model
and so your kilometerage may vary
4:35 PM
Gabriel C.
Its already leagues ahead of where I was.
Mark M.
my book does not go into SQL itself, as there are plenty of educational materials out there for that
Gabriel C.
If need be, could i create another table in the normalized data structure?
Mark M.
in principle, you can do whatever you want
the important part is to come up with the nature of the relationships between different aspects of your data
(the 1:N and M:N stuff from before)
relational databases are then a matter of defining the right tables to represent the data model
Gabriel C.
sweet. So besides SQLite and every keyword and term you placed here, Is there anything else i should study and look out for to create this?
Mark M.
you'll need to find your source of quotes
from a programming standpoint, there are plenty of other things you will wind up worrying about (threads, how to represent this in a UI, etc.)
but a lot of that will be stuff relatively in common with other apps
Gabriel C.
So the "sift through results in Java" you spoke about earlier. Does that involve lot of thread work- like to the SQL and back?
4:40 PM
Mark M.
your database queries themselves need to be on a background thread, as disk I/O is slow
since that work is on a background thread, you may as well do the rest of the analysis on the same background thread, until you come up with the text of the One True Quote
then, the main application thread can display that quote (TextView, WebView, etc.)
Gabriel C.
Sounds like I have a lot more to study, but this has been great man
Thank you, very much
Mark M.
you are very welcome
Gabriel C.
Have a great day. And If i wanted to do a private session, is there a way to ask for you personally as you already know most of it?
Mark M.
well, CommonsWare is a one-person firm
so, the chats are all me
Gabriel C.
Oh shit no way. Well thats cool
Mark M.
now, if you mean you want something that is not open to others, that's possible
Gabriel C.
sweet. Ill be back sometime soon mark. thanks again!
Mark M.
note that the chat transcript will be posted to http://commonsware.com/office-hours/ shortly after the chat period ends
Gabriel C.
Thats cool
Gabriel C.
has left the room
4:55 PM
Mark M.
turned off guest access

Friday, June 27

 

Office Hours

People in this transcript

  • Gabriel Cavaliere
  • Mark Murphy