创建一个android sqlite包含一个只有单行的表

时间:2016-01-20 04:59:00

标签: android sqlite android-sqlite

我想创建一个只有一行的android sqlite表,用于插入或更新和读取,我该怎么做,因为我只知道如何创建多行

该表只包含1个单行,用于用户插入数据和存储,当读取数据时会出现,最后更新意味着它将覆盖该行中的数据。

这是我的DBHelperNote.java

 public static final String TABLE_GOAL = "goal";
    public static final String GOAL_ID= "goal_id";
    public static final String GENDER = "gender";
    public static final String AGE = "age";
    public static final String HEIGHT = "height";
    public static final String WEIGHT = "weight";
    public static final String GOAL = "goal";

        private static final String CREATE_TABLE_GOAL = "CREATE TABLE " + TABLE_GOAL + " (" +
                    GOAL_ID + " INTEGER PRIMARY KEY, " +
                    GENDER + " text not null, " +
                    AGE + " text not null, "+
                    HEIGHT + " text not null, "+
                    WEIGHT + " text not null, "+
                    GOAL + " text not null "+
                    " );";

这是SQLControlerWeight.java

public void insertGoal(String gd,String age,String hg,String wg,,String gl) {
        ContentValues cv = new ContentValues();
        cv.put(DBHelperNote.GENDER, gd);
        cv.put(DBHelperNote.AGE, age);
        cv.put(DBHelperNote.HEIGHT, hg);
        cv.put(DBHelperNote.WEIGHT, wg);
        cv.put(DBHelperNote.GOAL, gl);
        database.insert(DBHelperNote.TABLE_GOAL, null, cv);
    }

    public Cursor readGoal() {
        String[] allColummn = new String[] {
                DBHelperNote.GOAL_ID,
                DBHelperNote.GENDER,
                DBHelperNote.AGE,
                DBHelperNote.HEIGHT,
                DBHelperNote.WEIGHT,
                DBHelperNote.GOAL,
        };
        Cursor c = database.query(DBHelperNote.TABLE_GOAL, allColummn, null,
                null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

这实际上是读取数据的方法,但它是从数组意味着多行,但我不知道如何将其更改为只调用一行

dbconnection = new SQLControlerWeight(this);
        dbconnection.openDatabase();

        Cursor cursor = dbconnection.readGoal();

        String[] from = new String[]{
                DBHelperNote.GOAL_ID,
                DBHelperNote.GENDER,
                DBHelperNote.AGE,
                DBHelperNote.HEIGHT,
                DBHelperNote.WEIGHT,
                DBHelperNote.GOAL,
        };
        int[] to = new int[]{
                R.id.goal_id,
                R.id.field_gender,
                R.id.field_age,
                R.id.field_height,
                R.id.field_weight,
                R.id.field_goal,

        };

2 个答案:

答案 0 :(得分:0)

由于您将插入GOAL_ID,您可以修改插入函数

public void insertGoal(String goal_id, String gd,String age,String hg,String wg,,String gl) {
        ContentValues cv = new ContentValues();
        if (goal_id != null){
            cv.put(DBHelperNote.GOAL_ID, goal_id);
        }
        cv.put(DBHelperNote.GENDER, gd);
        cv.put(DBHelperNote.AGE, age);
        cv.put(DBHelperNote.HEIGHT, hg);
        cv.put(DBHelperNote.WEIGHT, wg);
        cv.put(DBHelperNote.GOAL, gl);
        database.replace(DBHelperNote.TABLE_GOAL, null, cv);
    }

修改完成后更改insert to replace,这将确保提供主键值并且行存在该id,然后它将替换现有行而不创建新行。最重要的是用于检查goal_id是否为null的if条件,如果为null则不在contentvalue中提供。

正确修改if条件以正确比较Goal_id,因为我刚刚使用了我可以从您的问题内容中看到的那个。

答案 1 :(得分:0)

使用RawQuery方法稍微容易一些。

c1 = db.rawQuery(“select * from Table_Name”,null); c1.moveToFirst();

做{

 str = c1.getString(c1.getColumnIndex("FieldName"));
 ab.add(str);
} while (c1.moveToNext());