1

Right now I want to save my setting data using SQLite but no matter what I do, the cursors always returning null. In case the database is empty i have put the code:

if (sqlHelper.getProfilesCount(MySQLHelper.TABLE_SETTING_NAME) == 0){
        sqlHelper.insertSettingNote(Constants.STR_LANG,Constants.STR_LOC,Constants.STR_SENS);
    }

but it doesn't help. Neither rawQuery nor query help me read the file. Can you help me, please? Here is the code in MySQLHelper:

public SettingNote getSettingNote(String sql) {
    SettingNote stnote = null;
    Cursor cursor = getAll(sql);
    if (cursor != null) {
        stnote = cursorToSettingNote(cursor);
        cursor.close();
    }
    return stnote;
}

    public Cursor getAll(String sql) { // It always returning null
    Cursor cursor = null;
    try {
        //cursor = db.rawQuery("SELECT * FROM" + sql,null); // I try using both rawQuery and query
        String selectQuery = sql;
        String[] whereArgs = new String[] {"hn", "vi", "no"};
        cursor = db.query(selectQuery, null, "loc = ? lang = ? sensitive = ?", whereArgs , null, null, null);

        if (cursor != null && cursor.getCount() > 0) {
            if (cursor.moveToFirst()) {
                do {
                    //Looping cursor and fetching the value...
                } while (cursor.moveToNext());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
    }
    return cursor;
}

and here is my SettingNote:

public class SettingNote {
int id;
String lang, loc, sensitive;

public SettingNote(int id, String lang, String loc, String sensitive) {
    this.id = id;
    this.lang = lang;
    this.loc = loc;
    this.sensitive = sensitive;
}
public SettingNote(){};

public void setSensitive(String sensitive) {this.sensitive = sensitive;}

public void setLoc(String loc) {this.loc = loc;}

public void setLang(String lang) {this.lang = lang;}

public int getId() {return id;}

public String getLang() {return lang;}

public String getLoc() {return loc;}

public String getSensitive() {return sensitive;}

public void setId(int id) {this.id = id;}

}

  • Can u post insertSettingNote() method? – Raghavendra Nov 14 '16 at 06:36
  • if it returns null, then you should get something in the `e.printStackTrace();`. Post a stacktrace here – Vladyslav Matviienko Nov 14 '16 at 07:02
  • Ok. Here is my insertSettingNote: `public boolean insertSettingNote(String lang, String loc, String sens) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(NOTE_SETTING_LANG,lang); contentValues.put(NOTE_SETTING_LOC, loc); contentValues.put(NOTE_SETTING_SENS, sens); try { db.insert(TABLE_SETTING_NAME, null, contentValues); return true; } catch (Exception e) { return false; } }` – Việt Tùng Nguyễn Nov 14 '16 at 07:46

0 Answers0