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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:40:10+00:00 2026-05-22T22:40:10+00:00

So basically, I have this content provider which I use to store and retrieve

  • 0

So basically, I have this content provider which I use to store and retrieve some data in a database. In one of my activities, I have a function for adding data to this database; however, I do not want there to be duplicate entries. My function would look something like this:

public void addData(String data) {
  if ( /* data is not already in database */ ) {
    ConstantValues values = new ConstantValues();
    values.put(DATA_FIELD, data);
    getContentResolver().insert(CONTENT_URI, values);
  } else {
    // do nothing, it's already there
  }
}

Now, my question is, how can I check whether or not the data is already stored by the content provider?

EDIT:

This is the code for my content provider:

DataProvider.java

package org.frxstrem.xend;
import java.util.HashMap;
import org.frxstrem.xend.Data.Commands;
import org.frxstrem.xend.Data.SerialNumbers;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
public class DataProvider extends ContentProvider {

    private static final String DB_NAME = "xsend";
    private static final int DB_VERSION = 1;

    private static final String TABLE_SN = "serial_numbers";
    private static final String TABLE_CMD = "commands";

    private static final int SN = 1;
    private static final int SN_ID = 2;
    private static final int CMD = 3;
    private static final int CMD_ID = 4;

    private static HashMap<String, String> snpm;
    private static HashMap<String, String> cmdpm;
    private static final UriMatcher uriMatcher;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(Context c) {
            super(c, DB_NAME, null, DB_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_SN + " ("
                    + SerialNumbers._ID + " INTEGER PRIMARY KEY, "
                    + SerialNumbers.TEXT + " TEXT"
                    + ");");
            db.execSQL("CREATE TABLE " + TABLE_CMD + " ("
                    + Commands._ID + " INTEGER PRIMARY KEY, "
                    + Commands.TEXT + " TEXT"
                    + ");");
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_SN);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_CMD);
            onCreate(db);
        }

    }

    private DatabaseHelper dbh;

    @Override
    public boolean onCreate() {
        dbh = new DatabaseHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        String orderBy;
        switch(uriMatcher.match(uri)) {
        case SN:
        {
            qb.setTables(TABLE_SN);
            qb.setProjectionMap(snpm);
            orderBy = SerialNumbers.DEFAULT_SORT_ORDER;
        }
        break;

        case SN_ID:
        {
            qb.setTables(TABLE_SN);
            qb.setProjectionMap(snpm);
            qb.appendWhere(SerialNumbers._ID + " = " + uri.getPathSegments().get(1));
            orderBy = SerialNumbers.DEFAULT_SORT_ORDER;
        }
        break;
        case CMD:
        {
            qb.setTables(TABLE_CMD);
            qb.setProjectionMap(cmdpm);
            orderBy = Commands.DEFAULT_SORT_ORDER;
        }
        break;

        case CMD_ID:
        {
            qb.setTables(TABLE_CMD);
            qb.setProjectionMap(cmdpm);
            qb.appendWhere(Commands._ID + " = " + uri.getPathSegments().get(1));
            orderBy = Commands.DEFAULT_SORT_ORDER;
        }
        break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        if(!TextUtils.isEmpty(sortOrder)) {
            orderBy = sortOrder;
        }

        SQLiteDatabase db = dbh.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);

        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    @Override
    public String getType(Uri uri) {
        switch(uriMatcher.match(uri)) {
        case SN:
            return SerialNumbers.CONTENT_TYPE;

        case SN_ID:
            return SerialNumbers.CONTENT_ITEM_TYPE;

        case CMD:
            return Commands.CONTENT_TYPE;

        case CMD_ID:
            return Commands.CONTENT_ITEM_TYPE;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        Uri baseUri;
        String table;
        String nullColumnHack;
        ContentValues values;
        if(uriMatcher.match(uri) == SN) {
            baseUri = SerialNumbers.CONTENT_URI;
            table = TABLE_SN;
            nullColumnHack = SerialNumbers.TEXT;
            if(initialValues != null)
                values = new ContentValues(initialValues);
            else
                values = new ContentValues();

            if(!values.containsKey(SerialNumbers.TEXT))
                throw new IllegalArgumentException("Text required");
        } else if(uriMatcher.match(uri) == CMD) {
            baseUri = Commands.CONTENT_URI;
            table = TABLE_CMD;
            nullColumnHack = Commands.TEXT;
            if(initialValues != null)
                values = new ContentValues(initialValues);
            else
                values = new ContentValues();

            if(!values.containsKey(Commands.TEXT))
                throw new IllegalArgumentException("Text required");
        } else {
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        SQLiteDatabase db = dbh.getWritableDatabase();
        long rowId = db.insert(table, nullColumnHack, values);
        if(rowId > 0) {
            Uri newUri = ContentUris.withAppendedId(baseUri, rowId);
            getContext().getContentResolver().notifyChange(newUri, null);
            return newUri;
        }

        throw new SQLException("Failed to insert row into " + uri);
    }

    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        SQLiteDatabase db = dbh.getWritableDatabase();
        int count;
        switch(uriMatcher.match(uri)) {
        case SN:
        {
            count = db.delete(TABLE_SN, where, whereArgs);
        }
        break;

        case SN_ID:
        {
            String msgId = uri.getPathSegments().get(1);
            count = db.delete(TABLE_SN, SerialNumbers._ID + " = " + msgId + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs); 
        }
        break;
        case CMD:
        {
            count = db.delete(TABLE_CMD, where, whereArgs);
        }
        break;

        case CMD_ID:
        {
            String msgId = uri.getPathSegments().get(1);
            count = db.delete(TABLE_CMD, Commands._ID + " = " + msgId + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs); 
        }
        break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
        SQLiteDatabase db = dbh.getWritableDatabase();
        int count;
        switch(uriMatcher.match(uri)) {
        case SN:
        {
            count = db.update(TABLE_SN, values, where, whereArgs);
        }
        break;

        case SN_ID:
        {
            String msgId = uri.getPathSegments().get(1);
            count = db.update(TABLE_SN, values, SerialNumbers._ID + " = " + msgId + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs); 
        }
        break;
        case CMD:
        {
            count = db.update(TABLE_CMD, values, where, whereArgs);
        }
        break;

        case CMD_ID:
        {
            String msgId = uri.getPathSegments().get(1);
            count = db.update(TABLE_CMD, values, Commands._ID + " = " + msgId + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs); 
        }
        break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(Data.AUTHORITY, "sn", SN);
        uriMatcher.addURI(Data.AUTHORITY, "sn/#", SN_ID);
        uriMatcher.addURI(Data.AUTHORITY, "cmd", CMD);
        uriMatcher.addURI(Data.AUTHORITY, "cmd/#", CMD_ID);

        snpm = new HashMap<String, String>();
        snpm.put(SerialNumbers._ID, SerialNumbers._ID);
        snpm.put(SerialNumbers.TEXT, SerialNumbers.TEXT);

        cmdpm = new HashMap<String, String>();
        cmdpm.put(Commands._ID, Commands._ID);
        cmdpm.put(Commands.TEXT, Commands.TEXT);
    }
}

Data.java

package org.frxstrem.xend;
import android.net.Uri;
import android.provider.BaseColumns;
public class Data {

    public static final String AUTHORITY = "org.frxstrem.xsend";

    private Data() { }

    public static class SerialNumbers implements BaseColumns {

        private SerialNumbers() { }

        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/sn");
        public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.frxstrem.serialnumber";
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.frxstrem.serialnumber";

        public static final String DEFAULT_SORT_ORDER = "_id ASC";

        public static final String TEXT = "text";

    }

    public static class Commands implements BaseColumns {

        private Commands() { }
        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/cmd");
        public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.frxstrem.command";
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.frxstrem.command";

        public static final String DEFAULT_SORT_ORDER = "_id ASC";

        public static final String TEXT = "text";

    }
}
  • 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-05-22T22:40:11+00:00Added an answer on May 22, 2026 at 10:40 pm

    I figured it out myself, using the following code:

    Cursor c = getContentResolver().query(CONTENT_URI, null, DATA_FIELD + " = " + DatabaseUtils.sqlEscapeString(data), null, null);
    if(c.getCount() == 0) {
      // not found in database
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET application. Basically the delivery process is this one : Nant
I have implemented a locationlistener in my app which uses the Network Provider. This
Well basically I have this script that takes a long time to execute and
Basically you have two ways for doing this: for (int x = 0; x
I'm not really sure how to title this question but basically I have an
I have written a secure TCP server in .NET. This was basically as simple
this kind of follows on from another question of mine. Basically, once I have
I basically have a page which shows a processing screen which has been flushed
My application is basically a content based router which will route MMS events. The
I have written a plugin for Wordpress which generates a jQuery.accordion list of content

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.