Sample Database for Meter Logger Example

จาก Paween Khoenkaw

SQL command for the creation of example database.

CREATE TABLE waterdata (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , meter_id NUMERIC NOT NULL , meter_value NUMERIC, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);


The result database is look like this

Water meter database.png


Download SQLiteMan

The complete dbhelper class

public class dbhelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "water.db";

private static final int DATABASE_VERSION = 1;


public dbhelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

// if database was not exist (first run) it will go here!

@Override

public void onCreate(SQLiteDatabase arg0) {

// created blank table

String sql = "CREATE TABLE waterdata (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , meter_id NUMERIC NOT NULL , meter_value NUMERIC, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);";

arg0.execSQL(sql);

}


// if older version database was already exist it will go here!

@Override

public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

// you place sql for alter table here!

}


}