创建一个android sqlite,其中包含一个只有一行的表
我想创建一个只有一行的android sqlite表,用于插入、更新和读取,我该怎么做,因为我只知道如何创建多行 该表仅包含一行,用于用户插入数据和存储,当读取数据时,数据将出来,最后一次更新意味着它将覆盖该行中的数据 这是我的DBHelperNote.java创建一个android sqlite,其中包含一个只有一行的表,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,我想创建一个只有一行的android sqlite表,用于插入、更新和读取,我该怎么做,因为我只知道如何创建多行 该表仅包含一行,用于用户插入数据和存储,当读取数据时,数据将出来,最后一次更新意味着它将覆盖该行中的数据 这是我的DBHelperNote.java public static final String TABLE_GOAL = "goal"; public static final String GOAL_ID= "goal_id"; public static
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;
}
这实际上是读取数据put的方法,但它来自数组,表示多行,但我不知道如何将其更改为只调用一行
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,
};
由于将插入目标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更改为replace,这将确保如果提供了主键值并且存在具有该id的行,那么它将替换现有行而不创建新行。最重要的是检查goal_id是否为null的if条件,如果为null,则不在contentvalue中提供该条件
适当修改if条件,以便对目标id进行适当比较,因为我刚刚使用了可以从您的问题内容中可视化的条件。使用RawQuery方法更容易一些 c1=db.rawQuery(“从表_名称中选择*”,null); c1.移动到第一个() 做{
使用相同的
GOAL\u ID
更新您的表,但是readGoal()如何,因为我的readGoal方法实际上是读取数组,但现在我只有一行,所以我可以更改代码?这不会有什么区别,因为它将读取游标中所有可用的行,所以如果一行将读取一行,如果n行则将读取n行。我得到这一行错误cv.put(DBHelperNote.GOAL_ID,);?这是“GOAL_ID”是吗?它是我使用的id的值,以便您可以用包含goal_id value.same error cv.put(DBHelperNote.goal_id,)的变量替换;,可以解析符号吗?是的,我知道原始查询更容易,但是当我读取所有需要放入文本以供查看的数据时,这取决于每个textview id,以及如何读取?然后直接使用下面的代码TextViewId.setText(c1.getString(c1.getColumnIndex(“FieldName”);无法获取您的意思textview t1=(textview)findViewByid(R.id.textview1);您已经在所有文本视图的代码中这样做了。然后只需像t1.setText(c1.getString(c1.getColumnIndex(“FieldName”))那样编写即可;
str = c1.getString(c1.getColumnIndex("FieldName"));
ab.add(str);
} while (c1.moveToNext());