2012年9月17日月曜日

SQLite

データベースの勉強

テーブル名productsで、名前と値段だけです。



********************  MainActivity.java  ********************
package jp.lesson.studyandroid;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import android.content.Context;
import android.widget.TextView;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        // データベース接続
        MyDBHelper helper = new MyDBHelper(this);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor c = db.query("products",
                new String[] { "name", "price" },
                null, null, null, null, null);
        /*    query()
             table:テーブル名
            columns:取得する列名(カラム名、フィールド名)の配列
            selection:取得するレコードの条件を指定
            group:
            having:
            order:
            limit:取得するレコードの上限数を指定。使わない場合は、null。
         */
       
        boolean isEof = c.moveToFirst();
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        String text="";
       
        while (isEof) {
            text += String.format("%s : %d円\r\n", c.getString(0), c.getInt(1));
            isEof = c.moveToNext();
        }
        textView1.setText(text);
        c.close();
        db.close();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    public class MyDBHelper extends SQLiteOpenHelper {
        public MyDBHelper(Context context) {
            super(context, null, null, 1);
                /*    コンテキストオブジェクト
                    fileName・・・データベース名
                    factory・・・通常nullを指定
                    version・・・データベースのバージョン
                */
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(
                    "create table products("+
                    "   name text not null,"+
                    "   price text"+
                    ");"
                );
            // table row insert
            db.execSQL("insert into products(name,price) values ('クッキー', 120);");
            db.execSQL("insert into products(name,price) values ('ビスケット', 85);");
            db.execSQL("insert into products(name,price) values ('ケーキ', 285);");
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO 自動生成されたメソッド・スタブ
           
        }
    }
}

********************  activity_main.xml  ********************
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
    </LinearLayout>

********************  strings.xml  ********************
<resources>
    <string name="app_name">StudyAndroid</string>
    <string name="hello">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
</resources>

0 件のコメント:

コメントを投稿