Android存储之SQLite数据库

Android存储之SQLite数据库数据库

创建数据库

package ……;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by kongqw on 2015/11/22.
 */
public class SearchRecordOpenHelper extends SQLiteOpenHelper {

    // 创建数据库的SQL语句
    private static final String CREATE_SEARCH_RECORD_TABLE = "CREATE TABLE SEARCH_RECORD_TABLE(ID INTEGER PRIMARY KEY AUTOINCREMENT,TEXT VARCHAR(50) NOT NULL)";

    public SearchRecordOpenHelper(Context context) {
        super(context, "search_record.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 执行SQL语句创建数据库
        db.execSQL(CREATE_SEARCH_RECORD_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

操作数据库

package ……;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;

/**
 * Created by kongqw on 2015/11/22.
 * 查询记录的增删
 */
public class SearchRecordDao {

    private static final String TAG = "SearchRecordDao";
    private SearchRecordOpenHelper mSearchRecordOpenHelper;
    private SQLiteDatabase mSQLiteDatabase;

    // 构造方法
    public SearchRecordDao(Context context) {
        // 数据库创建保存搜索记录的表
        mSearchRecordOpenHelper = new SearchRecordOpenHelper(context);
    }

    /**
     * 添加一条查询记录
     *
     * @param text 插入的数据
     */
    public boolean addSearchRecord(String text) {
        try {
            mSQLiteDatabase = mSearchRecordOpenHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("TEXT", text);
            long r = mSQLiteDatabase.insert("SEARCH_RECORD_TABLE", null, values);
            if (-1 == r) {
                // 插入失败
                Log.e(TAG, "查询数据插入失败");
                return false;
            } else {
                // 插入成功
                Log.e(TAG, "查询数据插入成功");
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "查询插入数据出现异常");
            return false;
        } finally {
            mSQLiteDatabase.close();
        }
    }

    /**
     * 删除一条记录
     *
     * @param text 要删除的数据
     */
    public boolean deleteSearchRecord(String text) {
        try {
            mSQLiteDatabase = mSearchRecordOpenHelper.getWritableDatabase();
            mSQLiteDatabase.delete("SEARCH_RECORD_TABLE", "TEXT = ?", new String[]{text});
            Log.e(TAG, "查询数据删除成功");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "查询删除数据出现异常");
            return false;
        } finally {
            mSQLiteDatabase.close();
        }
    }

    /**
     * 查询查询记录
     *
     * @return
     */
    public ArrayList<String> selectSearchRecord() {
        Cursor cursor = null;
        ArrayList<String> searchRecord = new ArrayList<String>();
        try {
            mSQLiteDatabase = mSearchRecordOpenHelper.getReadableDatabase();
            // 查询部分数据
            // String sql = "select text from SEARCH_RECORD_TABLE limit ? offset ?";
            // cursor = mSQLiteDatabase.rawQuery(sql, new String[]{selectCount + "", startIndex + ""});
            // 查询全部数据
            cursor = mSQLiteDatabase.query("SEARCH_RECORD_TABLE", new String[]{"text"}, null, null, null, null, "id desc");
            while (cursor.moveToNext()) {
                String text = cursor.getString(0);
                searchRecord.add(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭游标
            cursor.close();
            // 关闭数据库
            mSQLiteDatabase.close();

            return searchRecord;
        }
    }

    /**
     * 查询指定行数的数据
     *
     * @return
     */
    public ArrayList<String> querySearchRecord() {
        return innerSelectSearchRecordWithLimit(20 + "");
    }

    /**
     * 查询指定条数的查询记录
     *
     * @return
     */
    private ArrayList<String> innerSelectSearchRecordWithLimit(String limit) {
        Cursor cursor = null;
        ArrayList<String> searchRecord = new ArrayList<String>();
        try {
            mSQLiteDatabase = mSearchRecordOpenHelper.getReadableDatabase();
            // 查询部分数据
            // String sql = "select text from SEARCH_RECORD_TABLE limit ? offset ?";
            // cursor = mSQLiteDatabase.rawQuery(sql, new String[]{selectCount + "", startIndex + ""});
            // 查询全部数据
            cursor = mSQLiteDatabase.query("SEARCH_RECORD_TABLE", new String[]{"text"}, null, null, null, null, "id desc", limit);
            while (cursor.moveToNext()) {
                String text = cursor.getString(0);
                searchRecord.add(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭游标
            cursor.close();
            // 关闭数据库
            mSQLiteDatabase.close();

            return searchRecord;
        }
    }

    /**
     * 删除所有历史记录
     *
     * @return 删除是否成功
     */
    public boolean deleteAllSearchRecord() {
        try {
            mSQLiteDatabase = mSearchRecordOpenHelper.getWritableDatabase();
            mSQLiteDatabase.delete("SEARCH_RECORD_TABLE", null, null);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            mSQLiteDatabase.close();
        }
    }
}

查看数据库

Android存储之SQLite数据库

上一篇:Net设计模式实例之建造者模式(Builder Pattern)(2)


下一篇:谷粒商城分布式高级(六)—— 缓存-缓存使用(整合redis & 缓存穿透/雪崩/击穿 & 本地锁)