Android实用的SQLite数据库工具类

每天一个小目标,早晚单车变摩托


一个实用的Sqlite的工具类,调用的时候只需DBUtils.getInstance().creads(this);即可完成创建数据库


之后依次调用方法就好



DBUtils.java

package com.example.bookadmin.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


import com.example.bookadmin.bean.SongBean;
import com.example.bookadmin.tools.utils.LogUtils;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/10/19.
 * by:TaoHui
 */

public class DBUtils {
    private static DBUtils dbUtils;
    private SQLiteDatabase db;
    /**
     * 单例模式
     * @return
     */
    public static DBUtils getInstance(){
        if(dbUtils == null){
            dbUtils = new DBUtils();
            return dbUtils;
        }
        return dbUtils;
    }
    /**
     * 创建数据表
     * @param contenxt 上下文对象
     */
    public void creads(Context contenxt){
        String path = contenxt.getCacheDir().getPath()+"/muisd.db";
        db = SQLiteDatabase.openOrCreateDatabase(path,null);
        String sql = "create table if not exists t_person" +
                "(id integer primary key autoincrement," +
                "bsid int(50) ,name text(50))";
        db.execSQL(sql);//创建表
    }
    /**
     * 查询数据
     * 返回List
     */
    public ArrayList<SongBean> selectis() {
        ArrayList<SongBean> list = new ArrayList<>();
        Cursor cursor = db.query("t_person",null,null,null,null,null,null);
        while (cursor.moveToNext()){
            SongBean userBean = new SongBean();
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int bsid = cursor.getInt(cursor.getColumnIndex("bsid"));
            userBean.setName(name);
            userBean.setBsid(bsid);
            userBean.setId(id);
            list.add(userBean);
            Log.e("--Main--", "==============selectis======"+id+"================"+name+"================"+bsid);
        }
        if(cursor != null){
            cursor.close();
        }

        return list;
    }
    /**
     * 根据ID删除数据
     * id 删除id
     */
    public int delData(int id){
        int inde = db.delete("t_person","id = ?",new String[]{String.valueOf(id)});
        Log.e("--Main--", "==============删除了======================"+inde );
        return inde;
    }
    /**
     * 根据ID修改数据
     * id 修改条码的id
     * bsid 修改的ID
     * name 修改的数据库
     */
    public int modifyData(int id,int bsid, String name){
        ContentValues contentValues = new ContentValues();
        contentValues.put("name",name);
        contentValues.put("bsid",id);
        int index = db.update("t_person",contentValues,"id = ?",new String[]{String.valueOf(id)});
        Log.e("--Main--", "==============修改了======================"+index );
        return index;
    }
    /**
     * 添加数据
     * bsid 添加的数据ID
     * name 添加数据名称
     */
    public long insertData(int bsid, String name){
        ContentValues contentValues = new ContentValues();
        contentValues.put("name",name);
        contentValues.put("bsid",bsid);
        long dataSize = db.insert("t_person",null,contentValues);
        Log.e("--Main--", "==============insertData======================"+name+"================"+bsid);
        return dataSize;
    }
    /**
     * 查询名字单个数据
     * @param names
     * @return
     */
    public boolean selectisData(String names){
        //查询数据库
        Cursor cursor = db.query("t_person",null,"name = ?",new String[]{names},null,null,null);
        while (cursor.moveToNext()){
           return true;
        }
        return false;
    }
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值