Code Monkey home page Code Monkey logo

dachshund's Introduction

Dachshund

A ligt-weight framework of Android Sqlite DB, which manages automatic DB tables creation and upgrade. On Android development with SQLite, a lot of us have to manually generate SQL strings in onCreate() and onUpgrade(). With this simple framework, just annoate your class fields as database columns, you get your tables created or upgraded in time.

How to use? Step 1. Override BaseDbTable:

@DbTable(tableName = "MyDbTable1", minVersion = 2)
public class MyDbTable extends BaseDbTable{
    @DbField(columnType = DbType.TEXT, defaultValue = "some_value")
    private static String column_username = "userName";
    
    @DbField(columnType = DbType.INTEGER, defaultValue = "0", minVersion = 3)
    private static String column_userage = "userAge";

    @Override
    public void onTableCreated(SQLiteDatabase db){
        // make initialization after your table is right created
        // normally you can insert initial rows into table
    }
    
    @Override
    public void onTableUpgraded(SQLiteDatabase db, int oldVer, int newVer){
        // callback when your db is upgraded from oldVer to newVer
    }
    
    // implement your additional logic below
    public void addRandom(){ insert(...); ... }
}

As annotated, the table was added in DB version 2 (minVersion = 2), the column "username" was added in ver 2 also as it derives the value from table; while column "userage" was a new column added in version 3.

Step 2. Init DbManager:

// here we specify dbVersion is 3, so if your current db versoin < 3, Dachshund will upgrade it by adding 'userAge' into table MyDbTable
DbManager dbManager = DbManager.init(getContext().getApplicationContext(), 
    new Class<BaseDbTable>[]{MyDbTable.class}, 
    "myDataBase", 3);

Step 3. Use your table:

MyDbTable dbTable = (MyDbTable)dbManager.getTable(MyDbTable.class);
dbTable.addRandom(); // while trigger db creation or upgrade here

Step 4. THAT'S ALL!

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.