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,
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`
i've got an error "no such table: InventoryDB.Facing". Both databases are in the same private /databases/ folder. They made not by me, i use prepopulated tables.
Here is my method
private void loadProducts() {
dbHandler1 dbh1 = dbHandler1.getInstance(this);
SQLiteDatabase db1 = dbh1.getWritableDatabase();
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++;
}
}
Here is 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();
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire