DBHelperクラスは、Respsitoryパターンに従います。基礎となるデータへのアクセスが「リポジトリ」クラスに含まれている場合。これにより、メインロジックが解放され、ビジネスロジックに集中できるようになり、すべてのデータ処理の責任がリポジトリにプッシュされます。アプリケーションの複雑さに応じて、リポジトリは「ファサード」として機能する場合があります。このファサードでは、呼び出しがドメインオブジェクトを担当するリポジトリに単純にプロキシされます。
以下のコードは、DBHelperの非常に単純なテンプレートを作成します
public class DBHelper
{
Context context;
private SQLiteDatabase db;
private final String DB_NAME = "MYDataBase";
private final int DB_VERSION = 1;
private final String TABLE_NAME = "Animal";
private final String TABLE_ROW_ID = "id";
private final String TABLE_ROW_ONE = "animal_name";
private final String TABLE_ROW_TWO = "animal_bio";
public DBHelper(Context context)
{
this.context = context;
CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
this.db = helper.getWritableDatabase();
}
public static byte[] getBitmapAsByteArray(Bitmap bitmap)
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
public void addRow(String name, String bio)
{
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, name);
values.put(TABLE_ROW_TWO, bio);
try
{
db.insert(TABLE_NAME, null, values);
Log.w("database message","Insert successfully");
}
catch(Exception e)
{
Log.e("DB ERROR", e.toString());
//e.printStackTrace();
}
}
public void deleteRow(long rowID)
{
try
{
db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowID, null);
Log.w("database message","delete successfully");
}
catch (Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void updateRow(long rowID,String name,String bio)
{
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, name);
values.put(TABLE_ROW_TWO, bio);
try
{
db.update(TABLE_NAME, values, TABLE_ROW_ID + "=" + rowID, null);
Log.w("database message","Update successfully");
}
catch (Exception e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}
public int count_row()
{
int row=0;
Cursor cursor;
try
{
cursor = db.query(
TABLE_NAME,
new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO},
null, null, null, null, null
);
cursor.moveToFirst();
if (!cursor.isAfterLast())
{
do
{
row++;
}
while (cursor.moveToNext());
}
}
catch (SQLException e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
Log.w("row count..",""+row);
return row;
}
public void getRow(long rowID)
{
Cursor cursor;
try
{
cursor = db.query
(
TABLE_NAME,
new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO},
TABLE_ROW_ID + "=" + rowID,
null, null, null, null, null
);
cursor.moveToFirst();
if (!cursor.isAfterLast())
{
do
{
Log.w("row ",""+cursor.getLong(0));
Log.w("row ",""+cursor.getString(1));
Log.w("row ",""+cursor.getFloat(2));
}
while (cursor.moveToNext());
}
cursor.close();
}
catch (SQLException e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void getAllRows()
{
int i=0;
Cursor cursor;
try
{
cursor = db.query(
TABLE_NAME,
new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO},
null, null, null, null, null
);
cursor.moveToFirst();
if (!cursor.isAfterLast())
{
do
{
Log.w("row "+i,""+cursor.getLong(0));
Log.w("row "+i,""+cursor.getString(1));
Log.w("row "+i,""+cursor.getFloat(2));
i++;
}
while (cursor.moveToNext());
}
}
catch (SQLException e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}
private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{
public CustomSQLiteOpenHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
String newTableQueryString = "create table " +
TABLE_NAME +
" (" +
TABLE_ROW_ID + " integer primary key autoincrement not null," +
TABLE_ROW_ONE + " text," +
TABLE_ROW_TWO + " text" +
");";
db.execSQL(newTableQueryString);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
}
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DBHelper database = new DBHelper(this);
database.addRow("name1","bio1");
database.addRow("name2","bio2");
database.addRow("name3","bio3");
database.addRow("name4","bio4");
database.addRow("name5","bio5");
database.getAllRows();
database.updateRow(2,"name20","bio20");
database.getAllRows();
database.deleteRow(2);
database.getAllRows();
}
詳細
http://mel-tools.mit.edu/code/SimpleContentProvider/doc/edu/mit/mobile/android/content/class-use/DBHelper.html
アプリケーションのDBHelperクラスをコピーしてください。お役に立てば幸いです。