Example usage for android.database.sqlite SQLiteDatabase insert

List of usage examples for android.database.sqlite SQLiteDatabase insert

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase insert.

Prototype

public long insert(String table, String nullColumnHack, ContentValues values) 

Source Link

Document

Convenience method for inserting a row into the database.

Usage

From source file:cl.gisred.android.RepartoActivity.java

private boolean insertData(String sValue) {

    SQLiteDatabase db = sqlReparto.getWritableDatabase();
    long nIns = -1;

    if (db != null) {

        ContentValues valores = new ContentValues();
        valores.put("codigo", sValue);
        valores.put("x", oUbicActual.getX());
        valores.put("y", oUbicActual.getY());
        nIns = db.insert("repartos", null, valores);

        db.close();/*from   www .  j a va 2 s . c om*/
    }

    return nIns > 0;
}

From source file:info.staticfree.android.units.UnitUsageDBHelper.java

@SuppressWarnings("unchecked")
public void loadUnitClassifications() {
    final SQLiteDatabase db = getWritableDatabase();
    final JSONObject jo = loadInitialWeights(R.raw.unit_classification);

    db.beginTransaction();// w w  w . j  a  v  a2 s .c  om
    final ContentValues cv = new ContentValues();
    for (final Iterator i = jo.keys(); i.hasNext();) {
        final String unit = (String) i.next();
        final String description = jo.optString(unit);
        final String fprint = getFingerprint(unit);
        cv.put(ClassificationEntry._FACTOR_FPRINT, fprint);
        cv.put(ClassificationEntry._DESCRIPTION, description);
        db.insert(DB_CLASSIFICATION_TABLE, null, cv);
    }
    db.setTransactionSuccessful();
    db.endTransaction();
    db.close();
    Log.d(TAG, "Successfully added " + jo.length() + " classification entries.");
}

From source file:net.smart_json_database.JSONDatabase.java

private int insertRelation(String relName, int from_id, int to_id, SQLiteDatabase db) {
    int returnValue = -1;
    ContentValues values = new ContentValues();
    values.put("from_id", from_id);
    values.put("to_id", to_id);
    values.put("rel_name", relName);
    returnValue = Util.LongToInt(db.insert(TABLE_REL_JSON_DATA_JSON_DATA, null, values));
    return returnValue;
}

From source file:com.example.google.touroflondon.data.TourDbHelper.java

/**
 * Extract Route data from a {@link JSONArray} of save it in the database.
 * /*from w w  w  .  j av  a 2 s .co m*/
 * @param data
 */
public void loadRoute(JSONArray data) throws JSONException {

    SQLiteDatabase db = this.getWritableDatabase();

    // Empty the route table to remove all existing data
    db.delete(TourContract.RouteEntry.TABLE_NAME, null, null);

    // Need to complete transaction first to clear data
    db.close();

    // Begin the insert transaction
    db = this.getWritableDatabase();
    db.beginTransaction();

    // Loop over each location in array
    for (int i = 0; i < data.length(); i++) {
        // extract data
        JSONObject poi = data.getJSONObject(i);
        final double lat = poi.getDouble("lat");
        final double lng = poi.getDouble("lng");

        // Construct insert statement
        ContentValues cv = new ContentValues();
        cv.put(TourContract.RouteEntry.COLUMN_NAME_LAT, lat);
        cv.put(TourContract.RouteEntry.COLUMN_NAME_LNG, lng);

        // Insert data
        db.insert(TourContract.RouteEntry.TABLE_NAME, null, cv);
    }

    if (db != null) {
        // All insert statement have been submitted, mark transaction as
        // successful
        db.setTransactionSuccessful();
        db.endTransaction();
    }
}

From source file:com.example.android.touroflondon.data.TourDbHelper.java

/**
 * Extract Route data from a {@link JSONArray} of save it in the database.
 *
 * @param data/* w  w  w  .jav a 2  s. com*/
 */
public void loadRoute(JSONArray data) throws JSONException {

    SQLiteDatabase db = this.getWritableDatabase();

    // Empty the route table to remove all existing data
    db.delete(TourContract.RouteEntry.TABLE_NAME, null, null);

    // Need to complete transaction first to clear data
    db.close();

    // Begin the insert transaction
    db = this.getWritableDatabase();
    db.beginTransaction();

    // Loop over each location in array
    for (int i = 0; i < data.length(); i++) {
        // extract data
        JSONObject poi = data.getJSONObject(i);
        final double lat = poi.getDouble("lat");
        final double lng = poi.getDouble("lng");

        // Construct insert statement
        ContentValues cv = new ContentValues();
        cv.put(TourContract.RouteEntry.COLUMN_NAME_LAT, lat);
        cv.put(TourContract.RouteEntry.COLUMN_NAME_LNG, lng);

        // Insert data
        db.insert(TourContract.RouteEntry.TABLE_NAME, null, cv);
    }

    if (db != null) {
        // All insert statement have been submitted, mark transaction as successful
        db.setTransactionSuccessful();
        db.endTransaction();
    }
}

