检查bindView方法中sqlite中的Query是否为空

时间:2015-06-12 18:08:34

标签: android sqlite android-cursoradapter

如何在bindView方法中检查从android sqlite数据库查询的数据是否为空?

这是我到目前为止所做的,但我认为我做错了。

更新

@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 个答案:

答案 0 :(得分:0)

关键字 NOT 是MySQL中的保留字。如果它不是SQLite中的保留字,我会感到非常惊讶。

我认为问题在于:

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

看起来保留字 NOT 被用作列名。要将其用作标识符,需要通过将其包含在反引号字符中来转义"转义"。

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

这是MySQL中的规范模式。对于SQLite,您使用(MySQL兼容的)反引号字符,或者您可以使用双引号(更符合ANSI标准。)

也可以限定列名。 (我确定这适用于MySQL)。

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

参考文献:

MySQL参考手册:9.3 Keywords and Reserved Words https://dev.mysql.com/doc/refman/5.5/en/keywords.html

  

某些关键字(例如SELECTDELETEBIGINT 保留,需要特殊处理才能用作表名和列名等标识符

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

  

如果要使用关键字作为名称,则需要引用它。 SQLite中有四种引用关键字的方法:

CREATE TABLE语句中也可能存在问题。由于您正在创建表,因此可以选择为列使用不同的名称,该名称​​不是保留字。