blob: 42e7ac7e843100f511c7dae1642a8c3b3f85c249 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.database.sqlite;
18
Mathew Inwoodf86bea92018-08-10 16:10:20 +010019import android.annotation.UnsupportedAppUsage;
Bjorn Bringerta006b4722010-04-14 14:43:26 +010020import android.os.ParcelFileDescriptor;
Brad Fitzpatrickcfda9f32010-06-03 12:52:54 -070021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022/**
Jeff Browne5360fb2011-10-31 17:48:13 -070023 * Represents a statement that can be executed against a database. The statement
24 * cannot return multiple rows or columns, but single value (1 x 1) result sets
25 * are supported.
26 * <p>
27 * This class is not thread-safe.
28 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 */
Jeff Browne5360fb2011-10-31 17:48:13 -070030public final class SQLiteStatement extends SQLiteProgram {
Mathew Inwoodf86bea92018-08-10 16:10:20 +010031 @UnsupportedAppUsage
Jeff Browne5360fb2011-10-31 17:48:13 -070032 SQLiteStatement(SQLiteDatabase db, String sql, Object[] bindArgs) {
Jeff Brown75ea64f2012-01-25 19:37:13 -080033 super(db, sql, bindArgs, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034 }
35
36 /**
Vasu Norifb16cbd2010-07-25 16:38:48 -070037 * Execute this SQL statement, if it is not a SELECT / INSERT / DELETE / UPDATE, for example
38 * CREATE / DROP table, view, trigger, index etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 *
40 * @throws android.database.SQLException If the SQL string is invalid for
41 * some reason
42 */
43 public void execute() {
Jeff Browne5360fb2011-10-31 17:48:13 -070044 acquireReference();
45 try {
Jeff Brown75ea64f2012-01-25 19:37:13 -080046 getSession().execute(getSql(), getBindArgs(), getConnectionFlags(), null);
Jeff Browne5360fb2011-10-31 17:48:13 -070047 } catch (SQLiteDatabaseCorruptException ex) {
48 onCorruption();
49 throw ex;
50 } finally {
51 releaseReference();
52 }
Vasu Norifb16cbd2010-07-25 16:38:48 -070053 }
54
55 /**
kopriva219f7dc2018-10-09 13:42:28 -070056 * Execute this SQL statement, if the number of rows affected by execution of this SQL
Vasu Norifb16cbd2010-07-25 16:38:48 -070057 * statement is of any importance to the caller - for example, UPDATE / DELETE SQL statements.
58 *
59 * @return the number of rows affected by this SQL statement execution.
60 * @throws android.database.SQLException If the SQL string is invalid for
61 * some reason
62 */
63 public int executeUpdateDelete() {
Jeff Browne5360fb2011-10-31 17:48:13 -070064 acquireReference();
Vasu Nori24675612010-09-27 14:54:19 -070065 try {
Jeff Browne5360fb2011-10-31 17:48:13 -070066 return getSession().executeForChangedRowCount(
Jeff Brown75ea64f2012-01-25 19:37:13 -080067 getSql(), getBindArgs(), getConnectionFlags(), null);
Jeff Browne5360fb2011-10-31 17:48:13 -070068 } catch (SQLiteDatabaseCorruptException ex) {
69 onCorruption();
70 throw ex;
Vasu Nori24675612010-09-27 14:54:19 -070071 } finally {
Jeff Browne5360fb2011-10-31 17:48:13 -070072 releaseReference();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 }
74 }
75
76 /**
Vasu Nori5bf67242010-03-16 09:55:13 -070077 * Execute this SQL statement and return the ID of the row inserted due to this call.
78 * The SQL statement should be an INSERT for this to be a useful call.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 *
Vasu Nori5bf67242010-03-16 09:55:13 -070080 * @return the row ID of the last row inserted, if this insert is successful. -1 otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 *
82 * @throws android.database.SQLException If the SQL string is invalid for
83 * some reason
84 */
85 public long executeInsert() {
Jeff Browne5360fb2011-10-31 17:48:13 -070086 acquireReference();
Vasu Nori24675612010-09-27 14:54:19 -070087 try {
Jeff Browne5360fb2011-10-31 17:48:13 -070088 return getSession().executeForLastInsertedRowId(
Jeff Brown75ea64f2012-01-25 19:37:13 -080089 getSql(), getBindArgs(), getConnectionFlags(), null);
Jeff Browne5360fb2011-10-31 17:48:13 -070090 } catch (SQLiteDatabaseCorruptException ex) {
91 onCorruption();
92 throw ex;
Vasu Nori24675612010-09-27 14:54:19 -070093 } finally {
Jeff Browne5360fb2011-10-31 17:48:13 -070094 releaseReference();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 }
96 }
97
98 /**
99 * Execute a statement that returns a 1 by 1 table with a numeric value.
100 * For example, SELECT COUNT(*) FROM table;
101 *
102 * @return The result of the query.
103 *
104 * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows
105 */
106 public long simpleQueryForLong() {
Jeff Browne5360fb2011-10-31 17:48:13 -0700107 acquireReference();
Vasu Nori24675612010-09-27 14:54:19 -0700108 try {
Jeff Browne5360fb2011-10-31 17:48:13 -0700109 return getSession().executeForLong(
Jeff Brown75ea64f2012-01-25 19:37:13 -0800110 getSql(), getBindArgs(), getConnectionFlags(), null);
Jeff Browne5360fb2011-10-31 17:48:13 -0700111 } catch (SQLiteDatabaseCorruptException ex) {
112 onCorruption();
113 throw ex;
Vasu Nori24675612010-09-27 14:54:19 -0700114 } finally {
Jeff Browne5360fb2011-10-31 17:48:13 -0700115 releaseReference();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 }
117 }
118
119 /**
120 * Execute a statement that returns a 1 by 1 table with a text value.
121 * For example, SELECT COUNT(*) FROM table;
122 *
123 * @return The result of the query.
124 *
125 * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows
126 */
127 public String simpleQueryForString() {
Jeff Browne5360fb2011-10-31 17:48:13 -0700128 acquireReference();
Vasu Nori24675612010-09-27 14:54:19 -0700129 try {
Jeff Browne5360fb2011-10-31 17:48:13 -0700130 return getSession().executeForString(
Jeff Brown75ea64f2012-01-25 19:37:13 -0800131 getSql(), getBindArgs(), getConnectionFlags(), null);
Jeff Browne5360fb2011-10-31 17:48:13 -0700132 } catch (SQLiteDatabaseCorruptException ex) {
133 onCorruption();
134 throw ex;
Vasu Nori24675612010-09-27 14:54:19 -0700135 } finally {
Jeff Browne5360fb2011-10-31 17:48:13 -0700136 releaseReference();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 }
138 }
139
Vasu Nori75010102010-07-01 16:23:06 -0700140 /**
Bjorn Bringerta006b4722010-04-14 14:43:26 +0100141 * Executes a statement that returns a 1 by 1 table with a blob value.
142 *
143 * @return A read-only file descriptor for a copy of the blob value, or {@code null}
144 * if the value is null or could not be read for some reason.
145 *
146 * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows
147 */
148 public ParcelFileDescriptor simpleQueryForBlobFileDescriptor() {
Vasu Nori75010102010-07-01 16:23:06 -0700149 acquireReference();
Jeff Browne5360fb2011-10-31 17:48:13 -0700150 try {
151 return getSession().executeForBlobFileDescriptor(
Jeff Brown75ea64f2012-01-25 19:37:13 -0800152 getSql(), getBindArgs(), getConnectionFlags(), null);
Jeff Browne5360fb2011-10-31 17:48:13 -0700153 } catch (SQLiteDatabaseCorruptException ex) {
154 onCorruption();
155 throw ex;
156 } finally {
157 releaseReference();
158 }
Vasu Nori75010102010-07-01 16:23:06 -0700159 }
160
Jeff Browne5360fb2011-10-31 17:48:13 -0700161 @Override
162 public String toString() {
163 return "SQLiteProgram: " + getSql();
Vasu Nori75010102010-07-01 16:23:06 -0700164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165}