Android携帯電話開発:SQLiteデータベース
1.データの保存と取得を容易にするクラスを定義する
2.上のクラスを使用してオブジェクトを作成
3.格納データ
4.データを読み込み、Cursorを使用
package com.eoeAndroid.SQLite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataDbAdapter {
public static final String KEY_COL = "col";
public static final String KEY_TEL = "telephone";
private static final String TAG = "DataDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE = "create table tlist (col text primary key, "
+ "telephone text not null);";
private static final String DATABASE_NAME = "dataBaseMM";
private static final String DATABASE_TABLE = "tlist";
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS list");
onCreate(db);
}
}
public DataDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public DataDbAdapter open() {
try {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
}catch(SQLException e) {
Log.i("TTTTT", " , !");
}
return this;
}
public void closeclose() {
mDbHelper.close();
}
public long createDiary(String col, String tel) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_COL, col);
initialValues.put(KEY_TEL, tel);
// String strSql = "insert into " + DATABASE_TABLE + " (" + KEY_COL + ", " + KEY_TEL
// + ") values(col, tel);";
// mDb.execSQL(strSql);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteDiary(String col) {
return mDb.delete(DATABASE_TABLE, KEY_COL + "=" + col, null) > 0;
}
public Cursor getAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] { KEY_COL, KEY_TEL
}, null, null, null, null, null);
}
public Cursor getDiary(String col) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] { KEY_COL, KEY_TEL},
KEY_COL + "=" + col, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateDiary(String col, String tel) {
ContentValues args = new ContentValues();
args.put(KEY_TEL, tel);
return mDb.update(DATABASE_TABLE, args, KEY_COL + "=" + col, null) > 0;
}
}
2.上のクラスを使用してオブジェクトを作成
private DataDbAdapter mDbHelper;
mDbHelper = new DataDbAdapter(this);
mDbHelper.open();
open関数データベースの作成またはオープン3.格納データ
//col tel 2
mDbHelper.createDiary(col, tel);
4.データを読み込み、Cursorを使用
private Cursor mDataCursor;
mDataCursor = mDbHelper.getAllNotes();
startManagingCursor(mDataCursor);
private List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
//
items.clear();
Map<String, Object> map = null;
for(int i=0; i<mDataCursor.getCount(); i++){
mDataCursor.moveToPosition(i);
map = new HashMap<String, Object>();
map.put("sellitem_1", mDataCursor.getString(0));
map.put("sellitem_2", mDataCursor.getString(1));
items.add(map);
}