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>

 

main.java

package com.example.sqlitedemo2;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
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 btn1;
    Button btn2;
    Button btn3;
    Button btn4;
    SQLiteDatabase db;

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

        db = openOrCreateDatabase("stu.db", MODE_PRIVATE, null);

        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)");

        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);
        btn3 = (Button) findViewById(R.id.button3);
        btn4 = (Button) findViewById(R.id.button4);

        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                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();
            }
        });

        btn2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Cursor cur = db.query("tb_user", new String[]{"name","age","sex"}, null, null, null, null, null);
                while(cur.moveToNext()){
                    String name = cur.getString(cur.getColumnIndex("name"));
                    int age = cur.getInt(cur.getColumnIndex("age"));
                    String sex = cur.getString(cur.getColumnIndex("sex"));
                    
                    Log.i("stuinfo", name + "," + age + "," + sex);
                }
                cur.close();
            }
            
        });
        
        
        btn3.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //修改数据
                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();
            }
        });
        
        btn4.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //删除数据
                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();
            }
        });
    }

}

 

posted on 2015-03-31 11:38  Builder  阅读(295)  评论(0编辑  收藏  举报