PG備忘録

プログラミングいろいろ

SQLiteのデータの追加・表示

f:id:olee46:20170531141239j:plain
機能:
・レコードの追加
・全レコードの表示

必要なクラス
・MainActivity.java
・MyDbHelper.java (SQLLiteOpenHelperを実装)

MainActivity.java

package com.example.orisa.mydb3;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    //部品の変数
    RelativeLayout layout;
    TextView tv;//データベースを表示するテキストビュー
    EditText addName;//名前の入力
    EditText addAge;//年齢の入力
    Button addBtn;//データの追加
    Button showBtn;//データベースの表示


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //部品の取得
        layout = (RelativeLayout) findViewById(R.id.layout);
        tv = (TextView)findViewById(R.id.tv) ;
        addName = (EditText)findViewById(R.id.addName);
        addAge = (EditText)findViewById(R.id.addAge);
        addBtn = (Button)findViewById(R.id.addBtn);
        showBtn = (Button)findViewById(R.id.showBtn);

        //データベースの生成
        MyDbHelper hlp = new MyDbHelper(this);
        final SQLiteDatabase db = hlp.getReadableDatabase();

        //データの取得と追加
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ContentValuesにデータを入れる
                ContentValues val = new ContentValues();
                val.put("name", addName.getText().toString());
                val.put("age", addAge.getText().toString());

                //データベースにレコードを挿入
                db.insert("person_table", null, val);
            }
        });

        //データの読み出し
        showBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //テキストビューをリセットする
                tv.setText(null);

                //全レコードを表示
                Cursor c = db.query("person_table", new String[]{"name, age"},
                        null, null, null, null, null);
                boolean mov = c.moveToFirst();
                while (mov){
                    //一つのレコードを追加
                    tv.append(String.format("%s: %d才", c.getString(0), c.getInt(1)));
                    //改行
                    tv.append("\n");
                    mov = c.moveToNext();
                }
                c.close();
            }
        });

    }
}


MyDbHelper.java

package com.example.orisa.mydb3;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by orisa on 2017/05/30.
 */

public class MyDbHelper extends SQLiteOpenHelper {
    public MyDbHelper(Context context){
        super(context, null, null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE person_table(name TEXT NOT NULL, age TEXT);");

        //データを挿入する
        db.execSQL("INSERT INTO person_table(name, age) values(?,?);", new Object[]{"Adam Smith", 30});
        db.execSQL("INSERT INTO person_table(name, age) values(?,?);", new Object[]{"John Lennon", 40});
        db.execSQL("INSERT INTO person_table(name, age) values(?, ?);", new Object[]{"Paul McCartney", 50});


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}


activity_main.xml
f:id:olee46:20170531141248j:plain



参考サイト
SQLiteでデータベース - 愚鈍人