SQLiteStatement.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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
19/**
20 * A pre-compiled statement against a {@link SQLiteDatabase} that can be reused.
21 * The statement cannot return multiple rows, but 1x1 result sets are allowed.
22 * Don't use SQLiteStatement constructor directly, please use
23 * {@link SQLiteDatabase#compileStatement(String)}
24 */
25public class SQLiteStatement extends SQLiteProgram
26{
27    /**
28     * Don't use SQLiteStatement constructor directly, please use
29     * {@link SQLiteDatabase#compileStatement(String)}
30     * @param db
31     * @param sql
32     */
33    /* package */ SQLiteStatement(SQLiteDatabase db, String sql) {
34        super(db, sql);
35    }
36
37    /**
38     * Execute this SQL statement, if it is not a query. For example,
39     * CREATE TABLE, DELTE, INSERT, etc.
40     *
41     * @throws android.database.SQLException If the SQL string is invalid for
42     *         some reason
43     */
44    public void execute() {
45        mDatabase.lock();
46        acquireReference();
47        try {
48            native_execute();
49        } finally {
50            releaseReference();
51            mDatabase.unlock();
52        }
53    }
54
55    /**
56     * Execute this SQL statement and return the ID of the most
57     * recently inserted row.  The SQL statement should probably be an
58     * INSERT for this to be a useful call.
59     *
60     * @return the row ID of the last row inserted.
61     *
62     * @throws android.database.SQLException If the SQL string is invalid for
63     *         some reason
64     */
65    public long executeInsert() {
66        mDatabase.lock();
67        acquireReference();
68        try {
69            native_execute();
70            return mDatabase.lastInsertRow();
71        } finally {
72            releaseReference();
73            mDatabase.unlock();
74        }
75    }
76
77    /**
78     * Execute a statement that returns a 1 by 1 table with a numeric value.
79     * For example, SELECT COUNT(*) FROM table;
80     *
81     * @return The result of the query.
82     *
83     * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows
84     */
85    public long simpleQueryForLong() {
86        mDatabase.lock();
87        acquireReference();
88        try {
89            return native_1x1_long();
90        } finally {
91            releaseReference();
92            mDatabase.unlock();
93        }
94    }
95
96    /**
97     * Execute a statement that returns a 1 by 1 table with a text value.
98     * For example, SELECT COUNT(*) FROM table;
99     *
100     * @return The result of the query.
101     *
102     * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows
103     */
104    public String simpleQueryForString() {
105        mDatabase.lock();
106        acquireReference();
107        try {
108            return native_1x1_string();
109        } finally {
110            releaseReference();
111            mDatabase.unlock();
112        }
113    }
114
115    private final native void native_execute();
116    private final native long native_1x1_long();
117    private final native String native_1x1_string();
118}
119