import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private final static String DB_NAME="testDB.db"; private final static String DB_TABLE="teatTable"; private final static int DB_VERSION=1; private SQLiteDatabase databaseObject; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText=(EditText)findViewById(R.id.editText); DatabaseHelper dbHelperObject = new DatabaseHelper(MainActivity.this); databaseObject = dbHelperObject.getWritableDatabase(); //保存ボタン findViewById(R.id.button) .setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { try{ //データベースへの書き込み String writeString = editText.getText().toString(); writeToDB(writeString); }catch(Exception e){ //書き込み失敗時にメッセージ showDialog( getApplicationContext(), "ERROR", "データベースの書き込みに失敗しました" ); } } }); findViewById(R.id.button2) .setOnClickListener( new View.OnClickListener(){ @Override public void onClick(View view){ try{ //データベースの読み込み String readString =readToDB(); editText.setText(readString); }catch(Exception e){ showDialog( getApplicationContext(), "ERROR", "データの読み込みに失敗しました"); } } }); } /*************************************** * */ private void writeToDB(String editedString)throws Exception{ ContentValues contentValObject =new ContentValues(); contentValObject.put("id","0"); contentValObject.put("info",editedString); int numberOfColumns = databaseObject.update( DB_TABLE, contentValObject, null, null ); } /**************************************** * */ private String readToDB()throws Exception{ //データベースからテーブルを読み込む Cursor cursor=databaseObject.query( DB_TABLE, new String[]{"id","info"}, "id='0'", null, null, null, null ); //cursor内のレコード数が0の場合は例外処理を行うインスタンスを生成 if (cursor.getCount() == 0){ throw new Exception(); } //カーソルの位置を先頭のレコードに移動 cursor.moveToFirst(); String valueCursor = cursor.getString(1); cursor.close(); return valueCursor; } /************************************** * */ private static void showDialog( Context context, String title, String text ){ AlertDialog.Builder varAlertDialog= new AlertDialog.Builder(context); varAlertDialog.setTitle(title); varAlertDialog.setMessage(text); varAlertDialog.setPositiveButton("OK",null); varAlertDialog.show(); } //ヘルパークラスの定義 private static class DatabaseHelper extends SQLiteOpenHelper{ //データベースを作成、 public DatabaseHelper(Context context){ //ヘルパークラスのコンストラクタの呼び出し super( context, DB_NAME, null, DB_VERSION ); } //テーブルを作成するメソッドの定義 @Override public void onCreate(SQLiteDatabase db){ db.execSQL( "CREATE TABLE IF NOT EXISTS "+ DB_TABLE+ "(id text primary key,info text)" ); } //データベースをアップグレードするメソッドの定義 @Override public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion ){ db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE); onCreate(db); } }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/28 13:49
2017/12/29 01:47
2018/01/15 14:25