从db2排序的db1中选择值

时间:2016-01-17 08:35:59

标签: java android sql sqlite

我需要从Android上的两个Sqlite数据库中选择值。值需要按特定顺序排序。值在表第一个db中的产品中,它们的顺序在表中面对第二个db。当我做这样的事情时,

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`

我收到了错误

  

android.database.sqlite.SQLiteException:near" cp":语法错误(代码1):

两个数据库都在同一个private / databases /文件夹中。他们不是由我做的,我只是使用它们。

这是我的方法

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

这是dbHandler

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 && 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 && db.isOpen()) {
                db.close();
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

问题是您在fon之间没有空格,请参阅下文:

    "inner join InventoryDB.Facing as f" +
    "on p.PrdID = f.PrdID " +

尝试编写如下查询:

    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);