Android: SQLite using the wrong database

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; //db.rawQuery("update NextID set NxtNumber = '" + rtnID + "' where NxtTable like 'StationObjects'", null); dbF.close(); return rtnID; } 

How can I make sure it uses the second database, not the first? Tom

+6
source share
1 answer

You can 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() 

In the case of two databases, you will need to define 2 DB_PATH and indicate them where they are relevant.

Hope this helps, Partners L and L

+2
source

Source: https://habr.com/ru/post/925411/


All Articles