admin
2021-07-06 abce02c7a61820f5d580f87364d542e817be429c
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
101
102
103
104
105
106
107
108
109
110
111
package com.tejia.lijin.app.db;
 
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
import com.tejia.lijin.app.entity.SearchHistory;
 
import java.util.ArrayList;
import java.util.List;
 
public class SearchHistoryDao {
 
    private SQLiteOpenHelper helper;
 
    private Context mContext;
 
    private String dbName = "Search.db";
 
    private int verson = 1;
 
    public SearchHistoryDao(Context context) {
        mContext = context;
    }
 
    public void insert(SearchHistory info) {
 
        helper = new SearchHistoryDatabaseHelper(mContext, dbName, null, verson);
 
        Log.i("MYSQLITEHELPER", "before get db");
 
        SQLiteDatabase db = helper.getWritableDatabase();
 
        Log.i("MYSQLITEHELPER", "after get db");
 
        db.execSQL("insert into search_history(name) values(?)", new Object[]{info.getName()});
 
        db.close();
    }
 
    public List<SearchHistory> getAllSearchHistory() {
 
        List<SearchHistory> list = new ArrayList<>();
 
        helper = new SearchHistoryDatabaseHelper(mContext, dbName, null, verson);
 
        SQLiteDatabase db = helper.getWritableDatabase();
 
        Cursor cursor = db.rawQuery("select id,name from search_history ORDER BY id DESC", null);
 
        if (cursor == null) {
            return null;
        }
 
        while (cursor.moveToNext()) {
            SearchHistory history = new SearchHistory();
            history.setId(cursor.getInt(0));
            history.setName(cursor.getString(1));
            list.add(history);
        }
        return list;
    }
 
    /*
    修改指定ID的Name值
     */
    public void updateNameById(int id, String newName) {
 
        helper = new SearchHistoryDatabaseHelper(mContext, dbName, null, verson);
 
        SQLiteDatabase db = helper.getWritableDatabase();
 
        ContentValues values = new ContentValues();
 
        values.put("name", newName);
 
        db.update("search_history", values, "id=?", new String[]{id + ""});
    }
 
    /*
    删除指定ID项
     */
    public void deleteById(int id) {
 
        helper = new SearchHistoryDatabaseHelper(mContext, dbName, null, verson);
 
        SQLiteDatabase db = helper.getWritableDatabase();
 
        db.delete("search_history", "id=?", new String[]{id + ""});
 
    }
 
    public void addSearch(SearchHistory info) {
 
        helper = new SearchHistoryDatabaseHelper(mContext, dbName, null, verson);
 
        SQLiteDatabase db = helper.getWritableDatabase();
 
//        ContentValues values = new ContentValues();
//
//        values.put("name", info.getName());
//
//        db.insert("search_history", null, values);
 
        db.rawQuery("update search_history set id=" + (info.getId() + 1) + " where id<10", null);
 
    }
}