package com.tejia.lijin.app.sqlite; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import com.wpc.library.util.common.StringUtils; /** * 首页弹框+配置信息 */ public class HomeConfigSQHelper { private SQLiteDatabase db; private CustomServiceSQLiteOpenHelper openHelper; public HomeConfigSQHelper(Context context) { openHelper = new CustomServiceSQLiteOpenHelper(context); } /** * 查询数据 * * @param adid * @return */ public String getShowTime(String adid) { if (StringUtils.isEmpty(adid)) return ""; db = openHelper.getWritableDatabase();//以读写的方式访问数据库 Cursor cursor = db.rawQuery("select * from homeconfig_table where adid=?", new String[]{adid}); String time = ""; if (cursor.getCount() != 0) { while (cursor.moveToNext()) { //遍历数据库 time = cursor.getString(cursor.getColumnIndex("adtime")); } } cursor.close();//关闭cursor db.close();// 关闭数据库 Log.e("eee", "getShowTime: " + time); return time; } /** * 设置数据 * * @param id * @param time */ public boolean setShowTime(String id, String time) { if (StringUtils.isEmpty(id) || StringUtils.isEmpty(time)) return false; boolean b = true; //更具ID查询有这条数据 String result=getShowTime(id); if (result != null &&result.length() > 0) { b = update(id, time);//更新信息 } else { b = insert(id, time);//插入数据 } return b; } /** * 插入数据 * * @param id * @param time */ private boolean insert(String id, String time) { if (StringUtils.isEmpty(id) || StringUtils.isEmpty(time)) return false; db = openHelper.getWritableDatabase();//以读写的方式访问数据库 ContentValues value = new ContentValues(); value.put("adid", id); value.put("adtime", time); db.insert("homeconfig_table", null, value); db.close();// 关闭数据库 return true; } /** * 更新数据 * * @param id * @param time */ private boolean update(String id, String time) { if (StringUtils.isEmpty(id) || StringUtils.isEmpty(time)) return false; db = openHelper.getWritableDatabase();//以读写的方式访问数据库 ContentValues value = new ContentValues(); value.put("adid", id); value.put("adtime", time); db.update("homeconfig_table", value, "adid=?", new String[]{id}); db.close();// 关闭数据库 return true; } }