Android学习(十) SQLite 基于SQLiteOpenHelper的操作方式

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".MainActivity" >



    <Button

        android:id="@+id/button1"

        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="读取数据" />



    <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="删除数据" />



</LinearLayout>

 

DBOpenHelper.java

package com.example.sqlitedemo3;



import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;



public class DBOpenHelper extends SQLiteOpenHelper{



    //实例化时,指定创建数据库的名称

    public DBOpenHelper(Context context, String name) {

        super(context, name, null, 1);

    }



    @Override  //当第一次创建数据库时调用,系统自动调用,只执行一次

    public void onCreate(SQLiteDatabase db) {

        //第一次执行时,创建tb_user表

        db.execSQL("create table if not exists tb_user(id integer primary key autoincrement,name text not null,age integer not null,sex text not null)");

    }



    @Override  //当数据库版本发生改变时调用,系统自动调用

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

        

    }



}

 

 

main.java

package com.example.sqlitedemo3;



import android.os.Bundle;

import android.app.Activity;

import android.content.ContentValues;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;



public class MainActivity extends Activity {



    Button btnAdd;

    Button btnQuery;

    Button btnUpdate;

    Button btnDelete;

    SQLiteDatabase db;

    DBOpenHelper helper;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        //创建DBOpenHelper对象,第一个参数为调用的activity,第二个参数为数据库的名称

        helper = new DBOpenHelper(MainActivity.this, "user.db");        



        btnAdd = (Button) findViewById(R.id.button1);

        btnQuery = (Button) findViewById(R.id.button2);

        btnUpdate = (Button) findViewById(R.id.button3);

        btnDelete = (Button) findViewById(R.id.button4);



        btnAdd.setOnClickListener(new OnClickListener() {



            @Override

            public void onClick(View v) {                

                //只是可写的数据库对象

                db = helper.getWritableDatabase();

                

                ContentValues values = new ContentValues();

                values.put("name", "李四");

                values.put("age", 20);

                values.put("sex", "女");                

                db.insert("tb_user", null, values);

                values.clear();

                

                values.put("name", "王五");

                values.put("age", 22);

                values.put("sex", "男");                

                db.insert("tb_user", null, values);

                Toast.makeText(MainActivity.this, "添加成功", 1).show();

                

                db.close();

            }

        });



        btnQuery.setOnClickListener(new OnClickListener() {



            @Override

            public void onClick(View v) {

                //只是可写的数据库对象

                db = helper.getReadableDatabase();

                //其他方法和前面没有区别

                Cursor cur = db.query("tb_user", new String[]{"id","name","age","sex"}, null, null, null, null, null);

                while(cur.moveToNext()){

                    int id = cur.getInt(0);

                    String name = cur.getString(cur.getColumnIndex("name"));

                    int age = cur.getInt(cur.getColumnIndex("age"));

                    String sex = cur.getString(cur.getColumnIndex("sex"));

                    

                    Log.i("stuinfo",id + ","+ name + "," + age + "," + sex);

                }

                cur.close();

                db.close();

            }

        });



        btnUpdate.setOnClickListener(new OnClickListener() {



            @Override

            public void onClick(View v) {

                //修改数据

                db = helper.getWritableDatabase();

                ContentValues values = new ContentValues();

                values.put("name", "张三丰");

                int result = db.update("tb_user", values, "id=?", new String[]{"1"});

                if(result > 0)

                    Toast.makeText(MainActivity.this, "修改成功", 1).show();

                else

                    Toast.makeText(MainActivity.this, "修改失败", 1).show();

                db.close();

            }

        });



        btnDelete.setOnClickListener(new OnClickListener() {



            @Override

            public void onClick(View v) {

                db = helper.getWritableDatabase();

                //删除数据

                int result = db.delete("tb_user", "id=?", new String[]{"1"});

                if(result > 0)

                    Toast.makeText(MainActivity.this, "删除成功", 1).show();

                else

                    Toast.makeText(MainActivity.this, "删除失败", 1).show();

                db.close();

            }

        });

    }



}

 

你可能感兴趣的:(Android学习)