1. 程式人生 > >Android資料庫sqlite儲存

Android資料庫sqlite儲存

一、修改佈局檔案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="新建資料表" />
    <Button android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查詢資料庫" />
    <Button android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="新增兩條資料" />
    <Button android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="刪除一條資料" />
    <Button android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="刪除資料表" />

</LinearLayout>

二、修改MainActivity檔案

package com.example.yxp.usersql;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;



import android.app.Activity;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {





    OnClickListener listener1 = null;
    OnClickListener listener2 = null;
    OnClickListener listener3 = null;
    OnClickListener listener4 = null;
    OnClickListener listener5 = null;

    Button button1;
    Button button2;
    Button button3;
    Button button4;
    Button button5;

    DatabaseHelper mOpenHelper;

    private static final String DATABASE_NAME = "dbForTest.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "diary";
    private static final String TITLE = "title";
    private static final String BODY = "body";

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
                    + " text not null, " + BODY + " text not null " + ");";
            Log.i("haiyang:createDB=", sql);
            db.execSQL(sql);

        }

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        prepareListener();
        initLayout();
        mOpenHelper = new DatabaseHelper(this);
    }



    private void initLayout() {
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(listener1);

        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(listener2);

        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(listener3);
        button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(listener4);

        button5 = (Button) findViewById(R.id.button5);
        button5.setOnClickListener(listener5);

    }

    private void prepareListener() {
        listener1 = new OnClickListener() {
            public void onClick(View v) {
                CreateTable();
            }
        };
        listener2 = new OnClickListener() {
            public void onClick(View v) {
                dropTable();
            }
        };
        listener3 = new OnClickListener() {
            public void onClick(View v) {
                insertItem();
            }
        };
        listener4 = new OnClickListener() {
            public void onClick(View v) {
                deleteItem();
            }
        };
        listener5 = new OnClickListener() {
            public void onClick(View v) {
                showItems();
            }
        };
    }

    /*
     * 重新建立資料表
     */
    private void CreateTable() {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
                + " text not null, " + BODY + " text not null " + ");";
        Log.i("haiyang:createDB=", sql);

        try {
            db.execSQL("DROP TABLE IF EXISTS diary");
            db.execSQL(sql);
            setTitle("重建表成功");
        } catch (SQLException e) {
            setTitle("重建表錯誤");
        }
    }

    /*
     * 刪除資料表
     */
    private void dropTable() {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        String sql = "drop table " + TABLE_NAME;
        try {
            db.execSQL(sql);
            setTitle("刪除成功:" + sql);
        } catch (SQLException e) {
            setTitle("刪除錯誤");
        }
    }

    /*
     * 插入兩條資料
     */
    private void insertItem() {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        String sql1 = "insert into " + TABLE_NAME + " (" + TITLE + ", " + BODY
                + ") values('AA', 'android很好');";
        String sql2 = "insert into " + TABLE_NAME + " (" + TITLE + ", " + BODY
                + ") values('BB', 'android很好');";
        try {
            Log.i("haiyang:sql1=", sql1);
            Log.i("haiyang:sql2=", sql2);
            db.execSQL(sql1);
            db.execSQL(sql2);
            setTitle("插入成功");
        } catch (SQLException e) {
            setTitle("插入失敗");
        }
    }

    /*
     * 刪除其中的一條資料
     */
    private void deleteItem() {
        try {
            SQLiteDatabase db = mOpenHelper.getWritableDatabase();
            db.delete(TABLE_NAME, " title = 'AA'", null);
            setTitle("刪除了一條title為AA的一條記錄");
        } catch (SQLException e) {

        }

    }

    /*
     * 在螢幕的title區域顯示當前資料表當中的資料的條數。
     */
    private void showItems() {

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        String col[] = { TITLE, BODY };
        Cursor cur = db.query(TABLE_NAME, col, null, null, null, null, null);

        Log.d("lcc",cur.getColumnName(1));
        Integer num = cur.getCount();
        setTitle(Integer.toString(num) + " 條記錄");
    }

}

三、效果