Giter VIP home page Giter VIP logo

androidb's Introduction

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

androidb's People

Contributors

mattelacchiato avatar

Stargazers

fg607 avatar Erastus  Thompson avatar Serhii Nadolynskyi avatar Stas Persiyanenko avatar Mohit Vijayvargia avatar Torben Schwellnus avatar Michael Wagner avatar Matthias Nitschke avatar Peter Siegmund avatar top secret avatar  avatar

Watchers

 avatar James Cloos avatar Lukáš Šálek avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.