2016-01-17 4 views
0

Мне нужно выбрать значения из двух баз данных Sqlite на Android. Значения должны сортироваться по определенному заказу. Значения указаны в таблице. Продукты в первом db, их порядок находится в таблице. Облицовка внутри второго db. Когда я делаю что-то вроде этого,Выберите значения из db1, упорядоченные по db2

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: рядом с "ср": ошибка синтаксиса (код 1):

Обе базы данных находятся в одной частной/базах данных/папке. Они не сделали меня, я просто использую их.

Вот мой метод

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

} 

ответ

0

Этот вопрос вы не имеете пространство между f и on смотри ниже:

"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); 
+0

Вы правы! Большое спасибо. –

Смежные вопросы