【问题标题】:SQL : Query() where COL = int instead of string ? (pictures + code inside)SQL:Query() where COL = int 而不是 string ? (图片+内码)
【发布时间】:2013-02-19 08:44:38
【问题描述】:

这是对我的问题的解释。
应用程序描述:我希望应用程序根据用户单击的按钮显示具有不同内容的列表视图。为此,在我的数据库中有一个带有数字(1 或 2)的列,我希望单击按钮 1 仅显示该数字为 1 的元素,依此类推。

A. 当用户点击一个按钮(称为button1)时,它会打开一个新活动(ResultListViewActivity)并通过putExtra 发送一个名为“myVariable”的整数。

private OnClickListener listener_button1 = new OnClickListener() {
    public void onClick(View v) {
        Intent t = new Intent(MainActivity.this,
                ResultListViewActivity.class);
        t.putExtra("myVariable", 1);
        startActivity(t);

    }

};

B. 在 ResultListViewActivity 中,有一个方法(findNameInTable)使用这个整数作为输入:

private void displayListView() {

    Bundle bundle = getIntent().getExtras();
    int myVariable = bundle.getInt("myVariable");

    Cursor c = dbHelper.findNameInTable(myVariable);
    // the desired columns to be bound
    String[] columns = new String[] { DatabaseAdapter.COL_NAME,
            DatabaseAdapter.COL_COMMENTS, };
    // the XML defined views which the data will be bound to
    int[] to = new int[] { R.id.name, R.id.comments, };
    // create the adapter using the cursor pointing to the desired data
    // as well as the layout information
    cursorAdapter = new SimpleCursorAdapter(this, R.layout.poi_info, c,
            columns, to, 0);

    ListView listView = (ListView) findViewById(R.id.listview);
    // Assign adapter to ListView
    listView.setAdapter(cursorAdapter);
}

C. 这个方法 findNameInTable() 在我的 DatabaseAdapter 类中定义如下:

    public Cursor findNameInTable(int myVariable) {
    c = myDatabase
            .query(DATABASE_TABLE, new String[] { COL_NAME , COL_COMMENTS }, COL_CAT1 = myVariable,
                    new String[] { Integer.toString(myVariable) }, null,
                    null, null);
    return c;
} 

我希望此方法返回一个游标,其中包含 COL_NAME(列名)和 COL_COMMENTS 的内容,用于数据库的每一行,其中 COL_CAT1 = myVariable(例如,如果我单击 button1,则为 1)。

我尝试使用 Integer.toString(myVariable) 将我的 int 转换为字符串,但它仍然无法正常工作。此外,在我的活动之上,我将 COL_CAT1 定义如下:

    public static final String COL_CAT1 = "cat1";

我真的希望它足够清楚,有人会帮助我完成这项工作,我真的开始对这件事感到绝望......

提前致谢!

编辑:发布代码而不是图片

【问题讨论】:

  • 您确实意识到没有人愿意重新输入您的代码,并且通过使用屏幕截图而不是文本,您几乎是在强迫他们这样做,是吗?
  • 抱歉,编辑了我的帖子。
  • @Phalanx 使用 COL_CAT1+"=?"而不是 COL_CAT1=myVariable
  • @JaiSoni :它有效,没有更多错误消息。我的应用程序仍然无法运行,但它可能来自其他原因,非常感谢!

标签: android sql database integer


【解决方案1】:

将您的查询更改为:

 c=mydatabase.query(
            DATABASE_TABLE,
            new String[] { COL_NAME,COL_COMMENTS },
            COL_CAT1 + "=?" ,
            new String[] { Integer.toString(myVariable) }, null, null, null
        );

【讨论】:

    猜你喜欢
    • 2010-11-13
    • 2021-10-12
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 2021-09-14
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多