OR Mapper ORMLiteを使ってみる -使用例-

import java.sql.SQLException;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.Dao;


public class HelloAndroidActivity extends OrmLiteBaseActivity<DatabaseHelper> {

	private final String LOG_TAG = getClass().getSimpleName();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(LOG_TAG, "creating " + getClass());
		TextView tv = new TextView(this);
		doSampleDatabaseStuff("onCreate", tv);
		setContentView(tv);
	}

	private void doSampleDatabaseStuff(String action, TextView tv) {
		// 永続化のサンプル
		try {
			Dao<Account, Integer> dao = getHelper().getDao(Account.class);
			dao.create(new Account("name1", "pass1"));
			dao.create(new Account("name2", "pass2"));
		} catch (SQLException e) {
			Log.e(LOG_TAG, "Database exception", e);
			tv.setText("Database exeption: " + e.getMessage());
			return;
		}
		
		// 取得のサンプル
		try {
			Dao<Account, Integer> dao = getHelper().getDao(Account.class);
			List<Account> accounts = dao.queryForAll();
			
			StringBuilder sb = new StringBuilder();
			for (Account account : accounts) {
				sb.append(account);
			}
			tv.setText(sb.toString());
		} catch (SQLException e) {
			Log.e(LOG_TAG, "Database exception", e);
			tv.setText("Database exeption: " + e.getMessage());
			return;
		}
		
	}

}