Android-ActiveRecord: ActiveRecord Android Style
Storing and retrieving data is a common problem. Fortunately, Android comes with a built-in SQLite database…somebody say ..”holy cow batman- kaboom!”
Active record (AR) is a design pattern for Object-Relational modelling (ORM) conceived by Martin Fowler. It gained popularity due in part to the rise of the Ruby on Rails framework with the inclusion of an aptly name gem called …mmh activerecord no surprise.
AR is usually thought of as the M…in MVC (Model-View-Controller) pattern handling the CRUD (Create, Read, Update, Delete) interactions with the back-end database. ..(ok enough acronyms)
Simple usage pattern:
- Initialize the database and its connection
- Create a class extending an ActiveRecordBase
- Set your attributes on the object
- Save the attributes as a record in the database
Android ActiveRecord: http://code.google.com/p/android-active-record/
Getting the source code from SVN (require Subversion (svn) installation)
Copy-n-Paste and hit Enter:
svn checkout http://android-active-record.googlecode.com/svn/trunk/ android-active-record-read-only
Now for some details:
NOTE: All attributes must be public for 2 reasons:
- allows direct access to the attribute/field
- most importantly for Java Reflection purposes to occur smoothly
1. Initialize the database and its connection
private void initDatabase() throws ActiveRecordException { Log.d(tag, "initDatabase()"); this.ctx = getApplicationContext(); this.builder = new DatabaseBuilder(dbName); // Setup the builder Database.setBuilder(this.builder); Log.d(tag, "opening ActiveRecordBase"); this.conn = ActiveRecordBase.open(ctx, dbName, dbVersion); }
2. Create a class extending ActiveRecordBase
import java.sql.Timestamp; import org.kroz.activerecord.ActiveRecordBase; import org.kroz.activerecord.Database; public class Note extends ActiveRecordBase { // public for AR-access public String text; public Timestamp date; public Note() { // EMPTY } public Note(Database db) { super(db); } @Override public String toString() { return "Date: " + this.date.toString() + "\n" + "Note: " + this.text; } }
3 & 4. Set the attributes on the created object and call the save() method on the object
// Create a Note entry by requesting it from the database connector Note ent = conn.newEntity(Note.class); // create a java calendar instance Calendar calendar = Calendar.getInstance(); Date now = calendar.getTime(); // Set the attributes ent.date = new java.sql.Timestamp( now ); // Set the date ent.text = "Fred Flinstone"; // Set the text ent.save(); // Save the object to the database
Update:
Will be starting a NEW SERIES on using Android-ActiveRecord!
NEXT – https://w2davids.wordpress.com/activerecord-android-intro/
Hey dude! I’ve found your initial code awesome. Thanks for that. I will soon try an implementation of your active record. I let you know the result. And I expect to contribute also. Just let me know how it can be done (I’m very used to github’s way of doing it).
Keep moving!
best regards,
otávio
have you seen the autocompletion ..i have one that works like google search…using activerecord android at the back-end
http://na.isobar.com/2010/android-autocompletion-with-icons/
Hi,
I’ve been using this and it’s worked very well. Thank you so much.
I just have one question. Is there a notion of migrations, or do you just completely replace the database with each schema change?
where in the do you put this code?
in what files (i am using calendarimp version)? please can somone help
2 Pingbacks