本文介绍了SQLite的查询返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所做


我有一个充满了我的imageID的数据库的GridView。现在我设置好的了,我得到了一个imageID onItemClickListner - >这个作品!现在,我想我的搜索数据库与imageID,但它永诺返回空,但如果与那些imageID的充满了我的DB正是应该不会是因为。
表的结构为:的RowID |来源|信息
在信息存储我的imageID的


我如何要改变光标,我可以克服信息的条目。
到这里,你找到我的光标的code:

code:


  //居wird NACH德宫ID笑脸gesucht。
    公共光标getSmiley(字符串信息)抛出的SQLException {        光标mCursor =        this.mDb.query(真,DATABASE_TABLE,新的String [] {信息,来源,
                },INFO +=+信息,NULL,NULL,NULL,NULL,NULL);
        如果(mCursor!= NULL){
            mCursor.moveToFirst();
        }
        返回mCursor;
    }

我如何获得imageID回


 公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
    Toast.makeText(SFilterConfigActivity.this,+位置,Toast.LENGTH_SHORT).show();
    //海尔wird herrausgefunden welche ID DAS画报帽子书房jeweiligen字符串gespeichert UND
    来源=将String.valueOf(v.getId());
    Log.v(IMAGEID,源);
    SmileyHelper.open();    光标C = SmileyHelper.getSmiley(源);
    startManagingCursor(C);        如果(C!= NULL){
            字符串的大小=将String.valueOf(c.getCount());
            Log.v(WhatsInC大小);
        }
    如果(c.moveToFirst()){
        做{
        文字= c.getString(c.getColumnIndex(SmileyDBAdapter.SOURCE));
        Log.v(WHATINTEXT,文本);
        笑脸= c.getString(c.getColumnIndex(SmileyDBAdapter.INFO));
        Log.v(WHATINSMILEY笑脸);
        }而(c.moveToNext());
    SmileyHelper.close();


其他信息:


  1. WhatIsInC,返回0。

如果你需要更多的code,只是告诉我
THX对您的帮助提前!
最好的祝福
野生动物园


解决方案

 公共光标getEmpByDept(字符串部)
  {
   SQLiteDatabase分贝= this.getReadableDatabase();
   的String [] =列新的String [] {_ ID,COLNAME,colAge,colDeptName};
   光标C = db.query(viewEmps,列colDeptName +=?,
    新的String [] {}系,NULL,NULL,NULL);
   返回℃;
  }

的db.query具有以下参数:

  1.String表名:表的运行对查询的名称
 2.String []列:查询的投影,即,列来检索
 3.String WHERE子句:WHERE子句中,如果没有通过空
 4.String []选择ARGS:WHERE子句中的参数
 5.String组方:字符串by子句指定组
 6.String有:一个字符串,指定HAVING子句
 7.String排序方式:字符串排序BY子句

那么,在您的查询,

  this.mDb.query(DATABASE_TABLE,新的String [] {INFO,来源},INFO +='+信息+',NULL,NULL,NULL,NULL , 空值);

  this.mDb.query(DATABASE_TABLE,新的String [] {INFO,来源},INFO +=?,新的String []
{资讯},NULL,NULL,NULL,NULL);

What I've done


I have a Gridview which is filled over my Database with the imageID's. Now I setted up a onItemClickListner with that I get the imageID -> this works!. Now I'd like to search my DB with that imageID, but it allways returns "null", but that shouldn't be because if filled my DB exactly with those imageID's.The Structure of the Table is that: RowID | SOURCE | INFOIn Info i stored the imageID's

Question


How do I have to change Cursor that I can get the entries over INFO.Down here you find the Code of my Cursor:

Code:


    //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;
    }

How I get the 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();


Additional Information:

  1. WhatIsInC, returns "0".

If you need more Code, just tell meThx for your help in advance!Best Regardssafari

解决方案
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;
  }

The db.query has the following parameters:

 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

So, in your query,

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

or

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

这篇关于SQLite的查询返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 09:40