IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

dbを操作するActivity

SQLをそのまま実行できるアクティビティを練習として作ってみました。タブレットの開発の場合は、adbで実機につないでdbの確認をするのではなく、デバック用のツール(アクティビティ)を提供して内部情報を確認した方が、便利なのではないかと思ってみたり。(もちろん、テスターにSQL書かせるようなツールはツールとしてNGですが....)

画面

f:id:naotoogawa:20140505194310p:plain

f:id:naotoogawa:20140505194315p:plain

コード

dbは作成されていることが前提です。

package com.example.test02;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SQLActivity extends Activity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("SQLActivity", "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sql);
        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.d("SQLActivity", "onclick");
        
        EditText editText = (EditText) findViewById(R.id.editText1);
        String text = editText.getText().toString().trim();     
        Log.d("SQLActivity", "edittext=" + text);     
        TextView textView = (TextView) findViewById(R.id.textView2);
        textView.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);

        SQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
        if (text.startsWith("select")) {
            SQLiteDatabase db = helper.getReadableDatabase();
            try {
                Cursor c = (Cursor)db.rawQuery(text, null);
                int rowcount = c.getCount();
                int columnCount = c.getColumnCount();
                StringBuffer sb = new StringBuffer();
                for (int j = 0; j < columnCount; j++) {
                    sb.append(c.getColumnName(j));
                    if ( j == columnCount -1 ) {
                        sb.append("\n");
                    } else {
                        sb.append(',');
                    }
                }
                sb.append("------\n");
                c.moveToFirst();
                for (int i = 0; i < rowcount ; i++) {
                    
                    for (int j = 0; j < columnCount; j++) {
                        sb.append(c.getString(j));
                        if ( j == columnCount -1 ) {
                            sb.append("\n");
                        } else {
                            sb.append(',');
                        }
                    }
                    c.moveToNext();
                }
                textView.setText(new String(sb));
            } catch (SQLException e) {
                showErrorDialog(textView, e);
            }
        } else {
            SQLiteDatabase db = helper.getWritableDatabase();
            try {
                db.execSQL(text);
                textView.setText("success : " + text);
            } catch (SQLException e) {
                showErrorDialog(textView, e);
            }
        }
    }
    
    private void showErrorDialog(TextView textView, SQLException e) {
        Log.e("ERROR", e.toString());
        textView.setText("faliure");
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("error");
        alertDialogBuilder.setMessage(e.toString());
        alertDialogBuilder.setCancelable(true);
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

    private static class MySQLiteOpenHelper extends SQLiteOpenHelper {
        public MySQLiteOpenHelper(Context context) {
            super(context, "mysqldbname", null, 1);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            this.onCreate(db);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            this.onUpgrade(db, oldVersion, newVersion);
        }
    }
}