SQLiteStatement.java (5589B)
1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 /* 17 ** Modified to support SQLite extensions by the SQLite developers: 18 ** sqlite-dev@sqlite.org. 19 */ 20 21 package org.sqlite.database.sqlite; 22 23 import android.os.ParcelFileDescriptor; 24 25 /** 26 * Represents a statement that can be executed against a database. The statement 27 * cannot return multiple rows or columns, but single value (1 x 1) result sets 28 * are supported. 29 * <p> 30 * This class is not thread-safe. 31 * </p> 32 */ 33 public final class SQLiteStatement extends SQLiteProgram { 34 SQLiteStatement(SQLiteDatabase db, String sql, Object[] bindArgs) { 35 super(db, sql, bindArgs, null); 36 } 37 38 /** 39 * Execute this SQL statement, if it is not a SELECT / INSERT / DELETE / UPDATE, for example 40 * CREATE / DROP table, view, trigger, index etc. 41 * 42 * @throws android.database.SQLException If the SQL string is invalid for 43 * some reason 44 */ 45 public void execute() { 46 acquireReference(); 47 try { 48 getSession().execute(getSql(), getBindArgs(), getConnectionFlags(), null); 49 } catch (SQLiteDatabaseCorruptException ex) { 50 onCorruption(); 51 throw ex; 52 } finally { 53 releaseReference(); 54 } 55 } 56 57 /** 58 * Execute this SQL statement, if the the number of rows affected by execution of this SQL 59 * statement is of any importance to the caller - for example, UPDATE / DELETE SQL statements. 60 * 61 * @return the number of rows affected by this SQL statement execution. 62 * @throws android.database.SQLException If the SQL string is invalid for 63 * some reason 64 */ 65 public int executeUpdateDelete() { 66 acquireReference(); 67 try { 68 return getSession().executeForChangedRowCount( 69 getSql(), getBindArgs(), getConnectionFlags(), null); 70 } catch (SQLiteDatabaseCorruptException ex) { 71 onCorruption(); 72 throw ex; 73 } finally { 74 releaseReference(); 75 } 76 } 77 78 /** 79 * Execute this SQL statement and return the ID of the row inserted due to this call. 80 * The SQL statement should be an INSERT for this to be a useful call. 81 * 82 * @return the row ID of the last row inserted, if this insert is successful. -1 otherwise. 83 * 84 * @throws android.database.SQLException If the SQL string is invalid for 85 * some reason 86 */ 87 public long executeInsert() { 88 acquireReference(); 89 try { 90 return getSession().executeForLastInsertedRowId( 91 getSql(), getBindArgs(), getConnectionFlags(), null); 92 } catch (SQLiteDatabaseCorruptException ex) { 93 onCorruption(); 94 throw ex; 95 } finally { 96 releaseReference(); 97 } 98 } 99 100 /** 101 * Execute a statement that returns a 1 by 1 table with a numeric value. 102 * For example, SELECT COUNT(*) FROM table; 103 * 104 * @return The result of the query. 105 * 106 * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows 107 */ 108 public long simpleQueryForLong() { 109 acquireReference(); 110 try { 111 return getSession().executeForLong( 112 getSql(), getBindArgs(), getConnectionFlags(), null); 113 } catch (SQLiteDatabaseCorruptException ex) { 114 onCorruption(); 115 throw ex; 116 } finally { 117 releaseReference(); 118 } 119 } 120 121 /** 122 * Execute a statement that returns a 1 by 1 table with a text value. 123 * For example, SELECT COUNT(*) FROM table; 124 * 125 * @return The result of the query. 126 * 127 * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows 128 */ 129 public String simpleQueryForString() { 130 acquireReference(); 131 try { 132 return getSession().executeForString( 133 getSql(), getBindArgs(), getConnectionFlags(), null); 134 } catch (SQLiteDatabaseCorruptException ex) { 135 onCorruption(); 136 throw ex; 137 } finally { 138 releaseReference(); 139 } 140 } 141 142 /** 143 * Executes a statement that returns a 1 by 1 table with a blob value. 144 * 145 * @return A read-only file descriptor for a copy of the blob value, or {@code null} 146 * if the value is null or could not be read for some reason. 147 * 148 * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows 149 */ 150 public ParcelFileDescriptor simpleQueryForBlobFileDescriptor() { 151 acquireReference(); 152 try { 153 return getSession().executeForBlobFileDescriptor( 154 getSql(), getBindArgs(), getConnectionFlags(), null); 155 } catch (SQLiteDatabaseCorruptException ex) { 156 onCorruption(); 157 throw ex; 158 } finally { 159 releaseReference(); 160 } 161 } 162 163 @Override 164 public String toString() { 165 return "SQLiteProgram: " + getSql(); 166 } 167 }