public class DatabaseUtil {
private static final String TAG = "DatabaseUtil";
/**
* Database Name
*/
private static final String DATABASE_NAME = "sales_database";
/**
* Database Version
*/
private static final int DATABASE_VERSION = 1;
/**
* Table Name
*/
private static final String DATABASE_TABLE = "SalesTb";
/**
* Table columns
*/
public static final String ROWID = "_id";
public static final String SNAME = "salesman_name";
public static final String CNAME = "customer_name";
public static final String CPHONENO = "phone_numebr";
public static final String CARMODEL = "car_model";
/**
* Database creation sql statement
*/
private static final String CREATE_SALES_TABLE = "create table "
+ DATABASE_TABLE + " (" + ROWID
+ " integer primary key autoincrement, " + SNAME
+ " text not null, " + CNAME + " text not null, " + CPHONENO
+ " text not null, " + CARMODEL + " text not null);";
/**
* Context
*/
private final Context mCtx;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
/**
* Inner private class. Database Helper class for creating and updating
* database.
*/
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/**
* onCreate method is called for the 1st time when database doesn't
* exists.
*/
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, "Creating DataBase: " + CREATE_SALES_TABLE);
db.execSQL(CREATE_SALES_TABLE);
}
/**
* onUpgrade method is called when database version changes.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
/**
* Constructor - takes the context to allow the database to be
* opened/created
*
* @param ctx
* the Context within which to work
*/
public DatabaseUtil(Context ctx) {
this.mCtx = ctx;
}
/**
* This method is used for creating/opening connection
*
* @return instance of DatabaseUtil
* @throws SQLException
*/
public DatabaseUtil open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
/**
* This method is used for closing the connection.
*/
public void close() {
mDbHelper.close();
}
/**
* This method is used to create/insert new sales record .
*/
public long createsales(ContentValues initialValues) {
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
/**
* This method will delete sales record.
*
* @param rowId
* @return boolean
*/
public boolean deletesales(long rowId) {
return mDb.delete(DATABASE_TABLE, ROWID + "=" + rowId, null) > 0;
}
/**
* This method will return Cursor holding all the Sales records.
*
* @return Cursor
*/
public Cursor fetchAllsales() {
return mDb.query(DATABASE_TABLE, new String[] { ROWID, SNAME, CNAME,
CPHONENO, CARMODEL }, null, null, null, null, null);
}
/**
* This method will return Cursor holding the specific Sales person record.
* @return Cursor
*/
public Cursor fetchsales(String name) throws SQLException {
Cursor mCursor = mDb.query(DATABASE_TABLE, new String[] { ROWID,
SNAME, CNAME, CPHONENO, CARMODEL },SNAME +"=? ", new String[]{name},
null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}