I'm using multiple SQLite databases in my android java project. The first one (for a login) works fine, using the following code and I am closing the connection. However, on the following page i'm calling a function which uses a different database but it seems to still reference the first database and not the new one. I'm getting this error:
09-14 13:18:01.990: I/SqliteDatabaseCpp(10035): sqlite returned: error code = 1, msg = no such table:
NextID, db=/mnt/extSdCard/DirectEnquiries/AuditingData/Static/Users
Which is right, NextID isn't in Users.
Here is the code for the login page which uses the Users table:
if(String.valueOf(loginCount).equals("2")) {
File dbfile = new File(Global.StaticDB + "/Users" );
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
Cursor c = db.rawQuery("SELECT UserID from Users where UserID like '" + txtUsername.getText().toString().trim() + "' AND Password like '" + txtPassword.getText().toString().trim() + "'", null);
c.moveToFirst();
if(c.getCount() > 0) {
Global.Username = c.getString(c.getColumnIndex("UserID"));
Global.currentDB = spnLocation.getSelectedItem().toString();
Global.currentDBfull = Global.sctArea + Global.currentDB;
db.close();
Context context = getApplicationContext();
CharSequence text = "Logged In";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent ShowMainPage = new Intent(view.getContext(), MainPage.class);
startActivityForResult(ShowMainPage, 0);
}
The next page is using this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText(Global.Username);
TextView dbtxt = (TextView) findViewById(R.id.txtDB);
dbtxt.setText(Functions.getNextID("StationObjects"));
}
And the function is:
public static int getNextID(String tablename) {
Log.e("Current DB is: ", Global.currentDBfull);
File dbfile = new File(Global.currentDBfull);
SQLiteDatabase dbF = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
int rtnID=(int)DatabaseUtils.longForQuery(dbF,"Select NxtNumber from NextID where NxtTable like '" + tablename + "'",null);
rtnID += 1;
//db.rawQuery("update NextID set NxtNumber = '" + rtnID + "' where NxtTable like 'StationObjects'", null);
dbF.close();
return rtnID;
}
How do I make sure it uses the second database and not the first? Tom
You could try using the following format to open and close your database:
private static String DB_PATH = "/data/data/PACKAGE_NAME/databases/DATABASE_NAME.db";
private static SQLiteDatabase db;
db = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY);
// RUN YOUR QUERIES ETC
db.close()
For the case of 2 databases you would need to define 2 DB_PATHs and reference them where relevent.
Hope this helps, L & L Partners
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With