Select values from db1 ordered by db2
<p>I need to select values from two Sqlite databases on Android. Values needs to be sorted by particular order. Values are in table Products in first db, order of them is in table Facing inside second db. When i do something like this,</p>

<pre><code>Select p.PrdImgURL, cp.PrdID 
from Products as p 
inner join CategoryProduct_MM as cp 
on p.PrdID = cp.PrdID 
inner join InventoryDB.Facing as f 
on p.PrdID = f.PrdID 
where cp.CategoryID == 1 and p.PrdImgURL is not null 
order by f.`Order`
</code></pre>

<p>I've got an error</p>

<blockquote>
  <p>android.database.sqlite.SQLiteException: near "cp": syntax error (code 1):</p>
</blockquote>

<p>Both databases are in the same private /databases/ folder. They made not by me, I just use them.</p>

<p>Here is my method</p>

<pre><code>    private void loadProducts() {
        dbHandler1 dbh1 = dbHandler1.getInstance(this);
        SQLiteDatabase db1 = dbh1.getWritableDatabase();
        db1.execSQL("attach database ? as InventoryDB", new String[]{DB_PATH + DB2_NAME});
        db1.beginTransaction();

        Cursor c = db1.rawQuery("Select p.PrdImgURL, cp.PrdID " +
                "from Products as p " +
                "inner join CategoryProduct_MM as cp " +
                "on p.PrdID = cp.PrdID " +
                "inner join InventoryDB.Facing as f" +
                "on p.PrdID = f.PrdID " +
                "where cp.CategoryID == 1 and p.PrdImgURL is not null "
                "order by f.`Order`", null);
        int count = c.getCount();
        String url[] = new String[count];
        int i = 0;


        while (c.moveToNext()) {
            url[i] = c.getString(c.getColumnIndex("PrdImgURL"));
            i++;
        }
    }
</code></pre>

<p>Here is dbHandler</p>

<pre><code>public class dbHandler1 extends SQLiteOpenHelper {
    private static final int VERSION = 1;
    private static final String DATABASE_NAME = "SuperupDB.sqlite";
    private static final String TAG = "qwe";
    private static File DATABASE_FILE;

    // This is an indicator if we need to copy the
    // database file.
    private boolean mInvalidDatabaseFile = false;
    private boolean mIsUpgraded = false;
    private Context mContext;

    /**
     * number of users of the database connection.
     */
    private int mOpenConnections = 0;

    private static dbHandler1 mInstance;

    synchronized static public dbHandler1 getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new dbHandler1(context.getApplicationContext());
        }
        return mInstance;
    }

    private dbHandler1(Context context) {
        super(context, DATABASE_NAME, null, VERSION);
        this.mContext = context;
        SQLiteDatabase db = null;
        try {
            db = getReadableDatabase();
            if (db != null) {
                db.close();
            }

            DATABASE_FILE = context.getDatabasePath(DATABASE_NAME);

/*            if (mInvalidDatabaseFile) {
                //copyDatabase();
                Log.d(TAG, "Where is db?");
            }*/
            if (mIsUpgraded) {
                doUpgrade();
            }
        } catch (SQLiteException e) {
        } finally {
            if (db != null &amp;&amp; db.isOpen()) {
                db.close();
            }
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //mInvalidDatabaseFile = true;

    }

    @Override
    public void onUpgrade(SQLiteDatabase database,
                          int old_version, int new_version) {
        //mInvalidDatabaseFile = true;
        mIsUpgraded = true;

    }

    /**
     * called if a database upgrade is needed
     */
    private void doUpgrade() {
        // implement the database upgrade here.
    }

    @Override
    public synchronized void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        // increment the number of users of the database connection.
        mOpenConnections++;
        if (!db.isReadOnly()) {
            // Enable foreign key constraints
            db.execSQL("PRAGMA foreign_keys=ON;");
        }
    }

    /**
     * implementation to avoid closing the database connection while it is in
     * use by others.
     */
    @Override
    public synchronized void close() {
        mOpenConnections--;
        if (mOpenConnections == 0) {
            super.close();
        }
    }

    private void setDatabaseVersion() {
        SQLiteDatabase db = null;
        try {
            db = SQLiteDatabase.openDatabase(DATABASE_FILE.getAbsolutePath(), null,
                    SQLiteDatabase.OPEN_READWRITE);
            db.execSQL("PRAGMA user_version = " + VERSION);
        } catch (SQLiteException e) {
        } finally {
            if (db != null &amp;&amp; db.isOpen()) {
                db.close();
            }
        }
    }

}
</code></pre>