Git Product home page Git Product logo

squeaky-android's Introduction

Squeaky for Android

SQLite is simple and lightweight; it follows that managing SQLite databases on Android should be also.

Squeaky strives to be a straightforward approach to creating, migrating, and accessing SQLite databases.

Setup

Add jitpack.io to your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

Add Squeaky-Android as a dependency to your app's build.gradle:

dependencies {
    compile 'com.github.jasonwyatt:squeaky-android:1.2.0'
}

Getting Started

Define your Tables

import co.jasonwyatt.squeaky.Table

public class TodosTable extends Table {
    @Override
    public String getName() {
        return "todos";
    }

    @Override
    public int getVersion() {
        return 1;
    }

    @Override
    public String[] getCreateTable() {
        return new String[] {
            "CREATE TABLE todos ("+
            "    name TEXT NOT NULL"+
            "    completed INTEGER NOT NULL DEFAULT 0"+
            ")"
        };
    }

    @Override
    public String[] getMigration(int nextVersion) {
        return new String[0];
    }
}

Create your Database and add Tables to it

// in your Application's or Activity's onCreate() method
Database myDB = new Database(this, "todos_db");

myDb.addTable(new TodosTable());
myDb.prepare();

Use it!

myDb.insert("INSERT INTO todos (name) VALUES (?)", "Learn how to use Squeaky");
myDb.insert("INSERT INTO todos (name, completed) VALUES (?, ?)", "Already done", 1);

// query
Cursor c = myDb.query("SELECT rowid, name, completed FROM todos");
while (c.moveToNext()) {
    // get values
    long rowid = c.getLong(0);
    String name = c.getString(1);
    boolean completed = c.getInt(2) == 1;
}

// in an OnClickListener, for example..
myDb.update("UPDATE todos SET completed = 1 WHERE rowid = ?", rowid);

Migrate a Table

Modify your Table definition class:

import co.jasonwyatt.squeaky.Table

public class TodosTable extends Table {
    @Override
    public String getName() {
        return "todos";
    }

    @Override
    public int getVersion() {
        return 2;
    }

    @Override
    public String[] getCreateTable() {
        return new String[] {
            "CREATE TABLE todos ("+
            "    name TEXT NOT NULL"+
            "    completed INTEGER NOT NULL DEFAULT 0"+
            "    due_date INTEGER"+
            ")"
        };
    }

    @Override
    public String[] getMigration(int nextVersion) {
        if (nextVersion == 2) {
            return new String[] {
                "ALTER TABLE todos ADD COLUMN due_date INTEGER"
            };
        }
        return new String[0];
    }
}

That's it. Next time your database is prepared after adding the table definition, the table will be migrated for you!

squeaky-android's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

squeaky-android's Issues

ProGuard/R8 rules

Adding R8 to our application, it seems there's some reflection being done in Squeaky, so the following rule was necessary to retain what was being accessed:

-keep class co.jasonwyatt.squeaky.DatabaseHelper { *; }

It'd be a handy improvement if rules could be bundled with the library.

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.