基本信息
源码名称:android 简化数据库操作 例子源码
源码大小:1.17M
文件格式:.zip
开发语言:Java
更新时间:2014-11-11
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 4 元 
   源码介绍



package com.example.greendemoandroid;

import java.util.List;

import com.liu.demo.DaoSession;
import com.liu.demo.Note;
import com.liu.demo.NoteDao;

import android.content.Context;
import android.util.Log;

public class DbService {  
      
    private static final String TAG = DbService.class.getSimpleName();  
    private static DbService instance;  
    private static Context appContext;  
    private DaoSession mDaoSession;  
    private NoteDao noteDao;  
      
      
    private DbService() {  
    }  
  
    public static DbService getInstance(Context context) {  
        if (instance == null) {  
            instance = new DbService();  
            if (appContext == null){  
                appContext = context.getApplicationContext();  
            }  
            instance.mDaoSession = BaseApplication.getDaoSession(context);  
            instance.noteDao = instance.mDaoSession.getNoteDao();  
        }  
        return instance;  
    }  
      
      
    public Note loadNote(long id) {  
        return noteDao.load(id);  
    }  
      
    public List<Note> loadAllNote(){  
        return noteDao.loadAll();  
    }  
      
    /** 
     * query list with where clause 
     * ex: begin_date_time >= ? AND end_date_time <= ? 
     * @param where where clause, include 'where' word 
     * @param params query parameters 
     * @return 
     */  
      
    public List<Note> queryNote(String where, String... params){  
        return noteDao.queryRaw(where, params);  
    }  
      
      
    /** 
     * insert or update note 
     * @param note 
     * @return insert or update note id 
     */  
    public long saveNote(Note note){  
        return noteDao.insertOrReplace(note);  
    }  
      
      
    /** 
     * insert or update noteList use transaction 
     * @param list 
     */  
    public void saveNoteLists(final List<Note> list){  
            if(list == null || list.isEmpty()){  
                 return;  
            }  
            noteDao.getSession().runInTx(new Runnable() {  
            @Override  
            public void run() {  
                for(int i=0; i<list.size(); i  ){  
                    Note note = list.get(i);  
                    noteDao.insertOrReplace(note);  
                }  
            }  
        });  
          
    }  
      
    /** 
     * delete all note 
     */  
    public void deleteAllNote(){  
        noteDao.deleteAll();  
    }  
      
    /** 
     * delete note by id 
     * @param id 
     */  
    public void deleteNote(long id){  
        noteDao.deleteByKey(id);  
        Log.i(TAG, "delete");  
    }  
      
    public void deleteNote(Note note){  
        noteDao.delete(note);  
    }  
      
}