簡體   English   中英

SQLite Android內連接

[英]SQLite Android Inner join

我有一個奇怪的問題。 我從服務器獲取數據並將其插入表中。 插入后,我使用兩個表之間的內部聯接查詢該數據。 這是我的查詢:

Select 
    F._id, F.id, F.logo, F.total_like, F.distance, F.store_name, F.mob_no_1,
    F.mob_no_2, F.mob_no_3, F.tel_no_1, F.tel_no_2, F.tel_no_3, F.description,
    R.total_record, R.total_page, R.current_page 
from 
    FAVOURITE_STORES as F 
    INNER JOIN FAVOURITE_RECORDS as R on F.area_id = R.area_id 
where 
    F.area_id = 2 and 
    R.area_id = 2

我在某些設備上將光標計數設置為1,在某些設備上,我將光標計數設置為零。 即使表格中有數據。

這是我的選擇查詢功能

public Cursor rawQuery(String sQuery,String[] selectionArgs) {
        if(mDatabase == null) {
            mDatabase = getWritableDatabase();
        }
        debug("Query "+sQuery);
        return mDatabase.rawQuery(sQuery,selectionArgs);
    }

游標類

public class ExampleCursorLoader extends CursorLoader {
    private Activity mActivity;
    private String msQuery;
    private DBUtil mDbUtil;
    private String[] mSelectionArgs;

    public ExampleCursorLoader(Activity context, String query,String[] selectionArgs) {
        super(context);
        this.mActivity = context;
        this.msQuery = query;
        this.mSelectionArgs = selectionArgs;
        this.mDbUtil = DBUtil.getInstance(mActivity.getApplicationContext());
    }

    public ExampleCursorLoader(Activity context, String query) {
        this(context,query,null);
        debug(query);
    }
    public Cursor loadInBackground() {  
        debug("Loading in Background");
        Cursor cursor=null;
        cursor = mDbUtil.rawQuery(msQuery,mSelectionArgs);
        return cursor;
    }

    private void debug(String s) {
        Log.v("Adapter " , "Adapter " + s);
    }

    protected void onStartLoading() {
        forceLoad();
        debug("Started Loading");
    }

    protected void onStopLoading() {
        super.onStopLoading();
    }


}

這就是我所說的。

return new ExampleCursorLoader(mActivity,sQuery);

計數為1的設備是三星s3。 三星是Grand。 有什么想法或建議嗎?

計數為1的設備是三星s3。 三星是Grand。 有什么想法或建議嗎?

這種問題很難解釋,因為它很可能不會拋出任何錯誤,只會顯示不同的和不需要的結果。

您所能做的就是“改進查詢”

首先使用占位符而不是原始語句:

String query = "... where F.area_id = ? and R.area_id = ?";
String[] whereArgs = {"2", "2"};

你也不需要使用AS子句,這足以說:

... from FAVOURITE_STORES F INNER JOIN FAVOURITE_RECORDS R on ...

從您提供的代碼看起來是正確的(查詢和rawQuery()的用法)。

在我看來,你的WHERE子句的最后一部分是不必要的:

 and R.area_id=2

您已經為FAVOURITE_STORES表指定了行以匹配area_id = 2。 當在該列上執行INNER JOIN時,它將僅返回FAVOURITE_RECORDS中的行,其中area_id列與FAVOURITE_STORES的area_id列匹配。

我不確定SQLite引擎如何處理這個問題,但值得一試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM