android开发 SQLite增删查改

package org.crazyit.db;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class DBTest extends Activity
{
    SQLiteDatabase db;
    Button bn = null;
    Button bnf= null;
    Button bnd= null;
    Button bnUpdate=null;
    Button bnShow=null;
    ListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        //创建或打开数据库(此处需要使用绝对路径)
        db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir()
            .toString() + "/my.db3" , null);    
        System.out.println(this.getFilesDir()
                .toString() + "/my.db3" );
        listView = (ListView)findViewById(R.id.show);
        
        
        bn = (Button)findViewById(R.id.ok);
        bnf=(Button)findViewById(R.id.find);
        bnd=(Button)findViewById(R.id.delete);
        bnUpdate=(Button)findViewById(R.id.update);
        bnShow=(Button)findViewById(R.id.Show);
        //增加
        bn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                //获取用户输入
                String title = ((EditText)findViewById(R.id.title))
                    .getText().toString();
                String content = ((EditText)findViewById(R.id.content))
                    .getText().toString();
                try
                {
                    insertData(db , title , content);
                    Cursor cursor = db.rawQuery("select * from news_inf", null);
                    inflateList(cursor);
                }
                catch(SQLiteException  se)
                {
                    //执行DDL创建数据表
                    db.execSQL("create table news_inf(_id integer primary key autoincrement,"
                        + " news_title varchar(50),"
                        + " news_content varchar(255))");
                    //执行insert语句插入数据
                    insertData(db , title , content);
                    System.out.println("重复插入了一边!");
                    //执行查询
                    Cursor cursor = db.rawQuery("select * from news_inf", null);
                    //绑定查询
                    inflateList(cursor);
                }
            }


        });        
        //修改:    有问
        bnUpdate.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                //获取用户输入
                String title = ((EditText)findViewById(R.id.title))
                    .getText().toString();
                String content = ((EditText)findViewById(R.id.content))
                    .getText().toString();
//                    处理数据
                    
//                    Cursor cursor = db.query("news_inf",new String[]{"news_title, news_inf"},
//                            "news_title like ?",new String[]{"2%"},    null,null,"news_title desc","");
                    //System.out.println("Start  update哈哈哈!");
                    
                    ContentValues values = new ContentValues();
                //    values.put("news_title", "跟新");
                    values.put("news_content", "update成功");
                    //"news_title like?",new String[]{title}
                    db.update("news_inf", values, "news_title like ?",new String[]{title});

                    
                    Cursor cursor = db.rawQuery("select * from news_inf",null);
//                    Cursor cursor = db.rawQuery(
//                            "select * from news_inf where news_title like ?", new String[]{"2"});
                    
                    System.out.println("update哈哈哈!");
                    inflateList(cursor);
            }
        });    
        
//            

        //查find
        bnf.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                //获取用户输入
                String title = ((EditText)findViewById(R.id.title))
                    .getText().toString();

                    System.out.println("start find哈哈哈!");
                    Cursor cursor = db.rawQuery("select * from news_inf where news_title like ?",new String[]{title});

                    
                    System.out.println("2222哈哈哈!");
                    inflateList(cursor);
            }
        });    

        
        //删除delete:
        bnd.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                //获取用户输入
                String title = ((EditText)findViewById(R.id.title))
                    .getText().toString();
                String content = ((EditText)findViewById(R.id.content))
                    .getText().toString();
                    //    处理数据
                      int result=db.delete("news_inf", "news_title like?",new String[]{title});

                    Cursor cursor = db.rawQuery("select * from news_inf", null);
                    inflateList(cursor);
        }
        });
        
        bnShow.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                    Cursor cursor = db.rawQuery("select * from news_inf", null);
                    inflateList(cursor);
        }
        });
    }
    
    
    
    private void insertData(SQLiteDatabase db, String title , String content)
    {
        //执行插入语句
//        db.execSQL("insert into news_inf values(null , ? , ?)"   //使用占位符号
//            , new String[]{title , content});
        
        //下面是不使用占位符方法
        String s="insert into news_inf(news_title,news_content) values('" +title+ "','" +content+ "')";
        db.execSQL(s);
        
    }
    
    //打印数据
    private void inflateList(Cursor cursor)
    {
        //填充SimpleCursorAdapter
//        @SuppressWarnings("deprecation")
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            DBTest.this , R.layout.line, cursor
            , new String[]{"news_title" , "news_content"}
            , new int[]{R.id.my_title , R.id.my_content});
        //显示数据
        listView.setAdapter(adapter);
    }
    
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        //退出程序时关闭SQLiteDatabase
        if (db != null && db.isOpen())
        {
            db.close();
        }
    }
}

posted @ 2012-12-20 14:41  koalaheihei  阅读(374)  评论(0)    收藏  举报