/**
  * Bind the value to an index. A prepareForInsert() or prepareForReplace() without a matching
  * execute() must have already have been called.
  *
  * @param index the index of the slot to which to bind
  * @param value the value to bind
  */
 public void bind(int index, String value) {
   if (value == null) {
     mPreparedStatement.bindNull(index);
   } else {
     mPreparedStatement.bindString(index, value);
   }
 }
 /**
  * Bind the value to an index. A prepareForInsert() or prepareForReplace() without a matching
  * execute() must have already have been called.
  *
  * @param index the index of the slot to which to bind
  * @param value the value to bind
  */
 public void bind(int index, byte[] value) {
   if (value == null) {
     mPreparedStatement.bindNull(index);
   } else {
     mPreparedStatement.bindBlob(index, value);
   }
 }
 /**
  * Utility method to run the query on the db and return the value in the first column of the first
  * row.
  */
 public static String stringForQuery(SQLiteDatabase db, String query, String[] selectionArgs) {
   SQLiteStatement prog = db.compileStatement(query);
   try {
     return stringForQuery(prog, selectionArgs);
   } finally {
     prog.close();
   }
 }
 /**
  * Close this object and release any resources associated with it. The behavior of calling
  * <code>insert()</code> after calling this method is undefined.
  */
 public void close() {
   if (mInsertStatement != null) {
     mInsertStatement.close();
     mInsertStatement = null;
   }
   if (mReplaceStatement != null) {
     mReplaceStatement.close();
     mReplaceStatement = null;
   }
   mInsertSQL = null;
   mColumns = null;
 }
 /**
  * Performs an insert, adding a new row with the given values.
  *
  * @param values the set of values with which to populate the new row
  * @param allowReplace if true, the statement does "INSERT OR REPLACE" instead of "INSERT",
  *     silently deleting any previously existing rows that would cause a conflict
  * @return the row ID of the newly inserted row, or -1 if an error occurred
  */
 private synchronized long insertInternal(ContentValues values, boolean allowReplace) {
   try {
     SQLiteStatement stmt = getStatement(allowReplace);
     stmt.clearBindings();
     if (LOCAL_LOGV) Log.v(TAG, "--- inserting in table " + mTableName);
     for (Map.Entry<String, Object> e : values.valueSet()) {
       final String key = e.getKey();
       int i = getColumnIndex(key);
       DatabaseUtils.bindObjectToProgram(stmt, i, e.getValue());
       if (LOCAL_LOGV) {
         Log.v(TAG, "binding " + e.getValue() + " to column " + i + " (" + key + ")");
       }
     }
     return stmt.executeInsert();
   } catch (SQLException e) {
     Log.e(TAG, "Error inserting " + values + " into table  " + mTableName, e);
     return -1;
   }
 }
 /**
  * Utility method to run the pre-compiled query and return the value in the first column of the
  * first row.
  */
 public static String stringForQuery(SQLiteStatement prog, String[] selectionArgs) {
   if (selectionArgs != null) {
     int size = selectionArgs.length;
     for (int i = 0; i < size; i++) {
       bindObjectToProgram(prog, i + 1, selectionArgs[i]);
     }
   }
   String value = prog.simpleQueryForString();
   return value;
 }
Beispiel #7
0
  /** @inheritdoc */
  @Override
  protected void bindValues(SQLiteStatement stmt, SqliteMaster entity) {
    stmt.clearBindings();

    String type = entity.getType();
    if (type != null) {
      stmt.bindString(1, type);
    }

    String name = entity.getName();
    if (name != null) {
      stmt.bindString(2, name);
    }

    String tableName = entity.getTableName();
    if (tableName != null) {
      stmt.bindString(3, tableName);
    }

    Long rootpage = entity.getRootpage();
    if (rootpage != null) {
      stmt.bindLong(4, rootpage);
    }

    String sql = entity.getSql();
    if (sql != null) {
      stmt.bindString(5, sql);
    }
  }
 /**
  * Execute the previously prepared insert or replace using the bound values since the last call
  * to prepareForInsert or prepareForReplace.
  *
  * <p>Note that calling bind() and then execute() is not thread-safe. The only thread-safe way
  * to use this class is to call insert() or replace().
  *
  * @return the row ID of the newly inserted row, or -1 if an error occurred
  */
 public long execute() {
   if (mPreparedStatement == null) {
     throw new IllegalStateException(
         "you must prepare this inserter before calling " + "execute");
   }
   try {
     if (LOCAL_LOGV) Log.v(TAG, "--- doing insert or replace in table " + mTableName);
     return mPreparedStatement.executeInsert();
   } catch (SQLException e) {
     Log.e(TAG, "Error executing InsertHelper with table " + mTableName, e);
     return -1;
   } finally {
     // you can only call this once per prepare
     mPreparedStatement = null;
   }
 }
 /**
  * Bind null to an index. A prepareForInsert() or prepareForReplace() without a matching
  * execute() must have already have been called.
  *
  * @param index the index of the slot to which to bind
  */
 public void bindNull(int index) {
   mPreparedStatement.bindNull(index);
 }
 /**
  * Bind the value to an index. A prepareForInsert() or prepareForReplace() without a matching
  * execute() must have already have been called.
  *
  * @param index the index of the slot to which to bind
  * @param value the value to bind
  */
 public void bind(int index, boolean value) {
   mPreparedStatement.bindLong(index, value ? 1 : 0);
 }
 /**
  * Bind the value to an index. A prepareForInsert() or prepareForReplace() without a matching
  * execute() must have already have been called.
  *
  * @param index the index of the slot to which to bind
  * @param value the value to bind
  */
 public void bind(int index, int value) {
   mPreparedStatement.bindLong(index, value);
 }
 /**
  * Bind the value to an index. A prepareForInsert() or prepareForReplace() without a matching
  * execute() must have already have been called.
  *
  * @param index the index of the slot to which to bind
  * @param value the value to bind
  */
 public void bind(int index, float value) {
   mPreparedStatement.bindDouble(index, value);
 }
 /**
  * Prepare the InsertHelper for a replace. The pattern for this is:
  *
  * <ul>
  *   <li>prepareForReplace()
  *   <li>bind(index, value);
  *   <li>bind(index, value);
  *   <li>...
  *   <li>bind(index, value);
  *   <li>execute();
  * </ul>
  */
 public void prepareForReplace() {
   mPreparedStatement = getStatement(true);
   mPreparedStatement.clearBindings();
 }
 /**
  * Prepare the InsertHelper for an insert. The pattern for this is:
  *
  * <ul>
  *   <li>prepareForInsert()
  *   <li>bind(index, value);
  *   <li>bind(index, value);
  *   <li>...
  *   <li>bind(index, value);
  *   <li>execute();
  * </ul>
  */
 public void prepareForInsert() {
   mPreparedStatement = getStatement(false);
   mPreparedStatement.clearBindings();
 }