Address Panel Post 3: Database classes Class (java)
In this post I will show you how to set up the classes you need to save the values in the address panel to a SQLite database.
First you will need to define the schema of the database.
Schema.java
package ewe.custom.controls.database;
public class Schema {
package ewe.custom.controls.database;
public class Schema {
//Database Info
//Database name
public static final String DATABASE_NAME="pvtdb";
//increment this anytime the structure is changed
public static final int DATABASE_VERSION=1;
//address table name
public static final String ADDRESS_TABLE="address";
//address column names
public static final String ADDRESS_ADDR1="ad_addr1";
public static final String ADDRESS_ADDR2="ad_addr2";
public static final String ADDRESS_CITY="ad_city";
public static final String ADDRESS_STATE="ad_state";
public static final String ADDRESS_ZIP="ad_zip";
}
Next add your database helper class. This class will be responsible for creating and updating the database.
DbHelper.java
package ewe.custom.controls.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
//This string creates the table
public static final String CREATE_ADDRESS_TABLE = "create table "
+ Schema.ADDRESS_TABLE + " ( " + Schema.ADDRESS_ADDR1
+ " text not null, " + Schema.ADDRESS_ADDR2 + " text not null, "
+ Schema.ADDRESS_CITY + " text not null, " + Schema.ADDRESS_STATE
+ " text not null, " + Schema.ADDRESS_ZIP + " text not null "
+ ") ;";
//this is the constructor
public DbHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
//called when database is first created
//usually when app is installed or user clears app data
@Override
public void onCreate(SQLiteDatabase arg0) {
arg0.execSQL(CREATE_ADDRESS_TABLE);
}
//called when you update the database version
//look at Schema.java
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
arg0.execSQL("drop table if exists " + Schema.ADDRESS_TABLE);
onCreate(arg0);
}
}
Finally you need to create the class that is going to contain all your database methods. The methods in this class will insert values and load values from the database. The methods are called from RelativeLayout.java file in blog post 2.
MyDb.java
package ewe.custom.controls.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
/**
* @author androidcontrols.blogspot.com
* Database methods
*/
public final class MyDb {
//database object
private SQLiteDatabase db;
private final Context context;
private final DbHelper dbhelper;
private static final String TAG = "db:";
/** Constructor that sets the context
* will create database if it doesn't exist
* @param c
*/
public MyDb(Context c) {
context = c;
dbhelper = new DbHelper(context, Schema.DATABASE_NAME, null,
Schema.DATABASE_VERSION);
}
/**
* Opens database
* @throws SQLiteException
*/
public void open() throws SQLiteException {
try {
db = dbhelper.getWritableDatabase();
} catch (SQLiteException ex) {
Log.v(" Open database exception caught", ex.getMessage());
db = dbhelper.getReadableDatabase();
}
}
/**
* Closes database
*/
public void close() {
db.close();
}
/**
* @author androidcontrols.blogspot.com
* Inserts the address into the table
*/
public long insertAddress(String addr1, String addr2, String city,
String state, String zip) {
try {
ContentValues newAddrRow = new ContentValues();
newAddrRow.put(Schema.ADDRESS_ADDR1, addr1);
newAddrRow.put(Schema.ADDRESS_ADDR2, addr2);
newAddrRow.put(Schema.ADDRESS_CITY, city);
newAddrRow.put(Schema.ADDRESS_STATE, state);
newAddrRow.put(Schema.ADDRESS_ZIP, zip);
return db.insert(Schema.ADDRESS_TABLE, null, newAddrRow);
} catch (SQLiteException ex) {
Log.v(TAG, "Insert address failed: " + ex.getMessage());
return -1;
}
}
/**
* @author androidcontrols.blogspot.com
* @return Bundle containing user address
*/
public Bundle getAddressBundle() {
Cursor c;
Bundle b = new Bundle();
try {
c = db.rawQuery("SELECT " + Schema.ADDRESS_ADDR1 + ", "
+ Schema.ADDRESS_ADDR2 + ", " + Schema.ADDRESS_CITY + ", "
+ Schema.ADDRESS_STATE + ", " + Schema.ADDRESS_ZIP + " "
+ "FROM " + Schema.ADDRESS_TABLE, new String[] {});
if (c.getCount() > 0) {
c.moveToFirst();
b.putString("addr1", c.getString(0));
b.putString("addr2", c.getString(1));
b.putString("city", c.getString(2));
b.putString("state", c.getString(3));
b.putString("zip", c.getString(4));
} else {
// TODO return invoice not found in database (should never
// happen)
return null;
}
return b;
} catch (Exception ex) {
Log.v(TAG, "Failed to get invoice header info: " + ex.getMessage());
return null;
}
}
/**
* @author androidcontrols.blogspot.com
* Deletes the old address then inserts new values
*/
public long saveAddress(String addr1, String addr2, String city,
String state, String zip) {
this.deleteOldAddress();
return this.insertAddress(addr1, addr2, city, state, zip);
}
/**
* @author androidcontrols.blogspot.com
* Delete old address
*/
public long deleteOldAddress() {
try {
return db.delete(Schema.ADDRESS_TABLE, "", new String[] {});
} catch (Exception ex) {
return -1;
}
}
}

