Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1065905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:55:37+00:00 2026-05-16T19:55:37+00:00

I use a database with multiple tables in my application. I have an XML

  • 0

I use a database with multiple tables in my application. I have an XML parser which needs to write data to two tables while parsing. I created two database adapters for both tables, but now I have a problem. When I’m working with one table, it’s easy:

FirstDBAdapter firstTable = new FirstDBAdapter(mycontext);
firstTable.open(); // open and close it every time I need to insert something
                   // may be hundreds of times while parsing
                   // it opens not a table but whole DB     
firstTable.insertItem(Item);        
firstTable.close(); 

Since it’s a SAX parser, in my opinion (maybe I’m wrong), this will be even better:

FirstDBAdapter firstTable = new FirstDBAdapter(mycontext);

@Override
public void startDocument() throws SAXException 
{
    firstTable.open(); // open and close only once
}

...
firstTable.insertItem(Item);
...

@Override
public void endDocument() throws SAXException 
{
    firstTable.close();
}

But how do I do it if I need to insert data to the second table? For example, if I have the second adapter, which I think will be a bad idea:

FirstDBAdapter firstTable = new FirstDBAdapter(mycontext);
SecondDBAdapter secondTable = new SecondDBAdapter(mycontext);

@Override
public void startDocument() throws SAXException 
{
    firstTable.open();
    secondTable.open(); 
}

Any thoughts on how to achieve this?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T19:55:37+00:00Added an answer on May 16, 2026 at 7:55 pm

    My database adapter. An instance is always stored in MyApplication which inherites from Application. Just think about a second table where I defined the first one… currently this is just a short version, in reality this adapter handles 7 tables in the database.

    public class MyDbAdapter {
        private static final String LOG_TAG = MyDbAdapter.class.getSimpleName();
    
        private SQLiteDatabase mDb;
        private static MyDatabaseManager mDbManager;
    
        public MyDbAdapter() {
            mDbManager = new MyDatabaseManager(MyApplication.getApplication());
            mDb = mDbManager.getWritableDatabase();
        }
    
        public static final class GameColumns implements BaseColumns {
            public static final String TABLE = "game";
            public static final String IMEI = "imei";
            public static final String LAST_UPDATE = "lastupdate";
            public static final String NICKNAME = "nickname";
        }
    
        public String getImei() {
            checkDbState();
            String retValue = "";
            Cursor c = mDb.rawQuery("SELECT imei FROM " + GameColumns.TABLE, null);
            if (c.moveToFirst()) {
                retValue = c.getString(c.getColumnIndex(GameColumns.IMEI));
            }
            c.close();
            return retValue;
        }
    
        public void setImei(String imei) {
            checkDbState();
            ContentValues cv = new ContentValues();
            cv.put(GameColumns.IMEI, imei);
            mDb.update(GameColumns.TABLE, cv, null, null);
        }
    
        public boolean isOpen() {
            return mDb != null && mDb.isOpen();
        }
    
        public void open() {
            mDbManager = new MyDatabaseManager(MyApplication.getApplication());
            if (!isOpen()) {
                mDb = mDbManager.getWritableDatabase();
            }
        }
    
        public void close() {
            if (isOpen()) {
                mDb.close();
                mDb = null;
                if (mDbManager != null) {
                    mDbManager.close();
                    mDbManager = null;
                }
            }
        }
    
        private void checkDbState() {
            if (mDb == null || !mDb.isOpen()) {
                throw new IllegalStateException("The database has not been opened");
            }
        }
    
        private static class MyDatabaseManager extends SQLiteOpenHelper {
            private static final String DATABASE_NAME = "dbname";
            private static final int DATABASE_VERSION = 7;
    
            private MyDatabaseManager(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }
    
            @Override
            public void onCreate(SQLiteDatabase db) {
                createGameTable(db);
            }
    
            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                Log.w(LOG_TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + "!");
            }
    
            private void dropDatabase(SQLiteDatabase db) {
                db.execSQL("DROP TABLE IF EXISTS " + GameColumns.TABLE);
            }
    
            private void createGameTable(SQLiteDatabase db) {
                db.execSQL("CREATE TABLE " + GameColumns.TABLE + " ("
                        + GameColumns._ID + " INTEGER PRIMARY KEY,"
                        + GameColumns.IMEI + " TEXT,"
                        + GameColumns.LAST_UPDATE + " TEXT,"
                        + GameColumns.NICKNAME + " TEXT);");
                ContentValues cv = new ContentValues();
                cv.put(GameColumns.IMEI, "123456789012345");
                cv.put(GameColumns.LAST_UPDATE, 0);
                cv.put(GameColumns.NICKNAME, (String) null);
                db.insert(GameColumns.TABLE, null, cv);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that needs to join tables from multiple databases into a
In my Android application; I have a single database with multiple tables. Each table
I have an application where I'd like to use a NoSQL database, but I
I have a web application that needs a database back-end. My back-end is really
I have a SQL database with two tables like this: Users Id (PK) Name
I have a backend process that maintains state in a PostgreSQL database, which needs
I have a project where i should use multiple tables to avoid keeping dublicated
I have to use oracle database in android. I have tried to work as
I would like to have multiple users that share the same tables in the
I'm trying to use EF 4.3 migrations with multiple code-first DbContexts. My application is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.