I´m a bit confused as to why there is no widely proposed model of storing your SQLite database structure in Android. Surely it would be nothing but advantageous to keep your column and table names somewhere central for reference, keeping you from going through your entire project when the database changes in structure. How are you handling this? I was thinking of creating a seperate class file called DatabaseConstants with the entire structure in static strings, but I would like to know if there is a better way?
Thanks for your time.
You can look at the ContactsContract for example to see the column names for Android Contacts.
Related
I'm learning Android by forking and modifying an open source app, FrontlineSMS for Android. Here's my copy!
I want to add a few tables, Poll and PollResponse, to the database, where Poll->PR is a one-to-many relationship.
I've added classes for Poll to the project. What is the "right" way to define the PollResponse object?
Saving the Poll data happens on line 562 of Keyword.java, and somehow in there a row is added to the database with a new ID created for that row. This is the place where I also want to save a few rows into PollResponse (which doesn't exist yet) and assign their id_poll field the value from the newly created Poll row. Is there a clean way to accomplish this child relationship, as some part of src/net/frontlinesms/android/model/Poll.java or /src/net/frontlinesms/android/model/PollDao.java? Or do I need to create completely separate PollResponse objects, save the data for the Poll, then query the database for the newly created ID and save that in PollResponses? The latter is the only way I can think to do it but it seems ugly. I also tried searching around for solutions to this but I may not have the right keywords in mind.
I discovered how to create a new table Poll by creating Poll.java and PollDao.java and adding the class to /src/net/frontlinesms/android/db/FrontlineSmsSqliteHelper.java , but I'm largely fumbling around this project, reading Android development guides and trial and error. Sorry for the lack of links, I'm only allowed 2 until I get more than 10 reputation. Thanks for your help!
*EDIT - Could there be a case for rewriting this project to use an ORM like greenDAO? Again I'm still learning Android so I'm not sure if the project uses any packaged ORM. It seems like it's custom written.
I am planning on creating an android application sometime in the future in which I'll want it to display a lot of constant data on the screen.
I'm not sure the best way to do this but I see two options:
Storing the data within the code itself such as creating a constants class.
Using an embedded database to hold the data.
I'm guessing option #2 is the best way? But it just seems weird using a database if I'm not going to be doing any updating to the database, I would only be selecting.
The total amount of data that I need the application to display is maybe about 400 lines consisting of a string and two integers...
Is there a different way people use for such a situation that I don't know about?
But it just seems weird using a database if I'm not going to be doing
any updating to the database
I am totally disagree with you. Database is not only for updating. It can be used as a better storage and definitely a best way for searching. So as you want to preserve the data then it is definitely wise to use database.
But if you want to handle data which will not persists , i,e you will use different datas for different run then you can use temporary class or other data structure to store data.
Finally, If you are planning to have portability then File storage is an easier solution.
SO you can see, that it totally depends on what you want.
I want to build a fairly simple Android application.
The basic data model of my app consist on several basic elements with relationships between them.
I want the data to be persistant so I'm thinking about using an sqlite DB.
I looked at the Android Developer website, and the closest thing that I could find that is relevant to my question is the "NotePad Tutorial" which make use of an sqlite DB to store notes.
I think by now I got the handle on the basics, but I still have some questions though:
In the tutorial they have only one table in the DB. If my application requires a more complicated scheme - should I still use the same method? that is - putting everything inside a subclass of SQLiteOpenHelper? Or is there a more "structured" way to go?
Should I bother creating classes for all the data elements stored in my DB? I mean this is what I learned that I should do, but in the documentation there is no hint about that.
If I Should create classes - How should I use them correctly? I mean, since the result of the query is a Cursor object, and not a collection of rows, Should/Can I parse that into objects?
Thanks!
Define all the tables in the same subclass, this makes easy to see all the table at one place and possibly write SQL for upgade etc
Yes, that would be easier for manipulation in the java side and makes code clean
Read from the cursor and initialize an arraylist of objects one by one.
I am trying to create an application in java which pulls out records from the database and maps it to objects. It does that without knowing what the schema of the database looks like. All i want to do is fetch all rows from all tables and store them somewhere. There could be a thousand tables with thousands of records each. The application doesn't know the name of any table or attribute. It should map "on the fly". I looked at hibernate but it doesnt give me what i want for this app. I don't want to create hard-coded xml files and classes for mapping. Any ideas how i can accomplish this ?
Thanks
Oracle has a bunch of data dictionary views for metadata.
ALL_TABLES, ALL_TAB_COLUMNS would be first places to start. Then you'd build ad-hoc queries based on what you get out of there. Not sure whether you have to deal with all data types (dates, blobs, spatial, user-defined....).
Not sure what you mean by "store them somewhere". If you start thinking CSV or XML files, you'll need to escape various characters from VARCHAR2 columns.
If you are looking for some generic extract/unload routines, you should look at what is already available in the database or open-source/commercially.
MyBatis provides a pretty simple way to map data results to objects and back, maybe check that out?
http://code.google.com/p/mybatis/
Not to be flip, but for this task, you might want to check out Ruby on Rails and its ActiveRecord approach
For a university assignment I have been assigned I have a Prize object which contains either text, image, or video content. I would like to persist this information into a BLOB field within an Apache Derby database (which will be running on a low powered PDA). How can I add this data to the database?
Thanks in advance.
In this article Five Steps to Managing Unstructured Data with Derby
you can read how to do this.
It describes how to insert binary data into a column with the BLOB datatype in Apache Derby using JDBC.
I assume you'll be connecting via JDBC. If so, simply write your SQL and look at the setBlob method of a PreparedStatement. Should be pretty straightforward.
Serialization is the easy way to do it, however if possible you could make it look like a real database table with a structure containing id (bigint), datatype (smallint), creationdate (date) and data (blob) and specifically make the client code to save the object's data there. This way you could do searches like "get all video prizes created between January 1st 2008 and January 15th 2009" and it wouldn't break down old data if your class would change too much for the serialization to stop working.
This sort of solution would be easy to extend in the future too if there would be need for it; I understand this is a school assignment and such need most likely won't ever surface but if your teacher/professor knows his stuff, I bet he's willing to give an extra point or two for doing this excercise in this way since it takes a bit more time and shows that you can take the steps to prepare in advance for coping in the everchanging landscape of software development.
If you are using Netbeans (I assume Eclipse has similar functionality) you can setup your database schema and the create new Java entity classes from the database and it will generate the appropriate JPA classes for you.
http://hendrosteven.wordpress.com/2008/03/06/simple-jpa-application-with-netbeans/
This is nice as it allows you to focus on your code rather than the database glue code.
The best solution , is to use Derby, because it keep being a multi platform app developed via Java.