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:com.wit.android.support.database.examples.database.ActorsEntity.java

/**
 *///from www .j  a  v a 2  s . co  m
@Override
protected void onCreateDefaultContent(@NonNull SQLiteDatabase db, @NonNull Context context) {
    Actor actor;
    for (int i = 0; i < 50; i++) {
        actor = new Actor();
        actor.setId(i);
        actor.setFirstName("Actor");
        actor.setLastName("Number no. " + Integer.toString(i + 1));
        db.insert(ENTITY_NAME, null, actor.toContentValues());
    }
}

From source file:org.mozilla.labs.Soup.provider.AppsProvider.java

@Override
public Uri insert(Uri uri, ContentValues initialValues) {
    // Validate the requested uri
    if (sUriMatcher.match(uri) != APPS) {
        throw new IllegalArgumentException("Unknown URI " + uri);
    }/*  w ww  . j a v a2 s . c  om*/

    ContentValues values;
    if (initialValues != null) {
        values = new ContentValues(initialValues);
    } else {
        values = new ContentValues();
    }

    Long now = Long.valueOf(System.currentTimeMillis());

    // Make sure that the fields are all set
    if (values.containsKey(AppsContract.Apps.CREATED_DATE) == false) {
        values.put(AppsContract.Apps.CREATED_DATE, now);
    }
    if (values.containsKey(AppsContract.Apps.MODIFIED_DATE) == false) {
        values.put(AppsContract.Apps.MODIFIED_DATE, now);
    }
    if (values.containsKey(AppsContract.Apps.INSTALL_TIME) == false) {
        values.put(AppsContract.Apps.INSTALL_TIME, now);
    }

    if (values.containsKey(AppsContract.Apps.NAME) == false) {
        Resources r = Resources.getSystem();
        values.put(AppsContract.Apps.NAME, r.getString(android.R.string.untitled));
    }
    if (values.containsKey(AppsContract.Apps.DESCRIPTION) == false) {
        values.put(AppsContract.Apps.DESCRIPTION, "");
    }

    if (values.containsKey(AppsContract.Apps.MANIFEST) == false) {
        values.put(AppsContract.Apps.MANIFEST, new JSONObject().toString());
    }
    if (values.containsKey(AppsContract.Apps.MANIFEST_URL) == false) {
        values.put(AppsContract.Apps.MANIFEST_URL, "");
    }
    if (values.containsKey(AppsContract.Apps.INSTALL_DATA) == false) {
        values.put(AppsContract.Apps.INSTALL_DATA, new JSONObject().toString());
    }

    if (values.containsKey(AppsContract.Apps.STATUS) == false) {
        values.put(AppsContract.Apps.STATUS, 0);
    }

    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    long rowId = db.insert(APPS_TABLE_NAME, null, values);

    if (rowId > 0) {
        Uri appUri = ContentUris.withAppendedId(AppsContract.Apps.CONTENT_URI, rowId);
        getContext().getContentResolver().notifyChange(appUri, null);
        return appUri;
    }

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

From source file:com.dm.material.dashboard.candybar.databases.Database.java

public void addRequest(Request request) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, request.getName());
    values.put(KEY_ACTIVITY, request.getActivity());

    db.insert(TABLE_REQUEST, null, values);
    db.close();//  w  w  w  .j ava 2s .  c  o  m
}

From source file:com.appsimobile.appsii.module.apps.AppsProvider.java

@Override
public Uri insert(Uri uri, ContentValues initialValues) {
    SqlArguments args = new SqlArguments(uri);

    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);/* w w w  .  j  av a2 s.  co m*/

    return uri;
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static void banUser(String decodedfrom, Context context) {
    DBHelper db = new DBHelper(context);
    SQLiteDatabase dbwrite = db.getWritableDatabase();

    Cursor c = dbwrite.rawQuery("SELECT _id FROM banned_users " + " WHERE name=" + esc(decodedfrom), null);

    if (c.getCount() > 0) {
        c.moveToFirst();//  ww w. j  a  v  a  2 s .  c  om
        dbwrite.execSQL("UPDATE banned_users SET bandisabled=0 WHERE _id=" + c.getInt(0));
    } else {
        ContentValues cv = new ContentValues();
        cv.put("name", decodedfrom);
        cv.put("bandisabled", 0);
        dbwrite.insert("banned_users", null, cv);
    }

    // Mark all the user posts as read, so they get deleted later
    dbwrite.execSQL("UPDATE headers SET read=1, read_unixdate=" + System.currentTimeMillis()
            + " WHERE from_header=" + esc(decodedfrom));

    c.close();
    dbwrite.close();
    db.close();
}

