Android SQLite 注意点(问题解决)

Android SQLite 注意点(问题解决)

注意:

一. sql语句

 String sql = "insert into " + TABLE_NAME
                + " ("
                + ID + ","
                + NAME + ","
                + TAG + ","
                + STATUS + ","
                + IMGPATH + ","
                + IMG
                + ")"
                + " values ("
                + getCursor().getCount() + ",\'"
                + name + "\',\'"
                + tag + "\',\'"
                + status + "\',\'"
                + imgPath + "\',\'"
                + img
                + "\')";

注意:这样写的sql语句 参数两边要加’ 例:‘参数’。转义 \’

二.Cursor(游标)的理解

 private List list = new ArrayList();
    public List<String> queryAllTag() {
        Cursor cursor = getCursor();
        if (cursor != null && cursor.moveToFirst()) {
            if (cursor.getCount() > 0) {
                    add(cursor);
                while (cursor.moveToNext()) {
                    add(cursor);
                }
            }

        }
        return list;
    }
private void add(Cursor cursor){
    String s = cursor.getString(cursor.getColumnIndex("Tag"));
    list.add(s);
}

注意:cursor.moveToFirst() 了之后就有第一条数据了,cursor.moveToNext() 换下一行。所以在 cursor.moveToFirst() 后记得有条数据(第一条)。

你可能感兴趣的:(知识点,问题解决)