Skip to content

mattelacchiato/androiDB

Repository files navigation

AndroiDB

A Lightweight Database Library for Android.

Installation

Fetch the code and import it as Eclipse project. Than include it in your own Android project (Project Properties -> Java Build Path -> Projects -> Add…). A user-friendly jar in two versions (dex compiled and sun-java compatible) is planned for the first release.

Usage

Define Table Structure

Just extend your class from Table and set a DB version on it:

@TableMetaData(version = 1)
public class Category extends Table {

	@Column
	private String name;

	@Column(notNull = true)
	private int budget;

	public Category(final Context context) {
		super(context);
	}
	
	//and some getters&setters
}

With the @Column-Annotation you define a column in the DB. You don’t need to define a Primary Key. This is alreade done by Table’s “_id” Column.

Enjoy

Now, you can call all CRUD-Method on Category:

Table cat = new Category(getContext());
cat.setBudget(42);
cat.save(); //"update or insert"

Table cat = new Category(getContext());
cat.find(42);
cat.setName("foo");
cat.save();

cat.delete();

Extend

Of course, you can extend your Table implementation with your specialized CRUD-Methods:

public boolean findByName(final String name) {
	Cursor c = db.query(getTableName(), getColumnNames(), "tableName='" + name + "'", null, null, null, null);
	return fillFirst(c);
}

The super class Table provides all methods you need, like getTableName(), getColumnNames(), fillFirst©…

Versioning

When creating a table instance, this table will immidiatly created in the DB (when it’s not existing, yet). Afterwards, we will look in our Metadata-table to compare the current table version with the new created one. When they differ, onUpgrade(int fromVersion, int toVersion) will be called. The default behaviour is to drop the current table, but you can simply override the method to provide your own upgrade handling.

Columns

The following types are currently supported:

notNull (false)

Flag for “NOT NULL”-constraint

autoIncrement (false)

Flag for “AUTOINCREMENT”-constraint

primaryKey (false)

Flag for “PRIMARY KEY”-constraint. Though the Table-class holds the primary key for each table, you won’t use this.

viewId (-1)

int value for an Android ressource to bind with this column. It’s not interpreted anywhere in androiDB, but you can use this value for your own (probably in your CursorAdapter).

indexNames ({})

Array of index names, which should include this column. Examples:

  • You want to create an index on your columns “firstname” and “lastname”. Just add to each column

@Column ( indexNames = {"idx_name"}) firstname @Column ( indexNames = {"idx_name"}) lastname

  • You want to speed up single searches for firstname, too:

@Column ( indexNames = {"idx_name", "idx_firstname"}) firstname @Column ( indexNames = {"idx_name"}) lastname

Caution

Table and Column Naming

Don’t use SQL reserved words as column names! I could quote all column names, but SQLite don’t behave like expected:

sqlite> CREATE TABLE IF NOT EXISTS Metadata ( '_id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'table' TEXT NOT NULL);
sqlite> insert into metadata ('table') values ('fjdksla');
sqlite> select * from metadata;
1|fjdksla
sqlite> select table from metadata;
Error: near "table": syntax error
sqlite> select 'table' from metadata;
table

Cursor and DB instances

You have to take care of your db and cursor instances. When you don’t need them anymore, you have to call explicitly close()!

Subprojects

Information

At www.blaulabs.de/2010/09/forschungswoche-androidb/ you can find a german blogpost about this project.

A presentation can be found at prezi.com/24kxq2purtmf/androidb/

tags

Android, db, database, sql, sqlite, orm, or-mapper, hibernate, framework, libary, androiddb

Releases

No releases published

Packages

No packages published

Languages