From source file:net.naonedbus.manager.impl.FavoriManager.java

public void addFavori(final SQLiteDatabase db, final Favori item) {
    final ContentValues values = getContentValues(item);
    db.insert(FavoriTable.TABLE_NAME, null, values);

    if (mIsImporting == false)
        notifyOnAdd(item);//  w  w w  .j a v  a2s.c  o m
}

From source file:com.dm.material.dashboard.candybar.databases.Database.java

public void addPremiumRequest(String orderId, String productId, String request) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_ORDER_ID, orderId);/* w ww  .  j a va  2s.c  o  m*/
    values.put(KEY_PRODUCT_ID, productId);
    values.put(KEY_REQUEST, request);

    db.insert(TABLE_PREMIUM_REQUEST, null, values);
    db.close();
}

From source file:com.example.petri.myapplication.GCMMessageListenerService.java

/**
 * Called when message is received./*from w w  w.  ja  v a  2  s  .  c  o m*/
 *
 * @param from SenderID of the sender.
 * @param data Data bundle containing message data as key/value pairs.
 *             For Set of keys use data.keySet().
 */
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    int from_id = Integer.parseInt(data.getString("from_id"));
    int to_id = Integer.parseInt(data.getString("to_id"));
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);
    DatabaseHelper helper = new DatabaseHelper(this);
    SQLiteDatabase db = helper.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("message", message);
    values.put("from_id", from_id);
    values.put("to_id", to_id);

    // Insert the new row, returning the primary key value of the new row
    long newRowId;
    newRowId = db.insert("messages", "message_id", values);
    String strSQL = "UPDATE chats SET last_activity = CURRENT_TIMESTAMP WHERE user_id = " + from_id;
    db.execSQL(strSQL);

    //        SQLiteDatabase dbr = helper.getReadableDatabase();

    //        Cursor cursor = dbr.query(false, "messages", new String[]{"message"}, null, null, null, null, null, null);
    //        cursor.moveToLast();
    //        String this_message = cursor.getString(
    //                cursor.getColumnIndexOrThrow("message")
    //        );
    //        Log.d("mygcm", this_message);

    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction(ChatActivity.MessageResponseReceiver.ACTION_RESP);
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    broadcastIntent.putExtra("from_id", from_id);
    LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);

    //        SQLiteDatabase mydatabase =
    //        mydatabase.execSQL("CREATE TABLE IF NOT EXISTS messages(message_id INT auto_increment, message TEXT, primary key(message_id));");
    //        mydatabase.execSQL("INSERT INTO TutorialsPoint VALUES('null','" + message + "');");
    /**
     * Production applications would usually process the message here.
     * Eg: - Syncing with server.
     *     - Store message in local database.
     *     - Update UI.
     */

    /**
     * In some cases it may be useful to show a notification indicating to the user
     * that a message was received.
     */
    sendNotification(message, from_id);
}

From source file:project.cs.lisa.database.IODatabase.java

/**
 * Inserts a value in the database.//  ww w. j  av a 2s  . c o m
 * 
 * @param table      the table where the values will be inserted
 * @param values   the values that will be inserted
 */
private void insert(String table, ContentValues values) {
    SQLiteDatabase db = this.getWritableDatabase();

    db.insert(table, null, values);

    db.close();
}

From source file:redgun.bakingapp.data.RecipesProvider.java

@Override
public Uri insert(Uri uri, ContentValues values) {
    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    final int match = sUriMatcher.match(uri);
    Uri returnUri;/*from  ww  w .j av  a2s  . c  o m*/

    switch (match) {
    case RECIPES: {
        long _id = db.insert(RecipesContract.RecipeEntry.TABLE_NAME, null, values);
        if (_id > 0)
            returnUri = RecipesContract.RecipeEntry.buildRecipesUri(_id);
        else
            throw new android.database.SQLException("Failed to insert row into " + uri);
        break;
    }
    default:
        throw new UnsupportedOperationException("Unknown uri: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return returnUri;
}