Android中使用Sqlite数据库(三) 使用 SQLiteDatabase类实现 增删改查

package com.example.android_db.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.android_db.db.DbOpenHelper;
import com.example.android_db.service.PersonService2;
public class PersonDao2 implements PersonService2 {
    private DbOpenHelper helper = null;
    public PersonDao2(Context context) {
        // TODO Auto-generated constructor stub
        helper = new DbOpenHelper(context);
    }
    @Override
    public boolean addPerson(ContentValues values) {
        // TODO Auto-generated method stub
        boolean flag = false;
        SQLiteDatabase database = null;
        long id = -1;
        try {
            database = helper.getWritableDatabase();
            id = database.insert("person", null, values);
            flag = (id != -1 ? true : false);
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            if (database != null) {
                database.close();
            }
        }
        return flag;
    }
    @Override
    public boolean deletePerson(String whereClause, String[] whereArgs) {
        // TODO Auto-generated method stub
        boolean flag = false;
        SQLiteDatabase database = null;
        int count = 0;
        try {
            database = helper.getWritableDatabase();
            count = database.delete("person", whereClause, whereArgs);
            flag = (count > 0 ? true : false);
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            if (database != null) {
                database.close();
            }
        }
        return flag;
    }
    @Override
    public boolean updatePerson(ContentValues values, String whereClause,
            String[] whereArgs) {
        boolean flag = false;
        SQLiteDatabase database = null;
        int count = 0;// 影响数据库的行数
        try {
            database = helper.getWritableDatabase();
            count = database.update("person", values, whereClause, whereArgs);
            flag = (count > 0 ? true : false);
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            if (database != null) {
                database.close();
            }
        }
        return flag;
    }
    @Override
    public Map<String, String> viewPerson(String selection,
            String[] selectionArgs) {
        // TODO Auto-generated method stub
        // select 返回的列的名称(投影查询) from
        SQLiteDatabase database = null;
        Cursor cursor = null;
        Map<String, String> map = new HashMap<String, String>();
        try {
            database = helper.getReadableDatabase();
            cursor = database.query(true, "person", null, selection,
                    selectionArgs, null, null, null, null);
            int cols_len = cursor.getColumnCount();
            while (cursor.moveToNext()) {
                for (int i = 0; i < cols_len; i++) {
                    String cols_name = cursor.getColumnName(i);
                    String cols_value = cursor.getString(cursor
                            .getColumnIndex(cols_name));
                    if (cols_value == null) {
                        cols_value = "";
                    }
                    map.put(cols_name, cols_value);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            if (database != null) {
                database.close();
            }
        }
        return map;
    }
    @Override
    public List<Map<String, String>> listPersonMaps(String selection,
            String[] selectionArgs) {
        // TODO Auto-generated method stub
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        SQLiteDatabase database = null;
        Cursor cursor = null;
        try {
            database = helper.getReadableDatabase();
            cursor = database.query(false, "person", null, selection,
                    selectionArgs, null, null, null, null);
            int cols_len = cursor.getColumnCount();
            while (cursor.moveToNext()) {
                Map<String, String> map = new HashMap<String, String>();
                for (int i = 0; i < cols_len; i++) {
                    String cols_name = cursor.getColumnName(i);
                    String cols_value = cursor.getString(cursor
                            .getColumnIndex(cols_name));
                    if (cols_value == null) {
                        cols_value = "";
                    }
                    map.put(cols_name, cols_value);
                }
                list.add(map);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            if (database != null) {
                database.close();
            }
        }
        return list;
    }
}


测试类:


package com.example.android_db.test;
import java.util.List;
import java.util.Map;
import com.example.android_db.dao.PersonDao2;
import com.example.android_db.service.PersonService2;
import android.content.ContentValues;
import android.test.AndroidTestCase;
import android.view.ViewDebug.FlagToString;
public class MyTest2 extends AndroidTestCase {
    public MyTest2() {
        // TODO Auto-generated constructor stub
    }
    public void addPerson() {
        PersonService2 service2 = new PersonDao2(getContext());
        ContentValues values = new ContentValues();// 类似map的属性
        values.put("name", "杰克");
        values.put("address", "XX");
        values.put("sex", "男");
        boolean flag = service2.addPerson(values);
        System.out.println("--->>" + flag);
    }
    public void deletePerson() {
        PersonService2 service2 = new PersonDao2(getContext());
        // delete from person where id = ?
        //不包含 where关键字
        boolean flag = service2.deletePerson(" id = ? ", new String[] { "4" });
        System.out.println("--->>" + flag);
    }
      
    public void updatePerson(){
        PersonService2 service2 = new PersonDao2(getContext());
        ContentValues values = new ContentValues();// 类似map的属性
        values.put("name", "杰克");
        values.put("address", "XX");
        values.put("sex", "女");
        boolean flag = service2.updatePerson(values, " id = ? ", new String[]{"2"});
        System.out.println("--->>" + flag);
    }
      
    public void viewPerson(){
        PersonService2 service2 = new PersonDao2(getContext());
        Map<String,String> map = service2.viewPerson(" id = ? ", new String[]{"2"});
        System.out.println("-->>"+map.toString());
    }
      
    public void listPerson(){
        PersonService2 service2 = new PersonDao2(getContext());
        //select * from person
        List<Map<String,String>> list = service2.listPersonMaps(null, null);
        System.out.println("-->>"+list.toString());
    }
}


你可能感兴趣的:(sqlite)