admin
2021-05-28 5965c01b38a2e83cecd7616daa11185fc2499303
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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;
    }
}