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:org.egov.android.data.SQLiteHelper.java

/**
 * Insert data into table/*from  w w  w .  j a va 2 s  .c o m*/
 * 
 * @param tableName
 * @param ContentValues
 */

public long insert(String tableName, ContentValues cv) {
    SQLiteDatabase db = getWritableDatabase();
    long result = db.insert(tableName, null, cv);
    db.close();
    return result;
}

From source file:com.manning.androidhacks.hack023.provider.TodoContentProvider.java

@Override
public Uri insert(Uri uri, ContentValues initialValues) {
    ContentValues values;/*from  w  w  w.  j a v  a2 s . c  om*/
    if (initialValues != null) {
        values = new ContentValues(initialValues);
    } else {
        values = new ContentValues();
    }
    String table = null;
    String nullableCol = null;

    switch (sUriMatcher.match(uri)) {
    case TODO:
        table = TODO_TABLE_NAME;
        nullableCol = COLUMN_TITLE;
        break;
    default:
        new RuntimeException("Invalid URI for inserting: " + uri);
    }

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    long rowId = db.insert(table, nullableCol, values);

    if (rowId > 0) {
        Uri noteUri = ContentUris.withAppendedId(uri, rowId);
        getContext().getContentResolver().notifyChange(noteUri, null);
        return noteUri;
    }

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

From source file:com.manning.androidhacks.hack042.AddPoiActivity.java

public void onAddClick(View v) {
    String title = mTitleEditText.getText().toString();
    String latitude = mLatitudeEditText.getText().toString();
    String longitude = mLongitudeEditText.getText().toString();

    DatabaseHelper dbHelper = new DatabaseHelper(this);
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put("title", title);
    cv.put("latitude", latitude);
    cv.put("longitude", longitude);

    db.insert("pois", null, cv);
    finish();//from   www.j  a v  a  2 s.  c om
}

From source file:nerd.tuxmobil.fahrplan.congress.FahrplanMisc.java

public static void writeHighlight(Context context, Lecture lecture) {
    HighlightDBOpenHelper highlightDB = new HighlightDBOpenHelper(context);

    SQLiteDatabase db = highlightDB.getWritableDatabase();

    try {//  w ww  .  j  a  va 2s.  co m
        db.beginTransaction();
        db.delete(HighlightsTable.NAME, HighlightsTable.Columns.EVENT_ID + "=?",
                new String[] { lecture.lecture_id });

        ContentValues values = new ContentValues();

        values.put(HighlightsTable.Columns.EVENT_ID, Integer.parseInt(lecture.lecture_id));
        int highlightState = lecture.highlight ? HighlightsTable.Values.HIGHLIGHT_STATE_ON
                : HighlightsTable.Values.HIGHLIGHT_STATE_OFF;
        values.put(HighlightsTable.Columns.HIGHLIGHT, highlightState);

        db.insert(HighlightsTable.NAME, null, values);
        db.setTransactionSuccessful();
    } catch (SQLException e) {
    } finally {
        db.endTransaction();
        db.close();
    }
}

From source file:com.cuddlesoft.nori.database.APISettingsDatabase.java

/**
 * Insert a new {@link SearchClient.Settings} into the database.
 *
 * @param settings Settings object./*  w w  w .j a  v  a2 s.  c om*/
 * @return ID of the newly inserted row.
 */
public long insert(SearchClient.Settings settings) {
    // Insert data into the database.
    SQLiteDatabase db = getWritableDatabase();
    final long id = db.insert(TABLE_NAME, null, searchClientSettingsToContentValues(settings));
    db.close();

    sendUpdateNotification();
    return id;
}

From source file:com.wit.android.support.database.loremipsum.BaseLoremIpsumEntity.java

/**
 *//*from   w  w w. j av  a  2s. c o  m*/
@Override
protected void onCreateDefaultContent(@NonNull SQLiteDatabase db, @NonNull Context context) {
    for (int i = 0; i < mDefaultContentSize; i++) {
        db.insert(ENTITY_NAME, null, new LoremIpsum(i).toContentValues());
    }
}

From source file:com.wit.android.support.database.examples.database.MoviesEntity.java

/**
 *///from  w w  w .  java  2s .  co m
@Override
protected void onCreateDefaultContent(@NonNull SQLiteDatabase db, @NonNull Context context) {
    Movie movie;
    for (int i = 0; i < 5; i++) {
        movie = new Movie();
        movie.setId(i);
        movie.setTitle("Movie no. " + Integer.toString(i + 1));
        db.insert(ENTITY_NAME, null, movie.toContentValues());
    }
}

From source file:net.olejon.mdapp.MyTools.java

public void saveArticle(String title, String uri, String webview) {
    String domain = uri.replaceAll("https?://", "").replaceAll("[w]{3}\\.", "").replaceAll("/.*", "");

    ContentValues savedArticlesContentValues = new ContentValues();
    savedArticlesContentValues.put(SavedArticlesSQLiteHelper.COLUMN_TITLE, title);
    savedArticlesContentValues.put(SavedArticlesSQLiteHelper.COLUMN_DOMAIN, domain);
    savedArticlesContentValues.put(SavedArticlesSQLiteHelper.COLUMN_URI, uri);
    savedArticlesContentValues.put(SavedArticlesSQLiteHelper.COLUMN_WEBVIEW, webview);

    SQLiteDatabase savedArticlesSqLiteDatabase = new SavedArticlesSQLiteHelper(mContext).getWritableDatabase();

    savedArticlesSqLiteDatabase.insert(SavedArticlesSQLiteHelper.TABLE, null, savedArticlesContentValues);

    savedArticlesSqLiteDatabase.close();

    showToast(mContext.getString(R.string.saved_articles_article_saved), 0);
}

From source file:com.gmail.emerssso.srbase.database.SRContentProvider.java

private Uri insertIntoTable(String table, Uri contentUri, Uri requestedUri, ContentValues values,
        SQLiteDatabase sqlDB) {
    long id = sqlDB.insert(table, null, values);
    getContext().getContentResolver().notifyChange(requestedUri, null);
    return Uri.parse(contentUri + "/" + id);
}

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

public static void setAuthorFavorite(boolean isFavorite, boolean mustBeFavorite, String author,
        Context context) {/* w w  w.j  a  v  a  2s  .  c o  m*/

    if (isFavorite && mustBeFavorite || !isFavorite && !mustBeFavorite)
        return;

    DBHelper dbhelper = new DBHelper(context);
    SQLiteDatabase dbwrite = dbhelper.getWritableDatabase();

    if (isFavorite && !mustBeFavorite) {
        // Remove from the table
        dbwrite.execSQL("DELETE FROM favorite_users WHERE name=" + esc(author));
    } else if (!isFavorite && mustBeFavorite) {
        // Insert into the table
        ContentValues cv = new ContentValues();
        cv.put("name", author);
        dbwrite.insert("favorite_users", null, cv);
    }
    dbwrite.close();
    dbhelper.close();
}