SQLite增删改查(利用系统API)
2014-02-19 16:19 kingshow 阅读(244) 评论(0) 编辑 收藏 举报在SQLite增删改查(自己写SQL语句)中介绍了通过自己写SQL语句来实现增删改查。下面是利用系统API来实现。
一、在上一篇中工程中直接增加PersonService2,PersonDao2和MyTest2类。如图
二、PersonService2详细代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bruce.db_test.service; import java.util.List; import java.util.Map; import android.content.ContentValues; public interface PersonService2 { public boolean insertPerson(ContentValues values); public boolean deletePerson(String whereClause,String[] whereArgs); public boolean updatePerson(ContentValues values, String whereClause,String[] whereArgs); public Map<String, String> findPerson(String selection, String[] selectionArgs); public List<Map<String, String>> listPerson(String selection, String[] selectionArgs); }
三、PersonDao2详细代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bruce.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 android.util.Log; import com.bruce.db_test.db.DbOpenHelper; import com.bruce.db_test.service.PersonService2; public class PersonDao2 implements PersonService2 { private String TAG = "MyTest"; private DbOpenHelper helper = null; public PersonDao2(Context context){ helper = new DbOpenHelper(context); Log.i(TAG, "-->helper assignment"); } @Override public boolean insertPerson(ContentValues values) { boolean flag = false; long id = -1; SQLiteDatabase database = null; try { database = helper.getWritableDatabase(); id = database.insert("person", null, values); flag = (id != -1? true:false); Log.i(TAG, "-->insert "+flag +",id=" + id); } catch (Exception e) { e.printStackTrace(); }finally{ if(database != null){ database.close(); } } return flag; } @Override public boolean deletePerson(String whereClause,String[] whereArgs) { boolean flag = false; int count = 0; SQLiteDatabase database = null; try { database = helper.getWritableDatabase(); count = database.delete("person", whereClause, whereArgs); flag = (count > 0 ? true:false); Log.i(TAG, "-->delete "+flag +",count=" + count); } catch (Exception e) { e.printStackTrace(); } 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); Log.i(TAG, "-->updata " + flag); } catch (Exception e) { e.printStackTrace(); } finally{ if(database != null){ database.close(); } } return flag; } @Override public Map<String, String> findPerson(String selection, String[] selectionArgs) { Map<String, String> map = new HashMap<String, String>(); SQLiteDatabase database = null; Cursor cursor = null; Log.i(TAG, "-->findPerson begin"); try { database = helper.getWritableDatabase(); cursor = database.query(false, "person", null, selection, selectionArgs, null, null, null, null, null); int count = cursor.getColumnCount(); while (cursor.moveToNext()) { for(int i=0;i<count;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) { e.printStackTrace(); }finally{ if(database != null){ database.close(); } } System.out.println("-->map:"+ map.toString()); return map; } @Override public List<Map<String, String>> listPerson(String selection, String[] selectionArgs) { 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, null); int count = cursor.getColumnCount(); while(cursor.moveToNext()){ Map<String, String> map = new HashMap<String, String>(); for(int i=0;i<count;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) { e.printStackTrace(); } finally{ if(database != null){ database.close(); } } return list; } }
四、MyTest2详细代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bruce.db_test.test; import java.util.List; import java.util.Map; import com.bruce.dao.PersonDao2; import com.bruce.db_test.service.PersonService2; import android.Manifest.permission; import android.content.ContentValues; import android.test.AndroidTestCase; import android.util.Log; public class MyTest2 extends AndroidTestCase { private String TAG = "MyTest"; public void insertTest(){ PersonService2 service2 = new PersonDao2(getContext()); ContentValues values = new ContentValues(); values.put("name", "星爷"); values.put("address", "香港"); values.put("sex", "男"); boolean flag = service2.insertPerson(values); Log.i(TAG, "test insert end,flag="+ flag); } public void deletePersonTest() { PersonService2 service2 = new PersonDao2(getContext()); String[] whereArgs = {"5"}; boolean flag = service2.deletePerson(" id = ? ", whereArgs); Log.i(TAG, "test delete end,flag="+ flag); } public void updatePersonTest(){ PersonService2 service2 = new PersonDao2(getContext()); ContentValues values = new ContentValues(); values.put("name", "老毛"); values.put("address", "北京"); values.put("sex", "No"); String[] whereArgs = {"1"}; //boolean flag = service2.updatePerson(values, " id = ? ", new String[]{"1"}); boolean flag = service2.updatePerson(values, " id = ? ", whereArgs); Log.i(TAG, "-->test " + flag); } public void viewPersonTest(){ Log.i(TAG, "-->viewPerson begin"); PersonService2 service2 = new PersonDao2(getContext()); String selection = " id = ? "; String[] selectionArgs = {"1"}; Map<String, String> map = service2.findPerson(selection, selectionArgs); Log.i(TAG, "--->viewPerson" + map.toString()); } public void listPersonTest(){ PersonService2 service2 = new PersonDao2(getContext()); List<Map<String, String>> list = service2.listPerson(null, null); Log.i(TAG, "--->list test:" + list.toString()); } }
五、以下是测试时的一些输出信息