Day11_04

package com.example.day11_04;

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

public class MyHelper extends SQLiteOpenHelper {

    public MyHelper(Context context) {
        super(context, "itcast.db", null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))");
    }

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

    }

}
package com.example.day11_04;

import android.support.v7.app.ActionBarActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.SyncStateContract.Helpers;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    MyHelper myHelper;
    private EditText mEtName;
    private EditText mEtPhone;
    private TextView mTvShow;
    private Button mBtnAdd;
    private Button mBtnQuery;
    private Button mBtnUpdate;
    private Button mBtnDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myHelper = new MyHelper(this);
        init();
    }

    private void init() {
        mEtName = (EditText) findViewById(R.id.editText1);
        mEtPhone = (EditText) findViewById(R.id.editText2);
        mTvShow = (TextView) findViewById(R.id.textView4);
        mBtnAdd = (Button) findViewById(R.id.button1);
        mBtnQuery = (Button) findViewById(R.id.button2);
        mBtnUpdate = (Button) findViewById(R.id.button3);
        mBtnDelete = (Button) findViewById(R.id.button4);
    }

    public void click(View v) {
        String name, phone;
        SQLiteDatabase db;
        ContentValues values;
        switch (v.getId()) {
        case R.id.button1:// 添加数据
            name = mEtName.getText().toString();
            phone = mEtPhone.getText().toString();
            db = myHelper.getWritableDatabase();
            values = new ContentValues();
            values.put("name", name);
            values.put("phone", phone);
            db.insert("information", null, values);
            Toast.makeText(this, "信息已添加", 0).show();
            db.close();
            break;
        case R.id.button2:// 查询数据
            db = myHelper.getReadableDatabase();
            Cursor cursor = db.query("information", null, null, null, null,
                    null, null);
            if (cursor.getCount() == 0) {
                mTvShow.setText("");
                Toast.makeText(this, "没有数据", 0).show();
            } else {
                cursor.moveToFirst();
                mTvShow.setText("Name:" + cursor.getString(1) + "; Tel:"
                        + cursor.getString(2));
            }
            while (cursor.moveToNext()) {
                mTvShow.append("
" + "Name:" + cursor.getString(1)
                        + "; Tel:" + cursor.getString(2));
            }
            cursor.close();
            db.close();
            break;
        case R.id.button3:// 修改数据
            db = myHelper.getWritableDatabase();
            values = new ContentValues();
            values.put("phone", phone = mEtPhone.getText().toString());
            db.update("information", values, "name=?", new String[] { mEtName
                    .getText().toString() });
            Toast.makeText(this, "信息已修改", 0).show();
            db.close();
            break;
        case R.id.button4:// 删除数据
            db = myHelper.getWritableDatabase();
            String m = mEtName.getText().toString();
            int delete = db.delete("information", "name=?", new String[] {m});
            Toast.makeText(this,  "删除成功", 0).show();
            db.close();
            break;
        }
    }

}
package com.example.day11_04;

import android.support.v7.app.ActionBarActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.SyncStateContract.Helpers;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    MyHelper myHelper;
    private EditText mEtName;
    private EditText mEtPhone;
    private TextView mTvShow;
    private Button mBtnAdd;
    private Button mBtnQuery;
    private Button mBtnUpdate;
    private Button mBtnDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myHelper = new MyHelper(this);
        init();
    }

    private void init() {
        mEtName = (EditText) findViewById(R.id.editText1);
        mEtPhone = (EditText) findViewById(R.id.editText2);
        mTvShow = (TextView) findViewById(R.id.textView4);
        mBtnAdd = (Button) findViewById(R.id.button1);
        mBtnQuery = (Button) findViewById(R.id.button2);
        mBtnUpdate = (Button) findViewById(R.id.button3);
        mBtnDelete = (Button) findViewById(R.id.button4);
    }

    public void click(View v) {
        String name, phone;
        SQLiteDatabase db;
        ContentValues values;
        switch (v.getId()) {
        case R.id.button1:// 添加数据
            name = mEtName.getText().toString();
            phone = mEtPhone.getText().toString();
            db = myHelper.getWritableDatabase();
            values = new ContentValues();
            values.put("name", name);
            values.put("phone", phone);
            db.insert("information", null, values);
            Toast.makeText(this, "信息已添加", 0).show();
            db.close();
            break;
        case R.id.button2:// 查询数据
            db = myHelper.getReadableDatabase();
            Cursor cursor = db.query("information", null, null, null, null,
                    null, null);
            if (cursor.getCount() == 0) {
                mTvShow.setText("");
                Toast.makeText(this, "没有数据", 0).show();
            } else {
                cursor.moveToFirst();
                mTvShow.setText("Name:" + cursor.getString(1) + "; Tel:"
                        + cursor.getString(2));
            }
            while (cursor.moveToNext()) {
                mTvShow.append("
" + "Name:" + cursor.getString(1)
                        + "; Tel:" + cursor.getString(2));
            }
            cursor.close();
            db.close();
            break;
        case R.id.button3:// 修改数据
            db = myHelper.getWritableDatabase();
            values = new ContentValues();
            values.put("phone", phone = mEtPhone.getText().toString());
            db.update("information", values, "name=?", new String[] { mEtName
                    .getText().toString() });
            Toast.makeText(this, "信息已修改", 0).show();
            db.close();
            break;
        case R.id.button4:// 删除数据
            db = myHelper.getWritableDatabase();
            String m = mEtName.getText().toString();
            int delete = db.delete("information", "name=?", new String[] {m});
            Toast.makeText(this,  "删除成功", 0).show();
            db.close();
            break;
        }
    }

}

 

 

 

原文地址:https://www.cnblogs.com/zwcg/p/11793103.html