android - SQLite 查询返回空

标签 android sqlite cursor

我做了什么


我有一个 Gridview,它在我的数据库中填充了图像 ID。现在我设置了一个 onItemClickListner,我得到了 imageID -> 这有效!。现在我想用那个 imageID 搜索我的数据库,但它总是返回“null”,但这不应该是因为如果用这些图像 ID 完全填充我的数据库。 表的结构是:RowID |来源 |信息 在 Info 中,我存储了 imageID 的

问题


我必须如何更改 Cursor 才能通过 INFO 获取条目。 在这里你可以找到我的光标代码:

代码:


    //Es wird nach der ID des smiley gesucht.
    public Cursor getSmiley(String info) throws SQLException {

        Cursor mCursor =

        this.mDb.query(true, DATABASE_TABLE, new String[] { INFO, SOURCE,
                }, INFO + "=" + info, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

我如何获得 imageID-back


    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    Toast.makeText(SFilterConfigActivity.this, "" + position, Toast.LENGTH_SHORT).show();


    //Hier wird herrausgefunden welche ID das Bild hat und in den jeweiligen String gespeichert
    source = String.valueOf(v.getId());
    Log.v("IMAGEID", source);
    SmileyHelper.open();

    Cursor c = SmileyHelper.getSmiley(source);
    startManagingCursor(c);

        if (c != null){
            String size = String.valueOf(c.getCount());
            Log.v("WhatsInC", size);
        }


    if (c.moveToFirst()) { 
        do { 
        text = c.getString(c.getColumnIndex(SmileyDBAdapter.SOURCE));
        Log.v("WHATINTEXT", text);
        smiley = c.getString(c.getColumnIndex(SmileyDBAdapter.INFO)); 
        Log.v("WHATINSMILEY", smiley);
        } while (c.moveToNext());
    SmileyHelper.close();

附加信息:

  1. WhatIsInC,返回“0”。

如果您需要更多代码,请告诉我 感谢您提前提供帮助! 此致 Safari

最佳答案

public Cursor getEmpByDept(String Dept)
  {
   SQLiteDatabase db=this.getReadableDatabase();
   String [] columns=new String[]{"_id",colName,colAge,colDeptName};
   Cursor c=db.query(viewEmps, columns, colDeptName+"=?", 
    new String[]{Dept}, null, null, null);
   return c;
  }

db.query 具有以下参数:

 1.String Table Name: The name of the table to run the query against
 2.String [ ] columns: The projection of the query, i.e., the columns to retrieve
 3.String WHERE clause: where clause, if none pass null
 4.String [ ] selection args: The parameters of the WHERE clause
 5.String Group by: A string specifying group by clause
 6.String Having: A string specifying HAVING clause
 7.String Order By by: A string Order By by clause

因此,在您的查询中,

this.mDb.query(DATABASE_TABLE, new String[] { INFO, SOURCE },INFO + " = '" + info + "'" , null, null, null, null, null);

this.mDb.query(DATABASE_TABLE,new String[] {INFO, SOURCE},INFO + " = ?",new String[]
{info},null, null, null, null);

关于android - SQLite 查询返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7754146/

相关文章:

sqlite - Cordova /Phonegap ORM?

c++ - Windows - SDL_PumpEvents 重置为系统光标

Java:比较方法违反了其一般契约

java - 在java中创建按钮

python - SQL在特定列中插入多个数据(python)

javascript - WordPress 网站的光标编码

javascript - 单击时旋转/动画光标 -45 度?

android - ListView 内的 map fragment ?

java - 如何在onResume内判断设备是否已从 sleep 模式唤醒

android - 如何获取Android中的所有列?