2

私はsqlite、LocationsContentProviderを使用してAndroidアプリケーションに取り組んでいます。JOINで困っています。

これはうまくいきました:

    // Returns all the locations from the table
    public Cursor getAllLocations(){            
        Cursor cursor;
        cursor = mDB.query(DATABASE_TABLE, new String[] { FIELD_ROW_ID,  FIELD_LAT , FIELD_LNG, FIELD_ZOOM, FIELD_ADDRESS } , null, null, null, null, null);
        Log.d(TAG, "DB: query complete" + cursor );
        return cursor;
    }

そして、これは機能していません:

    // Return all locations joined with sub map name
    public Cursor getAllLocations(){
        Cursor cursor;
        String query;
        query = "SELECT a._id, a.lat, a.lng, a.sub_map_id, a.zoom, a.address, b._id, b.sub_map FROM locations a" +
                " INNER JOIN sub_map_table b ON a.sub_map_id=b._id";
        Log.d(TAG, "DB: query = \n" + query;
        Log.d(TAG, "DB: query complete" );
        cursor = mDB.rawQuery(query, null);
        return cursor;
    }

データベース構造に関するその他の詳細:

// Fields for locations table
public static final String FIELD_ROW_ID ="_id";
public static final String FIELD_LAT = "lat";
public static final String FIELD_LNG = "lng";
public static final String FIELD_ZOOM = "zoom";
public static final String FIELD_ADDRESS ="address";
public static final String FIELD_SUB_MAP_ID = "sub_map_id";

// Fields for SUB_MAP_TABLE table
public static final String FIELD_SUB_MAP_ROW_ID = "_id";
public static final String FIELD_SUB_MAP = "sub_map";

//table name, a constant
private static final String DATABASE_TABLE = "locations";
private static final String SUB_MAP_TABLE = "sub_map_table";

private static final String TAG = null;

//instance variable for SQLiteDatabse
private SQLiteDatabase mDB;

//constructor
public LocationsDB(Context context) {
    super(context, DBNAME, null, VERSION);
    this.mDB = getWritableDatabase();
}

/** This is a callback method, invoked when the method getReadableDatabase() / getWritableDatabase() is called
    * provided the database does not exists
    * */
    @Override
    public void onCreate(SQLiteDatabase db) {
        String create_table_locations =     "create table " + DATABASE_TABLE + " ( " +
                                         FIELD_ROW_ID + " integer primary key autoincrement , " +
                                         FIELD_LNG + " double , " +
                                         FIELD_LAT + " double , " +
                                         FIELD_ZOOM + " text , " +
                                         FIELD_ADDRESS + " text , " +
                                         FIELD_SUB_MAP_ID + " integer " +
                                         " ) ";

        String create_table_submap = "create table " + SUB_MAP_TABLE + " ( " +
                                    FIELD_SUB_MAP_ROW_ID + " integer primary key autoincrement , " +
                                    FIELD_SUB_MAP + " text " +
                                    " ) ";

        db.execSQL(create_table_locations);
        db.execSQL(create_table_submap);

一日中これを見ていましたが、何が悪いのかわかりません。教えてください。何も送信されていないようですか?または、送信された場合、キャプチャされていません。

4

1 に答える 1

2

「何も送信されていないようです」ということで、エラーが発生していないと思います。それは単にクエリがデータを返さないということですか?

その場合、問題はデータにある可能性があります。sub_map_id が NULL である 'location' 内のすべての行は結合に失敗し、返されません。同様に、sub_map_id に一致する _id のエントリが sub_map_table にない行は返されません。

INNER JOIN の代わりに LEFT JOIN を使用すると、「場所」からすべての行が取得されます。ただし、sub_map_table への結合が失敗した場合、b._id および b.sub_map に返される値は NULL になります。

さらに、結果セットに重複した列名があり、iea_id と b._id の両方が列名 '_id' で返され、カーソルからそれらを参照しようとすると問題が発生します。AS を使用してそれらを区別し、結果で一意の名前を付けることができます。

SELECT a._id AS a_id, b._id AS b_id
于 2013-10-03T13:32:06.373 に答える