package com.file.databasetest;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    private Button mCreateDatabase;
    private Button mAddData;
    private Button mUpdateData;
    private Button mDeleteData;
    private Button mQueryData;
    private TextView mResult;
    private MyDatabaseHelper mDbHelper;

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

        mDbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);
        mCreateDatabase = (Button) findViewById(R.id.create_database);
        mAddData = (Button) findViewById(R.id.add_data);
        mUpdateData = (Button) findViewById(R.id.update_data);
        mDeleteData = (Button) findViewById(R.id.delete_data);
        mQueryData = (Button) findViewById(R.id.query_data);
        mResult = (TextView) findViewById(R.id.result);

        mCreateDatabase.setOnClickListener(this);
        mAddData.setOnClickListener(this);
        mUpdateData.setOnClickListener(this);
        mDeleteData.setOnClickListener(this);
        mQueryData.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.create_database:
            mDbHelper.getWritableDatabase();
            break;

        case R.id.add_data:
            addData();
            break;

        case R.id.update_data:
            updateData();
            break;

        case R.id.delete_data:
            deleteData();
            break;

        case R.id.query_data:
            queryData();
            break;

        default:
            break;
        }
    }

    private void addData() {
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", "The Da Vinci Code");
        values.put("author", "Dan Brown");
        values.put("pages", 454);
        values.put("price", 16.96);
        db.insert("Book", null, values);
        values.clear();
        values.put("name", "The Lost Symbol");
        values.put("author", "Dan Brown");
        values.put("pages", 510);
        values.put("price", 19.95);
        db.insert("Book", null, values);
    }

    private void updateData() {
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("price", 10.99);
        db.update("Book", values, "name = ?", new String[] { "The Da Vinci Code" });
    }

    private void deleteData() {
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        db.delete("Book", "pages > ?", new String[] { "500" });
    }

    private void queryData() {
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        Cursor cursor = db.query("Book", null, null, null, null, null, null);
        mResult.setText("----- query data from Book -----");
        mResult.append("\nTotal count is " + cursor.getCount());
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String author = cursor.getString(cursor.getColumnIndex("author"));
                int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                double price = cursor.getDouble(cursor.getColumnIndex("price"));
                mResult.append("\nname is " + name + "\n"
                                + "author is " + author + "\n"
                                + "pages is " + pages + "\n"
                                + "price is " + price);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }
}

@@ -2760,32 +2760,364 @@
 oid addData() %7B%0A
+        // db.execSQL(%22insert into Book (name, author, pages, price) values(?, ?, ?, ?)%22,%0A        // new String%5B%5D %7B%22The Da Vinci Code%22, %22Dan Brown%22, %22454%22, %2216.96%22%7D);%0A        // db.execSQL(%22insert into Book (name, author, pages, price) values(?, ?, ?, ?)%22,%0A        // new String%5B%5D %7B%22The Lost Symbol%22, %22Dan Brown%22, %22510%22, %2219.95%22%7D);%0A
         SQLiteDa
@@ -3153,32 +3153,32 @@
 ableDatabase();%0A
-
         ContentV
@@ -3829,16 +3829,138 @@
 10.99);%0A
+        // db.execSQL(%22update Book set price = ? where name = ?%22, new String%5B%5D%7B%2210.99%22,%0A        // %22The Da Vinci Code%22%7D);%0A
         
@@ -4128,32 +4128,115 @@
 ableDatabase();%0A
+        // db.execSQL(%22delete from Book where pages %3E ?%22, new String%5B%5D %7B %22500%22 %7D);%0A
         db.delet
@@ -4313,32 +4313,32 @@
 d queryData() %7B%0A
-
         SQLiteDa
@@ -4374,32 +4374,84 @@
 ableDatabase();%0A
+        // db.rawQuery(%22select * from Book%22, null);%0A
         Cursor c

add comment on how to use execSQL