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

  • Home
  • SEARCH
  • 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 8310959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:38:57+00:00 2026-06-08T19:38:57+00:00

I am new to Android programming and am trying to understand the best practices.

  • 0

I am new to Android programming and am trying to understand the best practices.

I want to do multiple inserts into two different database tables, but as one transaction (as the tables shared a foreign key). I want my function to return a result so that I can display a Toast or something to say that an error occurred, otherwise I want to return the row ID of the first insert.

I believe one way of doing this is sort-of as follows (Disclaimer: psuedo-ish code, probably won’t compile!):

Long result = -1;
myDatabase.beginTransaction();
try {
   // Insert into first table
   ContentValue someValues = new ContentValues();
   someValues.put("dbfield1", 1);
   result = myDatabase.insert(DATABASE_TABLE_1, null, someValues);

   if (-1 != result ) {         
       // Insert into second table
       someValues.clear();
       someValues.put("dbfield2", 2);

       if( myDatabase.insert(DATABASE_TABLE_2, null, someValues) < 0 ) {
           result = -1;
       }
   }

   mDatabase.setTransactionSuccessful();
} catch(Exception e) {
    // An error occurred
    result = -1;
} finally {
   mDatabase.endTransaction();
}

Is there a simpler/better way of doing this?

  • 1 1 Answer
  • 0 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-06-08T19:38:58+00:00Added an answer on June 8, 2026 at 7:38 pm

    You can override bulkInsert inside your ContentProvider
    your Code looks fine. The Method should return the inserted Rows Value but you can customize that so you return only the first ID.

    public int bulkInsert(Uri uri, ContentValues[] values) {
        Log.e("BULK", "Bulk insert started for URI" + uri.toString());
        bulkSqlDB = database.getWritableDatabase();
        int numInserted;
    
        bulkSqlDB.beginTransaction();
        try {
            for (ContentValues cv : values) {
                insert(uri, cv);
            }
            bulkSqlDB.setTransactionSuccessful();
            numInserted = values.length;
        } finally {
            getContext().getContentResolver().notifyChange(uri, null);
            bulkSqlDB.endTransaction();
        }
        return numInserted;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still new to programming/Java/Android so I'm trying to understand everything I do and
I'm still new in android programming, what I'm trying to do is, I want
I am new to Android programming and am trying to understand the concept of
I am trying to get into Android programming, but I cannot understand the Google
i am new to android programming and i am trying to develop an app
I am new to android programming and i am trying to develop this app
I'm new in Android Programming. I'm trying to write an app that uses USB
I'm new to Android programming. I spent a long time trying to look for
i am new to android programming and i want to create an api with
I am new to android programming and trying to build a map activity app.

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.