SQLite在Android中的典型用法

2016-04-10 08:33:11|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:Java, Android, 数据库|来源:唯设编程网

2. 创建DBManager类

为操作方便,我们将每个表对应的操作(包括增加、删除、修改、查询等)封装在一个适配器类中,这里仅仅哪生词表对应的适配器类来进行说明。

/**
 * 生词对应的数据库操作对象
 * Created by ygh on 2016/1/26.
 */
public class DBNewWordManager extends DBManager {
    public DBNewWordManager(Context context) {
        super(context);
        tag = this.getClass().getName();
    }

    @Override
    public void add(List<DataItem> items) {
        db.beginTransaction();  //开始事务
        try {
            for (DataItem item : items) {
                if (!(item instanceof NewWord))
                    continue;
                NewWord newWord = (NewWord) item;
                //生词
                db.execSQL("insert into glossary values (?,?,?,?,?,?);",
                        new Object[]{
                                newWord.getId(),
                                newWord.getMasterCode(),
                                newWord.getReviewCount(),
                                newWord.getQueryCount(),
                                newWord.getReviewTime(),
                                newWord.getAddTime()
                        });
            }
            db.setTransactionSuccessful();  //设置事务成功完成
        } catch (SQLiteException err) {
            Log.w(tag, err.getMessage());
        } finally {
            db.endTransaction();    //结束事务
        }
    }

    @Override
    public boolean add(DataItem item) {
        if (!(item instanceof NewWord))
            return false;

        NewWord newWord = (NewWord) item;
        if(newWord.getId() == -1) {
            Toast.makeText(_context,"对应生词没有初始化",Toast.LENGTH_LONG).show();
            return false;
        }

        //先进行查询,检查是否已经存在于数据库中
        query(newWord);
        if(newWord.isQuerySucceeded()){
            Toast.makeText(_context,"不用重复添加",Toast.LENGTH_LONG).show();
            return false;
        }

        try {
            //生词
            db.execSQL("insert into glossary values (?,?,?,?,?,?);",
                    new Object[]{
                            newWord.getId(),
                            newWord.getMasterCode(),
                            newWord.getReviewCount(),
                            newWord.getQueryCount(),
                            newWord.getReviewTime(),
                            newWord.getAddTime()
                    });
        } catch (SQLiteException err) {
            Log.w(tag, err.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public boolean delete(DataItem item) {
        if(!(item instanceof  NewWord))
            return false;

        NewWord newWord = (NewWord) item;
        try {
            db.execSQL("delete from glossary where ID=" + newWord.getId() + ";");
            return true;
        } catch (SQLiteException err) {
            Log.w(tag, err.getMessage());
            return false;
        }
    }

    @Override
    public void clearTables() {
        try {
            db.execSQL("delete from glossary");
        } catch (SQLiteException err) {
            Log.w(tag, err.getMessage());
        }
    }

    @Override
    public Cursor queryTheCursor() {
        Cursor c = db.rawQuery("SELECT b.word,a.ID,a.masterCode,a.reviewCount,a.queryCount,a.reviewTime,a.addTime FROM glossary as a inner join basic_info as b on a.ID=b.ID order by a.addTime DESC;", null);
        return c;
    }

    @Override
    public List<DataItem> query() {
        ArrayList<DataItem> items = new ArrayList<DataItem>();
        Cursor c = queryTheCursor();
        while (c.moveToNext()) {
            //基本解释
            NewWord item = new NewWord(c.getString(c.getColumnIndex("word")));
            item.setMasterCode(c.getInt(c.getColumnIndex("masterCode")));
            item.setReviewCount(c.getInt(c.getColumnIndex("reviewCount")));
            item.setQueryCount(c.getInt(c.getColumnIndex("queryCount")));
            item.setReviewTime(c.getString(c.getColumnIndex("reviewTime")));
            item.setAddTime(c.getString(c.getColumnIndex("addTime")));
            //设置查询状态
            item.setQuerySucceeded(true);

            //添加到列表中
            items.add(item);
        }
        c.close();

        return items;
    }

    @Override
    public void query(DataItem dataItem) {
        if(!(dataItem instanceof NewWord))
            return;

        NewWord newWord = (NewWord) dataItem;

        Integer wordID =  newWord.getId();
        String args[] = {wordID.toString()};

        Cursor c = db.rawQuery("SELECT * FROM glossary where ID = ?", args);
        if (c.moveToNext()) {
            newWord.setId(c.getInt(c.getColumnIndex("ID")));
            newWord.setMasterCode(c.getInt(c.getColumnIndex("masterCode")));
            newWord.setReviewCount(c.getInt(c.getColumnIndex("reviewCount")));
            newWord.setQueryCount(c.getInt(c.getColumnIndex("queryCount")));
            newWord.setReviewTime(c.getString(c.getColumnIndex("reviewTime")));
            newWord.setAddTime(c.getString(c.getColumnIndex("addTime")));
            //设置查询状态
            newWord.setQuerySucceeded(true);
        }
        c.close();
    }
}
发表评论0条 】
网友评论(共?条评论)..
SQLite在Android中的典型用法