2016-06-29 112 views
0

我不知道爲什麼我的方法來搜索我的SQLite數據庫並返回一個遊標返回null。我已經從txt文件加載數據庫,並試圖在應用程序中實現搜索功能時,我試圖查詢相同的數據庫以返回可能匹配的列表。然而,當我追溯錯誤時,我意識到這個方法一直返回null而不是我想要的遊標。誰能幫我這個?我很樂意承認我對此很新,並希望得到任何幫助。爲什麼我的SQLite數據庫查詢返回null?

這裏是我的數據庫表,用方法getWordMatches()即返回null:

public class DatabaseTable { 

    public static final String TAG = "ConstantDatabase"; 

    //the columns included in the table 
    public static final String COL_QUANTITY = "QUANTIY"; 
    public static final String COL_VALUE = "VALUE"; 
    public static final String COL_UNCERTAINTY = "UNCERTAINTY"; 
    public static final String COL_UNIT = "UNIT"; 
    public static final String _id = "_id"; 

    private static final String DATABASE_NAME = "CONSTANTS"; 
    private static final String FTS_VIRTUAL_TABLE = "FTS"; 
    private static final int DATABASE_VERSION = 1; 

    private final DatabaseOpenHelper mDatabaseOpenHelper; 

    public DatabaseTable(Context context){ 
     mDatabaseOpenHelper = new DatabaseOpenHelper(context); 
    } 

    private static class DatabaseOpenHelper extends SQLiteOpenHelper { 

     private final Context mHelperContext; 
     private SQLiteDatabase mDatabase; 

     private static final String FTS_TABLE_CREATE = 
       "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE + 
         " USING fts3 (" +_id+ ", "+ 
         COL_QUANTITY + ", " + 
         COL_VALUE + "," + 
         COL_UNCERTAINTY + "," + 
         COL_UNIT + ")"; 

     public DatabaseOpenHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
      Log.e("Database Operation", "DatabaseOpenHelper constructor called"); 
      mHelperContext = context; 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      mDatabase = db; 
      mDatabase.execSQL(FTS_TABLE_CREATE); 
      Log.e("Database Operation", "Constants Table Created ..."); 
      loadConstants(); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
        + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE); 
      onCreate(db); 
     } 

     public SQLiteDatabase getmDatabase(){ 
      return mDatabase; 
     } 


//  populating the virtual table with a string reading code 

     private void loadConstants() { 
      new Thread(new Runnable() { 
       public void run() { 
        try { 
         loadConstantss(); 
        } catch (IOException e) { 
         throw new RuntimeException(e); 
        } 
       } 
      }).start(); 

      Log.e("Loading", "Constants Table Populated ..."); 
     } 

     private void loadConstantss() throws IOException { 
      final Resources resources = mHelperContext.getResources(); 
      InputStream inputStream = resources.openRawResource(R.raw.txt); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 

      try { 
       String line; 
       while ((line = reader.readLine()) != null) { 
        String[] strings = TextUtils.split(line, ","); 
        if (strings.length < 4) continue; 
        long id = addConstant(strings[0].trim(), strings[1].trim(), strings[2].trim(), strings[3].trim()); 
        if (id < 0) { 
         Log.e(TAG, "unable to add word: " + strings[0].trim()); 
        } 
       } 
      } finally { 
       reader.close(); 
      } 
     } 

     public long addConstant(String quantity, String value, String uncertainty, String unit) { 
      ContentValues initialValues = new ContentValues(); 
      initialValues.put(COL_QUANTITY, quantity); 
      initialValues.put(COL_VALUE, value); 
      initialValues.put(COL_UNCERTAINTY, uncertainty); 
      initialValues.put(COL_UNIT, unit); 

      return mDatabase.insert(FTS_VIRTUAL_TABLE, null, initialValues); 
     } 

     //database openhelper ends 
    } 

    public Cursor getWordMatches(String query, String[] columns) { 
     String selection = COL_QUANTITY + " MATCH ?"; 
     String[] selectionArgs = new String[] {query+"*"}; 

     return query(selection, selectionArgs, columns); 
    } 

    public Cursor query(String selection, String[] selectionArgs, String[] columns) { 
     SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); 
     builder.setTables(FTS_VIRTUAL_TABLE); 

     Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(), 
       columns, selection, selectionArgs, null, null, null); 

     if (cursor == null) { 
      return null; 
     } else if (!cursor.moveToFirst()) { 
      cursor.close(); 
      return null; 
     } 
     return cursor; 
    } 

    public Cursor getAllTitles() 
    { 
     return mDatabaseOpenHelper.getmDatabase().query(FTS_VIRTUAL_TABLE, new String[] { 
         COL_QUANTITY, 
         COL_UNCERTAINTY, 
         COL_UNIT, 
         COL_VALUE}, 
       null, 
       null, 
       null, 
       null, 
       null); 
    } 
} 

再次感謝你這麼多。

編輯:

原來的錯誤,導致我這是當這一點在其他活動被稱爲:

Cursor c=db.getWordMatches(query,colums); 
Log.e("cursor returned",c.toString()); 

日誌貓返回

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference 
+0

你會發布錯誤嗎? –

+0

當然,我的不好 – grassss

回答

2

你的代碼關閉光標,如果其中沒有數據

if (cursor == null) { 
    return null; 
} else if (!cursor.moveToFirst()) { 
    cursor.close(); 
    return null; 
} 

這樣做會在常見用例中爲您提供空值。之後,您使用遊標,而不檢查

Log.e("cursor returned", c.toString()); 

如果這是你使用遊標的唯一的地方選中你可以只用

Log.e("cursor returned", String.valueOf(c)); 

替換它,否則你可能需要提高您的實現。無論如何,你可以看看DatabaseUtils