Skip to content

Latest commit

 

History

History
115 lines (91 loc) · 2.64 KB

Cookbook.md

File metadata and controls

115 lines (91 loc) · 2.64 KB

Cookbook

Common tasks snippets

Create database

Create using the default name
     DB snappydb = DBFactory.open(context);
Create with a given name
     DB snappydb = DBFactory.open(context, "books");

SnappyDB use the internal storage to create your database. It create a directory containing all the necessary files Ex: /data/data/com.snappydb/files/mydatabse

Open database

Open using the default name
     DB snappydb = DBFactory.open(context);
Open using a given name
     DB snappydb = DBFactory.open(context, "books");

Close database

     snappydb.close();

Destroy database

     snappydb.destroy();

Insert primitive types

     snappyDB.put("quote", "bazinga!");
     
     snappyDB.putShort("myshort", (short)32768);
     
     snappyDB.putInt("max_int", Integer.MAX_VALUE);
     
     snappyDB.putLong("max_long", Long.MAX_VALUE);
     
     snappyDB.putDouble("max_double", Double.MAX_VALUE);
     
     snappyDB.putFloat("myfloat", 10.30f);
     
     snappyDB.putBoolean("myboolean", true);

Read primitive types

     String quote      = snappyDB.get("quote");
     
     short myshort     = snappyDB.getShort("myshort");
     
     int maxInt        = snappyDB.getInt("max_int");
     
     long maxLong      = snappyDB.getLong("max_long");
     
     double maxDouble  = snappyDB.getDouble("max_double");
     
     float myFloat     = snappyDB.getFloat("myfloat");
     
     boolean myBoolean = snappyDB.getBoolean("myboolean");

Insert Serializable

     AtomicInteger objAtomicInt = new AtomicInteger (42);
     snappyDB.put("atomic integer", objAtomicInt);

Read Serializable

     AtomicInteger myObject = snappyDB.get("atomic integer", AtomicInteger.class);

Insert Array

     Number[] array = {new AtomicInteger (42), new BigDecimal("10E8"), Double.valueOf(Math.PI)};
     
     snappyDB.put("array", array);

Read Array

     Number [] numbers = snappyDB.getArray("array", Number.class);

Check Key

     boolean isKeyExists = snappyDB.exists("key");

Delete Key

     snappyDB.del("key");