android典型代码系列(七)------SQLite数据库的增删改查

4_SQLite数据库的增删改查 :

package cn.itcast.mobilesafe.db.dao;
import java.util.ArrayList;
import java.util.List;
import cn.itcast.mobilesafe.db.AppLockDBHelper;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class AppLockDao {
    private Context context;
    private AppLockDBHelper dbHelper;
    public AppLockDao(Context context) {
        this.context = context;
        dbHelper = new AppLockDBHelper(context);
    }
    /**查询    */
    public boolean find(String packname) {
        boolean result = false;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        if (db.isOpen()) {
            Cursor cursor = db.rawQuery("select packname from applock where packname=?",new String[] { packname });
            if (cursor.moveToNext()) {
                result = true;
            }
            cursor.close();
            db.close();
        }
        return result;
    }
    /**添加*/
    public void add(String packname){
        if(find(packname)){
            return ;
        }
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if(db.isOpen()){
            db.execSQL("insert into applock (packname) values (?)", new Object[]{packname});
            db.close();
        }
    }
    /**删除*/
    public void delete(String packname){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if(db.isOpen()){
            db.execSQL("delete from applock where packname=?", new Object[]{packname});
            db.close();
        }
    }
    /*** 查找全部包名*/
    public List<String> getAllApps(){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        List<String> packnames = new ArrayList<String>();
        if (db.isOpen()) {
           Cursor cursor =db.rawQuery("select packname from applock", null);
           while (cursor.moveToNext()) {
            String packname = cursor.getString(0);
            packnames.add(packname);
           }
           cursor.close();
           db.close();
        }
        return packnames;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值