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 8323919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:52:29+00:00 2026-06-08T23:52:29+00:00

I was refactoring my code and replacing all insert statements with insert or replace

  • 0

I was refactoring my code and replacing all insert statements with insert or replacewhile i found the following method that uses db.insert() instead of a prepared statement:

public static void insertIntoTableByNameAndFieldsAndValues(
        String tableName, String[] fields, String[] values, Context c)
        throws FieldsAndValuesMismatchException, UnrecognizedTypeException   {

    DatabaseAbstractionLayer dal = new DatabaseAbstractionLayer(c);
    SQLiteDatabase db = dal.getWritableDatabase();

    ContentValues con = new ContentValues();

    if (fields.length != values.length)
        throw new FieldsAndValuesMismatchException(
                "fieldsString and values are not the same size");

    int fieldCount = fields.length;

    for (int i = 0; i < fieldCount; i++) {
        String[] fieldArr = fields[i].split(":");
        String value = values[i];
        if (fieldArr[1].equalsIgnoreCase("long")) {
            // long
            if(value.equalsIgnoreCase("null")){
                con.putNull(fieldArr[0]);
            }else{
                con.put(fieldArr[0],(long) Integer.parseInt(value));
            }
        } else if (fieldArr[1].equalsIgnoreCase("string") || fieldArr[1].equalsIgnoreCase("text")) {
            // string

            if(value.equalsIgnoreCase("null")){
                con.putNull(fieldArr[0]);
            }else{
                con.put(fieldArr[0], value);
            }


        }else if(fieldArr[1].equalsIgnoreCase("double")){
            //double

            if(value.equalsIgnoreCase("null")){
                con.putNull(fieldArr[0]);
            }else{
                con.put(fieldArr[0], Double.valueOf(value));
            }

        } else {
            throw new UnrecognizedTypeException(fieldArr[1]
                    + " is not string,text, long or double");
        }
    }

    db.insert(tableName, null, con);

 db.close();
}

I don’t want to rewrite the code and use a raw query.
Is there a way to replace a row when a conflict occurs? I guess insertWithOnConflict() would do that but I couldn’t find a good example.

  • 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-08T23:52:30+00:00Added an answer on June 8, 2026 at 11:52 pm

    Yup, (change BaseColumns._ID to something suitable if you do not follow the standard name for the row identifier used by Android) and this should do what you want:

    db.insertWithOnConflict(tableName, BaseColumns._ID, v, 
            SQLiteDatabase.CONFLICT_REPLACE);
    

    But – that method sure does a lot of pointless things. In short, it could be boiled down to this:

    public static void insertIntoTableByNameAndFieldsAndValues(
        String tableName, String[] fields, String[] values, Context c)
        throws FieldsAndValuesMismatchException {
    
        if (fields.length != values.length) {
            throw new FieldsAndValuesMismatchException(
                "fields[] and values[] are not the same length");
        }
        ContentValues v = new ContentValues();
        // You really don't need to distinguish between integers, doubles
        // etc. etc. - SQLite is type agnostic, just dumping Strings from Java
        // will work just fine - the *only* point in converting stuff is to check
        // the format prior to inserts.
        // Using "null" as the null identifier is also pointless. Pass actual null
        // values instead - calling #putNull("somecolumn") is the same 
        // as #put("somecolumn", null)
        for (int i = 0; i < fields.length; i++) {
            v.put(fields[i], values[i]);
        }
        db.insertWithOnConflict(tableName, BaseColumns._ID, v, 
                SQLiteDatabase.CONFLICT_REPLACE);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was refactoring some code and found there are two places that can be
While refactoring code and ridding myself of all those #defines that we're now taught
I'm currently refactoring code to replace Convert.To's to TryParse. I've come across the following
While refactoring my code base I found a piece of code which I'd like
I am now refactoring a code written by someone else and I have found
I have some code that I had to write to replace a function that
I'm refactoring some code that currently looks like this: $$('#wrapper > div').each(function(e){ if((e.id!='header') &&
I'm a bit stuck with refactoring my code to make a method available to
Why does Visual Studio by default create a private static method when refactoring code
I've been refactoring some code that was originally using the Messenger in MVVM Foundation

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.