@Override public String call(final SQLiteStatement statement) throws Exception { if (!statement.step()) { throw new SQLiteException(SQLiteConstants.SQLITE_DONE, "No rows returned from query"); } return statement.columnString(0); } });
@Override public Void call() throws Exception { statement.dispose(); return null; } });
@Override public Void call(final SQLiteStatement statement) throws Exception { statement.bind(index, value); return null; } });
@Override public Long call(final SQLiteStatement statement) throws Exception { if (!statement.step()) { throw new SQLiteException(SQLiteConstants.SQLITE_DONE, "No rows returned from query"); } return statement.columnLong(0); } });
private static Row fillRowValues(SQLiteStatement stmt) throws SQLiteException { final int columnCount = stmt.columnCount(); Row row = new Row(columnCount); for (int index = 0; index < columnCount; index++) { row.set(index, new Value(stmt.columnValue(index), cursorValueType(stmt.columnType(index)))); } return row; }
try { st = prepare(sql); boolean r = st.step(); if (!r) { return ""; int columns = st.columnCount(); if (columns == 0) { return ""; String[] columnNames = new String[columns]; for (int i = 0; i < columns; i++) { columnNames[i] = String.valueOf(st.getColumnName(i)); widths[i] = columnNames[i].length(); do { for (int i = 0; i < columns; i++) { String v = st.columnNull(i) ? "<null>" : String.valueOf(st.columnValue(i)); cells.add(v); widths[i] = Math.max(widths[i], v.length()); } while (st.step()); return e.getMessage(); } finally { if (st != null) st.dispose();
@Override public void endWindow() { try { commitStatement.step(); commitStatement.reset(); if (bindings != null) { for (int i = 0; i < bindings.size(); i++) { execStatement.bind(i, bindings.get(i).toString()); } } int columnCount = execStatement.columnCount(); while (execStatement.step()) { HashMap<String, Object> resultRow = new HashMap<String, Object>(); for (int i = 0; i < columnCount; i++) { resultRow.put(execStatement.getColumnName(i), execStatement.columnValue(i)); } this.result.emit(resultRow); } execStatement.reset(); for (SQLiteStatement st : deleteStatements) { st.step(); st.reset(); } } catch (SQLiteException ex) { throw new RuntimeException(ex); } bindings = null; }
SQLiteConnection db = new SQLiteConnection(new File("/tmp/database")); db.open(true); ... SQLiteStatement st = db.prepare("SELECT order_id FROM orders WHERE quantity >= ?"); try { st.bind(1, minimumQuantity); while (st.step()) { orders.add(st.columnLong(0)); } } finally { st.dispose(); } ... db.dispose();
static Tuple getDataRow(SQLiteStatement stmt) throws SQLiteException { logger.entering("com.cloudant.sync.internal.sqlite.sqlite4java.SQLiteWrapperUtils","getDataRow",stmt); Tuple result = new Tuple(getColumnTypes(stmt)); for (int i = 0; i < stmt.columnCount(); i++) { Integer type = stmt.columnType(i); // Log.v(LOG_TAG, "i: " + i + ", type: " + mapColumnType(type) + ", expected type: " + result.getType(i)); switch (type) { case SQLiteConstants.SQLITE_NULL: result.put(i); break; case SQLiteConstants.SQLITE_TEXT: result.put(i, stmt.columnString(i)); break; case SQLiteConstants.SQLITE_INTEGER: result.put(i, stmt.columnLong(i)); break; case SQLiteConstants.SQLITE_FLOAT: result.put(i, Double.valueOf(stmt.columnDouble(i)).floatValue()); break; case SQLiteConstants.SQLITE_BLOB: result.put(i, stmt.columnBlob(i)); break; default: throw new IllegalArgumentException("Unsupported data type: " + type); } } return result; }
@Override public void processTuple(int tableNum, HashMap<String, Object> tuple) { InputSchema inputSchema = inputSchemas.get(tableNum); SQLiteStatement insertStatement = insertStatements.get(tableNum); try { for (Map.Entry<String, Object> entry : tuple.entrySet()) { ColumnInfo t = inputSchema.columnInfoMap.get(entry.getKey()); if (t != null && t.bindIndex != 0) { insertStatement.bind(t.bindIndex, entry.getValue().toString()); } } insertStatement.step(); insertStatement.reset(); } catch (SQLiteException ex) { throw new RuntimeException(ex); } }
import com.almworks.sqlite4java.SQLiteConnection; import com.almworks.sqlite4java.SQLiteStatement; import org.junit.Test; import java.io.File; public class SqliteTest { @Test public void aTest() throws Exception { SQLiteConnection db = new SQLiteConnection(new File("/tmp/database")); db.open(true); SQLiteStatement st = db.prepare("SELECT name FROM dummy"); try { while(st.step()) { System.err.printf("name = %s\n", st.columnString(1)); } } finally { st.dispose(); } } }
static List<String> getColumnNames(SQLiteStatement stmt) throws SQLiteException { // Log.v(LOG_TAG, "getColumnNames()"); List<String> columnNames = new ArrayList<String>(); int columnCount = stmt.columnCount(); for (int i = 0; i < columnCount; i++) { columnNames.add(i, stmt.getColumnName(i)); } // Log.v(LOG_TAG, "columnNames:" + columnNames); return columnNames; }
if (count != stmt.getBindParameterCount()) { throw new IllegalArgumentException( "Expected " + stmt.getBindParameterCount() + " bind arguments but " + bindArgs.length + " were provided."); switch (DBUtils.getTypeOfObject(arg)) { case Cursor.FIELD_TYPE_NULL: stmt.bindNull(i + 1); break; case Cursor.FIELD_TYPE_INTEGER: stmt.bind(i + 1, ((Number) arg).longValue()); break; case Cursor.FIELD_TYPE_FLOAT: stmt.bind(i + 1, ((Number) arg).doubleValue()); break; case Cursor.FIELD_TYPE_BLOB: stmt.bind(i + 1, (byte[]) arg); break; case Cursor.FIELD_TYPE_STRING: stmt.bind(i + 1, ((Boolean) arg).booleanValue() ? 1 : 0); } else { stmt.bind(i + 1, arg.toString());
/** * Binds SQL parameter to a BLOB value, represented by a byte array. * * @param index the index of the boundable parameter, starting with 1 * @param value an array of bytes to be used as the blob value; if null, {@link #bindNull} is called * @return this object * @throws SQLiteException if SQLite returns an error, or if the call violates the contract of this class * @see <a href="http://www.sqlite.org/c3ref/bind_blob.html">sqlite3_bind_blob</a> */ public SQLiteStatement bind(int index, byte[] value) throws SQLiteException { return value == null ? bindNull(index) : bind(index, value, 0, value.length); }
static void disposeQuietly(SQLiteStatement stmt) { if (stmt != null && !stmt.isDisposed()) { try { stmt.dispose(); } catch (Throwable e) {} } }
@Override public Void call(final SQLiteStatement statement) throws Exception { statement.bindNull(index); return null; } });
public void fillWith(SQLiteStatement stmt) throws SQLiteException { //Android caches results in the WindowedCursor to allow moveToPrevious() to function. //Robolectric will have to cache the results too. In the rows list. while (stmt.step()) { rows.add(fillRowValues(stmt)); } }
@Override public Integer call(final SQLiteStatement statement) throws Exception { return statement.columnCount(); } });
@Override public String call(final SQLiteStatement statement) throws Exception { return statement.getColumnName(index); } });