Java 似乎跳过了cursor.moveToFirst

Java 似乎跳过了cursor.moveToFirst,java,android,sql,database,cursor,Java,Android,Sql,Database,Cursor,我是Java新手,只是尝试创建一个数据库。我设法制作了一个DB,但当我想读取值时,它似乎会出错 这是“我的设置”活动的代码,该活动要求设置值并将它们添加到特定ID的DB中 public class Settings extends Activity{ Button Save; static Switch SwitchCalculations; public static String bool; public static List<Integer>

我是Java新手,只是尝试创建一个数据库。我设法制作了一个DB,但当我想读取值时,它似乎会出错

这是“我的设置”活动的代码,该活动要求设置值并将它们添加到特定ID的DB中

public class Settings extends Activity{

    Button Save;
    static Switch SwitchCalculations;
    public static String bool;
    public static List<Integer> list_id = new ArrayList<Integer>();
    public static List<String> list_idname = new ArrayList<String>();
    public static List<String> list_kind = new ArrayList<String>();
    public static List<String> list_value = new ArrayList<String>();
    static Integer[] arr_id;
    static String[] arr_idname;
    static String[] arr_kind;
    static String[] arr_value;
    public static final String TAG = "Settings";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);

        Save = (Button) findViewById(R.id.btnSave);
        SwitchCalculations = (Switch) findViewById(R.id.switchCalcOnOff);

        readData();

        Save.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                        writeData();
                        //Toast.makeText(this, "Data has been saved.", Toast.LENGTH_SHORT).show();
                        readData();

                    Save.setText("Opgeslagen");
            }
        });
    }
    public void writeData() {
            int id = 1;
            String idname = "switchCalcOnOff";
            String kind = "switch";
            boolean val = SwitchCalculations.isChecked();
            String value = new Boolean(val).toString();

            dbHelper_Settings dbh = new dbHelper_Settings(this);
            SQLiteDatabase db = dbh.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(dbh.C_ID, id);
            cv.put(dbh.C_IDNAME, idname);
            cv.put(dbh.C_KIND, kind);
            cv.put(dbh.C_VALUE, value);

            if (dbh.C_ID.isEmpty() == true) {
                db.insert(dbh.TABLE, null, cv);
                Log.d(TAG, "Insert: Data has been saved.");
            } else if (dbh.C_ID.isEmpty() == false) {
                db.update(dbh.TABLE, cv, "n_id='1'", null);
                Log.d(TAG, "Update: Data has been saved.");
            } else {
                Log.d(TAG, "gefaald");
            }
            db.close();
    }
    public void readData() {
        dbHelper_Settings dbh = new dbHelper_Settings(this);
        SQLiteDatabase db = dbh.getWritableDatabase();
        List<String> list_value = new ArrayList<String>();
        String[] arr_value;
        list_value.clear();

        Cursor cursor = db.rawQuery("SELECT " + dbh.C_VALUE + " FROM " + dbh.TABLE + ";", null);

        if (cursor.moveToFirst()) {
        do {
            list_value.add(cursor.getString(0));
        } while (cursor.moveToNext());
        }

        if (cursor != null && !cursor.isClosed()){

            cursor.close();
        }
        db.close();
        arr_value = new String[list_value.size()];

        for (int i = 0; i < list_value.size(); i++){
            arr_value[i] = list_value.get(i);
        }
    }
}
奇怪的是我没有收到任何错误信息。 但我使用Log.dTAG文本检查脚本被跳过的位置,即cursor.moveToFirst


有人能帮我解决这个问题吗?

这里,与您似乎期望的相反,您实际上检查了文本常量是否为空:

if (dbh.C_ID.isEmpty() == true) {
它不是:它总是包含n\u id

我认为您的目的是查找具有该id的记录,并根据结果进行插入或更新

您应该这样做:尝试通过helper进行选择,然后按照上面的代码插入或更新

编辑:

向您的助手添加如下内容:

public boolean someRowsExist(SQLiteDatabase db) {
    Cursor cursor = db.rawQuery("select EXISTS ( select 1 from " + TABLE + " )", new String[] {});
    cursor.moveToFirst();
    boolean exists = (cursor.getInt(0) == 1);
    cursor.close();
    return exists;
}
并使用它检查数据库中是否有任何行:

if (dbh.someRowsExist(db)) {  // instead of (dbh.C_ID.isEmpty() == true) {

看来您在调试查询时遇到了问题。Android提供了一种方便的方法,可以将整个光标格式化为字符串。然后,您可以将转储输出到LogCat,并查看是否有任何行被实际跳过。

如何解决此问题?如何检查数据库行是否存在?我可以用一些简单的东西,比如db.C_ID.contains1吗?因为你是新手,所以从正确的方式开始很重要。这是通过确保您真正理解每一行代码。只是试着把一些碎片拼凑起来,这是不可能的。当你知道的时候,你就有能力提出正确的问题。例如,谷歌搜索android sqlite,检查是否存在记录,只会给你几个链接。但只有当你真正了解发生了什么时,你才能问这些问题。请看我的编辑上面;希望能有帮助。这几乎是你提供给我的完美代码。在someRowsExists中,设置光标后,您应该执行cursor.moveToFirst;发布的很多东西都是一种伪代码,就像上面我的一样:。这就是为什么回顾每一件事很重要的原因——正是你所做的,谢谢,编辑了上面的内容。
if (dbh.someRowsExist(db)) {  // instead of (dbh.C_ID.isEmpty() == true) {