package com.android.mtov.Util; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import com.android.mtov.Bean.PlanBean; import java.util.ArrayList; /** * Created by sf on 2017/11/7. */ 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() + "/mtov.db"; db = SQLiteDatabase.openOrCreateDatabase(path, null); String sql = "create table if not exists mtov" + "(id integer primary key autoincrement," + "name text(50),mon text(50),address text(50),number text(50))"; db.execSQL(sql);//创建表 } /** * 查询数据 * 返回List */ public ArrayList<PlanBean> selectis() { ArrayList<PlanBean> list = new ArrayList<PlanBean>(); Cursor cursor = db.query("mtov", null, null, null, null, null, null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); String mon = cursor.getString(cursor.getColumnIndex("mon")); String address = cursor.getString(cursor.getColumnIndex("address")); String number = cursor.getString(cursor.getColumnIndex("number")); list.add(new PlanBean(id, name, mon, address, number)); Log.e("--Main--", "selectis=========" + id + "==" + name + "==" + mon + "==" + address + "==" + number); } if (cursor != null) { cursor.close(); } return list; } /** * 根据ID删除数据 * id 删除id */ public int delData(int id) { int inde = db.delete("mtov", "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("mtov", contentValues, "id = ?", new String[]{String.valueOf(id)}); Log.e("--Main--", "修改了===============" + index); return index; } /** * 添加数据 * bsid 添加的数据ID * name 添加数据名称 */ public long insertData(String name, String mon, String address, String number) { ContentValues contentValues = new ContentValues(); contentValues.put("name", name); contentValues.put("mon", mon); contentValues.put("address", address); contentValues.put("number", number); long dataSize = db.insert("mtov", null, contentValues); Log.e("--Main--", "insertData====" + name + "==" + mon + "==" + address + "==" + number); return dataSize; } /** * 查询名字单个数据 * @param names * @return */ public boolean selectisData(String names) { //查询数据库 Cursor cursor = db.query("mtov", null, "name = ?", new String[]{names}, null, null, null); while (cursor.moveToNext()) { return true; } return false; } }
android轻量级数据库SQlite的工具类
最新推荐文章于 2024-06-27 16:09:35 发布