From source file:com.appsimobile.appsii.module.home.provider.HomeContentProvider.java

@Override
public Uri insert(Uri uri, ContentValues initialValues) {

    SqlArguments args = new SqlArguments(uri);

    checkInsertConstraints(args.table, initialValues);

    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    final long rowId = db.insert(args.table, null, initialValues);
    if (rowId <= 0)
        return null;

    uri = ContentUris.withAppendedId(uri, rowId);
    sendNotify(uri);/*from   w  ww  .j  a v  a2s . co m*/

    return uri;
}

From source file:net.potterpcs.recipebook.RecipeData.java

public int insertRecipe(ContentValues values) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int ret = -1;
        try {//  w w w  . j a  v  a2 s  .  co m
            ret = (int) db.insert(RECIPES_TABLE, null, values);
        } finally {
            db.close();
        }
        return ret;
    }
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public boolean addTrustedAccessPoint(TrustedAccessPoint tap) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_SSID, tap.ssid);/*from  ww  w.j  ava  2 s  .  com*/
    values.put(KEY_BSSID, tap.bssid);
    int id = (int) db.insert(TABLE_TRUSTED_ACCESS_POINTS, null, values);
    return id >= 0;
}

From source file:com.google.android.apps.muzei.provider.MuzeiProvider.java

private Uri insertSource(@NonNull final Uri uri, final ContentValues initialValues) {
    Context context = getContext();
    if (context == null) {
        return null;
    }/*from ww  w.  j a  v a  2 s.com*/
    if (!initialValues.containsKey(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME)
            || TextUtils.isEmpty(initialValues.getAsString(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME))) {
        throw new IllegalArgumentException("Initial values must contain component name " + initialValues);
    }
    ComponentName componentName = ComponentName
            .unflattenFromString(initialValues.getAsString(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME));
    if (componentName == null) {
        throw new IllegalArgumentException("Invalid component name: "
                + initialValues.getAsString(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME));
    }
    ApplicationInfo info;
    try {
        // Ensure the service is valid and extract the application info
        info = context.getPackageManager().getServiceInfo(componentName, 0).applicationInfo;
    } catch (PackageManager.NameNotFoundException e) {
        throw new IllegalArgumentException("Invalid component name "
                + initialValues.getAsString(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME), e);
    }
    // Make sure they are using the short string format
    initialValues.put(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME, componentName.flattenToShortString());

    // Only Muzei can set the IS_SELECTED field
    if (initialValues.containsKey(MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED)) {
        if (!context.getPackageName().equals(getCallingPackage())) {
            Log.w(TAG, "Only Muzei can set the " + MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED
                    + " column. Ignoring the value in " + initialValues);
            initialValues.remove(MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED);
        }
    }

    // Disable network access callbacks if we're running on an API 24 device and the source app
    // targets API 24. This is to be consistent with the Behavior Changes in Android N
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
            && initialValues.containsKey(MuzeiContract.Sources.COLUMN_NAME_WANTS_NETWORK_AVAILABLE)
            && initialValues.getAsBoolean(MuzeiContract.Sources.COLUMN_NAME_WANTS_NETWORK_AVAILABLE)) {
        if (info.targetSdkVersion >= Build.VERSION_CODES.N) {
            Log.w(TAG,
                    "Sources targeting API 24 cannot receive network access callbacks. Changing "
                            + componentName + " to false for "
                            + MuzeiContract.Sources.COLUMN_NAME_WANTS_NETWORK_AVAILABLE);
            initialValues.put(MuzeiContract.Sources.COLUMN_NAME_WANTS_NETWORK_AVAILABLE, false);
        }
    }
    final SQLiteDatabase db = databaseHelper.getWritableDatabase();
    final long rowId = db.insert(MuzeiContract.Sources.TABLE_NAME,
            MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME, initialValues);
    // If the insert succeeded, the row ID exists.
    if (rowId > 0) {
        // Creates a URI with the source ID pattern and the new row ID appended to it.
        final Uri sourceUri = ContentUris.withAppendedId(MuzeiContract.Sources.CONTENT_URI, rowId);
        notifyChange(sourceUri);
        return sourceUri;
    }
    // If the insert didn't succeed, then the rowID is <= 0
    throw new SQLException("Failed to insert row into " + uri);
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public boolean addAllowedDomain(String domain, String exfiltrated) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_APP_INFO, domain.trim());
    values.put(KEY_PERMISSIONS, exfiltrated);
    int id = (int) db.insert(TABLE_ALLOWED_DOMAINS, null, values);
    return id >= 0;
}