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);
|
|
}
|
}
|