LouSQLite,SQLite数据库封装 - lyloou/lou GitHub Wiki

SQLite简介

使用场合

用来存储数据量大,结构性复杂的数据;

数据类型

TYPE 描述
integer 整型
real 浮点型
text 文本型
blob 二进制类型

note:

  • 对于图片,音乐,视频等二进制文件,虽然也可以用blob来存储,但是更推荐存储其文件路径(Android 保存图片到SQLite)。
  • 使用事务来保证让一系列的操作要么全部完成,要么一个都不会完成。(《第一行代码》第六章p261)

LouSQLite 使用方法介绍

初始化

LouSQLite.init(mContext, MyCallBack.getInstance());
Phrase phrase = new Phrase("青青子衿,悠悠我心");

插入一个数据到数据库

LouSQLite.insert(MyCallBack.TABLE_PHRASE, phrase);

插入一组数据

List<Phrase> lists =  Arrays.asList(
        new Phrase("窈窕淑女,君子好逑"),
        new Phrase("海上生明月,天涯共此时"),
        new Phrase("青青子衿,悠悠我心"),
        new Phrase("人生若只如初见")
);
LouSQLite.insert(MyCallBack.TABLE_PHRASE, lists);

更新到数据库

LouSQLite.update(MyCallBack.TABLE_PHRASE, phrase, MyCallBack.KEY_PHRASE_ID + "=?", new String[]{phrase.getId()});

查找

List<Phrase> lists = LouSQLite.query(MyCallBack.TABLE_PHRASE, "select * from " + MyCallBack.TABLE_PHRASE, null);

从数据库中删除

LouSQLite.delete(MyCallBack.TABLE_PHRASE, MyCallBack.KEY_PHRASE_ID + "=?", new String[]{phrase.getId()});

数据库配置

/**
 * 类描述:
 * 创建人: Lou
 * 创建时间: 2016/7/13 14:22
 * 修改人: Lou
 * 修改时间:2016/7/13 14:22
 * 修改备注:
 */
public class MyCallBack implements LouSQLite.ICallBack {
    private static final String TAG = "MyCallBack";

    private MyCallBack() {
    }

    private static LouSQLite.ICallBack INSTANCE;

    public static LouSQLite.ICallBack getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new MyCallBack();
        }
        return INSTANCE;
    }

    ///////////////////////////////////////////////////////////////////////////
    // db config
    ///////////////////////////////////////////////////////////////////////////
    public static final String DB_NAME = "DB_NAME.db";
    public static final int DB_VERSION = 1;


    ///////////////////////////////////////////////////////////////////////////
    // table phrase
    ///////////////////////////////////////////////////////////////////////////
    public static final String TABLE_PHRASE = "phrase";
    public static final String KEY_PHRASE_ID = "PHRASE_ID";
    private static final String KEY_PHRASE_CONTENT = "PHRASE_CONTENT";
    private static final String KEY_PHRASE_FAVORITE = "PHRASE_FAVORITE";
    private static final String TABLE_PHRASE_SQL = "create table " + TABLE_PHRASE + " (" +
            "id integer primary key autoincrement, " +
            KEY_PHRASE_ID + " text, " +
            KEY_PHRASE_CONTENT + " text, " +
            KEY_PHRASE_FAVORITE + " integer" +
            ")";


    ///////////////////////////////////////////////////////////////////////////
    // table favorite
    ///////////////////////////////////////////////////////////////////////////
    public static final String TABLE_FAVORITE = "favorite";
    public static final String KEY_FAVORITE_ID = "FAVORITE_ID";
    private static final String KEY_FAVORITE_CONTENT = "FAVORITE_CONTENT";
    private static final String KEY_FAVORITE_FAVORITE = "FAVORITE_FAVORITE";
    private static final String TABLE_FAVORITE_SQL = "create table " + TABLE_FAVORITE + " (" +
            "id integer primary key autoincrement, " +
            KEY_FAVORITE_ID + " text, " +
            KEY_FAVORITE_CONTENT + " text," +
            KEY_FAVORITE_FAVORITE + " integer" +
            ")";


    ///////////////////////////////////////////////////////////////////////////
    // overrite
    ///////////////////////////////////////////////////////////////////////////
    @Override
    public List<String> createTablesSQL() {
        return Arrays.asList(
                TABLE_PHRASE_SQL,
                TABLE_FAVORITE_SQL
        );
    }

    @Override
    public String getName() {
        return DB_NAME;
    }

    @Override
    public int getVersion() {
        return DB_VERSION;
    }

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

        switch (oldVersion) {
            case 0:
                db.execSQL(TABLE_FAVORITE_SQL); // 升级操作;
            case 1:
                break;
            default:
                break;
        }
    }


    @Override
    public <T> void assignValuesByInstance(String tableName, T t, ContentValues values) {

        switch (tableName) {
            case TABLE_PHRASE:
                if (t instanceof Phrase) {
                    Phrase phrase = (Phrase) t;
                    values.put(KEY_PHRASE_ID, phrase.getId());
                    values.put(KEY_PHRASE_CONTENT, phrase.getContent());
                    values.put(KEY_PHRASE_FAVORITE, phrase.getFavorite());
                }
                break;
            case TABLE_FAVORITE:
                if (t instanceof Phrase) {
                    Phrase phrase = (Phrase) t;
                    values.put(KEY_FAVORITE_ID, phrase.getId());
                    values.put(KEY_FAVORITE_CONTENT, phrase.getContent());
                    values.put(KEY_FAVORITE_FAVORITE, phrase.getFavorite());
                }
                break;
        }
    }

    @Override
    public Object newInstanceByCursor(String tableName, Cursor cursor) {
        switch (tableName) {
            case TABLE_PHRASE:
                return new Phrase(
                        cursor.getString(cursor.getColumnIndex(KEY_PHRASE_ID)),
                        cursor.getString(cursor.getColumnIndex(KEY_PHRASE_CONTENT)),
                        cursor.getInt(cursor.getColumnIndex(KEY_PHRASE_FAVORITE))
                );
            case TABLE_FAVORITE:
                return new Phrase(
                        cursor.getString(cursor.getColumnIndex(KEY_FAVORITE_ID)),
                        cursor.getString(cursor.getColumnIndex(KEY_FAVORITE_CONTENT)),
                        cursor.getInt(cursor.getColumnIndex(KEY_FAVORITE_FAVORITE))
                );
        }

        return null;
    }
}
⚠️ **GitHub.com Fallback** ⚠️