【问题标题】:Create a android sqlite contain a table with only single row创建一个 android sqlite 包含一个只有单行的表
【发布时间】:2016-01-20 13:51:42
【问题描述】:

我想创建一个只有一行的 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,

        };

【问题讨论】:

  • 用同样的GOAL_ID更新你的表

标签: android sqlite android-sqlite


【解决方案1】:

由于您将插入 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);
    }

所做的修改是将插入更改为替换,这将确保如果提供了主键值并且行存在具有该 id 的行,那么它将替换现有行而不创建新行。最重要的是用于检查goal_id 是否为null 的if 条件,如果为null 则不要在contentvalue 中提供。

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

【讨论】:

  • 但是 readGoal() 怎么样,因为我的 readGoal 方法实际上是读取数组,但现在我只有 1 行,所以我可以更改我的代码?
  • 这不会有什么不同,因为它会读取游标中所有可用的行,所以如果一个则读取一个,如果 n 行则将读取 n 行
  • 我收到此行错误 cv.put(DBHelperNote.GOAL_ID, ); ?这个 是 ? “goal_id”是?
  • 是id的值。我使用了 以便您可以用包含goal_id 值的变量替换。
  • 同样的错误 cv.put(DBHelperNote.GOAL_ID,); , 可以解析符号 ?
【解决方案2】:

使用 RawQuery 方法更容易一些。

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

做{

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

【讨论】:

  • 是的,我知道原始查询更容易,但是当我读取所有需要放入文本以供查看的数据时,取决于每个 textview id,以及如何?
  • 然后直接使用下面的代码TextViewId.setText(c1.getString(c1.getColumnIndex("FieldName")));
  • 无法理解你的意思
  • TextView t1=(TextView)findViewByid(R.id.textview1);您已经在所有文本视图的代码中完成了此操作。然后就像 t1.setText(c1.getString(c1.getColumnIndex("FieldName")));
猜你喜欢
  • 2013-01-05
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 2010-09-24
  • 2016-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多