I am using several SQLite databases in my javascript project for Android. The first (for login) works fine using the following code, and I close the connection. However, on the next page, I call a function that uses a different database, but seems to still reference the first database, not the new one. I get 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 correct, NextID is not in Users.
Here is the code for the login page that 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 following page uses 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 function:
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;
How can I make sure it uses the second database, not the first? Tom