Check if Query in sqlite is empty in a bindView method

269 views Asked by At

How to check if data queried from android sqlite database is empty in a bindView method?

This is what I have done so far, but I think I am doing the wrong thing.

UPDATE

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

     SQLiteDatabase db;

        db = openOrCreateDatabase(
            "no.db"
            , SQLiteDatabase.CREATE_IF_NECESSARY
            , null
            );
      //CREATE TABLES AND INSERT MESSAGES INTO THE TABLES
        String CREATE_TABLE_NOTICES = "CREATE TABLE IF NOT EXISTS notices ("
                + "ID INTEGER primary key AUTOINCREMENT,"
                + "NOT TEXT,"
                + "DA TEXT)";
        db.execSQL(CREATE_TABLE_NOTICES);

        Cursor myCur = null;
   // SQLiteDatabase db;
    myCur = db.rawQuery("SELECT ID as _id,NOT,DA FROM notices order by ID desc", null);


    mListAdapter = new MyAdapter(Page.this, myCur);
    setListAdapter(mListAdapter);

}

    @Override
    public void bindView(View view, Context context, Cursor myCur) {
        TextView firstLine=(TextView)view.findViewById(R.id.firstLine);

        if(myCur==null){
                String message="No Message";
                firstLine.setText(message);
        }
    }
1

There are 1 answers

1
spencer7593 On

The keyword NOT is a reserved word in MySQL. I'd be very surprised if it's not a reserved word in SQLite.

I think the problem is here:

  SELECT ID as _id,NOT,DA FROM notices order by ID desc
  --               ^^^

It looks like the reserved word NOT is being used as a column name. To use that as an identifier, it will need to be "escaped", by enclosing it in backtick characters.

  SELECT ID as _id,`NOT`,DA FROM notices ORDER BY ID DESC
  --               ^   ^

That's the normative pattern in MySQL. For SQLite, you use the (MySQL compatible ) backtick characters, or you can use double quotes (more ANSI standard compliant.)

It's also possible to qualify the column name. (I'm certain this works in MySQL).

  SELECT n.ID AS `_id`, n.NOT, n.DA FROM notices n ORDER BY n.id DESC
  --     ^^             ^^     ^^                ^          ^^  

References:

MySQL Reference Manual: 9.3 Keywords and Reserved Words https://dev.mysql.com/doc/refman/5.5/en/keywords.html

Certain keywords, such as SELECT, DELETE, or BIGINT, are reserved and require special treatment for use as identifiers such as table and column names.

SQLite: SQLite Keywords https://www.sqlite.org/lang_keywords.html

If you want to use a keyword as a name, you need to quote it. There are four ways of quoting keywords in SQLite:


It may also be a problem in the CREATE TABLE statement. Since you are creating the table, you have the option of using a different name for the column, a name which is not a reserved word.