SQLiteStatement.java revision c8e1f23891cff31a9da2bab412631ff770a92f56
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 17package android.database.sqlite; 18 19import android.os.SystemClock; 20 21/** 22 * A pre-compiled statement against a {@link SQLiteDatabase} that can be reused. 23 * The statement cannot return multiple rows, but 1x1 result sets are allowed. 24 * Don't use SQLiteStatement constructor directly, please use 25 * {@link SQLiteDatabase#compileStatement(String)} 26 */ 27public class SQLiteStatement extends SQLiteProgram 28{ 29 /** 30 * Don't use SQLiteStatement constructor directly, please use 31 * {@link SQLiteDatabase#compileStatement(String)} 32 * @param db 33 * @param sql 34 */ 35 /* package */ SQLiteStatement(SQLiteDatabase db, String sql) { 36 super(db, sql); 37 } 38 39 /** 40 * Execute this SQL statement, if it is not a query. For example, 41 * CREATE TABLE, DELTE, INSERT, etc. 42 * 43 * @throws android.database.SQLException If the SQL string is invalid for 44 * some reason 45 */ 46 public void execute() { 47 if (!mDatabase.isOpen()) { 48 throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); 49 } 50 long timeStart = SystemClock.uptimeMillis(); 51 mDatabase.lock(); 52 53 acquireReference(); 54 try { 55 native_execute(); 56 mDatabase.logTimeStat(mSql, timeStart); 57 } finally { 58 releaseReference(); 59 mDatabase.unlock(); 60 } 61 } 62 63 /** 64 * Execute this SQL statement and return the ID of the row inserted due to this call. 65 * The SQL statement should be an INSERT for this to be a useful call. 66 * 67 * @return the row ID of the last row inserted, if this insert is successful. -1 otherwise. 68 * 69 * @throws android.database.SQLException If the SQL string is invalid for 70 * some reason 71 */ 72 public long executeInsert() { 73 if (!mDatabase.isOpen()) { 74 throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); 75 } 76 long timeStart = SystemClock.uptimeMillis(); 77 mDatabase.lock(); 78 79 acquireReference(); 80 try { 81 native_execute(); 82 mDatabase.logTimeStat(mSql, timeStart); 83 return (mDatabase.lastChangeCount() > 0) ? mDatabase.lastInsertRow() : -1; 84 } finally { 85 releaseReference(); 86 mDatabase.unlock(); 87 } 88 } 89 90 /** 91 * Execute a statement that returns a 1 by 1 table with a numeric value. 92 * For example, SELECT COUNT(*) FROM table; 93 * 94 * @return The result of the query. 95 * 96 * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows 97 */ 98 public long simpleQueryForLong() { 99 if (!mDatabase.isOpen()) { 100 throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); 101 } 102 long timeStart = SystemClock.uptimeMillis(); 103 mDatabase.lock(); 104 105 acquireReference(); 106 try { 107 long retValue = native_1x1_long(); 108 mDatabase.logTimeStat(mSql, timeStart); 109 return retValue; 110 } finally { 111 releaseReference(); 112 mDatabase.unlock(); 113 } 114 } 115 116 /** 117 * Execute a statement that returns a 1 by 1 table with a text value. 118 * For example, SELECT COUNT(*) FROM table; 119 * 120 * @return The result of the query. 121 * 122 * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows 123 */ 124 public String simpleQueryForString() { 125 if (!mDatabase.isOpen()) { 126 throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); 127 } 128 long timeStart = SystemClock.uptimeMillis(); 129 mDatabase.lock(); 130 131 acquireReference(); 132 try { 133 String retValue = native_1x1_string(); 134 mDatabase.logTimeStat(mSql, timeStart); 135 return retValue; 136 } finally { 137 releaseReference(); 138 mDatabase.unlock(); 139 } 140 } 141 142 private final native void native_execute(); 143 private final native long native_1x1_long(); 144 private final native String native_1x1_string(); 145} 146