6

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

6
  • 1
    Where did you close your cursor on that login Database? Commented Sep 14, 2012 at 12:27
  • Just tried closing the cursor on the first sub but still the same error Commented Sep 14, 2012 at 12:29
  • 3
    You didn't post many of your logcat. Can we just check the "Current DB is:" message, exceptions and so on ? Also what is Global.sctArea, and what happens if (c.getCount() > 0) is false ? You're hidding many informations (I know that's for simplicity, but it's not helping much...) Commented Sep 17, 2012 at 8:13
  • Are you sure Global.currentDBfull points to the right path? Use static variables very carefully. Have you tried to user raw query insread of DatabaseUtils.longForQuery? Commented Sep 20, 2012 at 11:13
  • can u put the whole log post.. its insufficient data Commented Sep 21, 2012 at 9:42

1 Answer 1

2
+25

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.