使用SQLiteDatabase需要import android.database.sqlite.SQLiteDatabase
SQLiteDatabase類別用於管理資料庫,可以Create,Delete,執行SQL Command。
常用Method
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
|
public void execSQL (String sql)
Added in API level 1
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
It has no means to return any data (such as the number of affected rows). Instead, you're encouraged to use
insert(String, String, ContentValues)
,update(String, ContentValues, String, String[])
, et al, when possible.
When using " statement if your app is using
enableWriteAheadLogging()
, journal_mode is automatically managed by this class. So, do not set journal_mode using "PRAGMA journal_mode'enableWriteAheadLogging()
Parameters
sql | the SQL statement to be executed. Multiple statements separated by semicolons are not supported. |
---|
Throws
SQLException | if the SQL string is invalid |
---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Begins a transaction in EXCLUSIVE mode.
| |||||||||||
Begins a transaction in IMMEDIATE mode.
| |||||||||||
Begins a transaction in EXCLUSIVE mode.
| |||||||||||
Begins a transaction in IMMEDIATE mode.
| |||||||||||
Compiles an SQL statement into a reusable pre-compiled statement object.
| |||||||||||
Create a memory backed SQLite database.
| |||||||||||
Convenience method for deleting rows in the database.
| |||||||||||
Deletes a database including its journal file and other auxiliary files that may have been created by the database engine.
| |||||||||||
This method disables the features enabled by
enableWriteAheadLogging() . | |||||||||||
This method enables parallel execution of queries from multiple threads on the same database.
| |||||||||||
End a transaction.
| |||||||||||
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
| |||||||||||
Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
| |||||||||||
Finds the name of the first table, which is editable.
| |||||||||||
Returns list of full pathnames of all attached databases including the main database by executing 'pragma database_list' on the database.
| |||||||||||
Returns the maximum size the database may grow to.
| |||||||||||
Returns the current database page size, in bytes.
| |||||||||||
Gets the path to the database file.
| |||||||||||
This method was deprecated in API level 11. This method no longer serves any useful purpose and has been deprecated.
| |||||||||||
Gets the database version.
| |||||||||||
Returns true if the current thread has a transaction pending.
| |||||||||||
Convenience method for inserting a row into the database.
| |||||||||||
Convenience method for inserting a row into the database.
| |||||||||||
General method for inserting a row into the database.
| |||||||||||
Runs 'pragma integrity_check' on the given database (and all the attached databases) and returns true if the given database (and all its attached databases) pass integrity_check, false otherwise.
| |||||||||||
Returns true if the current thread is holding an active connection to the database.
| |||||||||||
This method was deprecated in API level 16. Always returns false. Do not use this method.
| |||||||||||
Returns true if the database is currently open.
| |||||||||||
Returns true if the database is opened as read only.
| |||||||||||
Returns true if write-ahead logging has been enabled for this database.
| |||||||||||
This method was deprecated in API level 11. This method no longer serves any useful purpose and has been deprecated.
| |||||||||||
This method was deprecated in API level 11. This method no longer serves any useful purpose and has been deprecated.
| |||||||||||
Returns true if the new version code is greater than the current database version.
| |||||||||||
Open the database according to the flags
OPEN_READWRITE OPEN_READONLY CREATE_IF_NECESSARY and/or NO_LOCALIZED_COLLATORS . | |||||||||||
Open the database according to the flags
OPEN_READWRITE OPEN_READONLY CREATE_IF_NECESSARY and/or NO_LOCALIZED_COLLATORS . | |||||||||||
Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY, errorHandler).
| |||||||||||
Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY).
| |||||||||||
Equivalent to openDatabase(file.getPath(), factory, CREATE_IF_NECESSARY).
| |||||||||||
Query the given table, returning a
Cursor over the result set. | |||||||||||
Query the given URL, returning a
Cursor over the result set. | |||||||||||
Query the given table, returning a
Cursor over the result set. | |||||||||||
Query the given URL, returning a
Cursor over the result set. | |||||||||||
Query the given URL, returning a
Cursor over the result set. | |||||||||||
Query the given URL, returning a
Cursor over the result set. | |||||||||||
Runs the provided SQL and returns a
Cursor over the result set. | |||||||||||
Runs the provided SQL and returns a
Cursor over the result set. | |||||||||||
Runs the provided SQL and returns a cursor over the result set.
| |||||||||||
Runs the provided SQL and returns a cursor over the result set.
| |||||||||||
Attempts to release memory that SQLite holds but does not require to operate properly.
| |||||||||||
Convenience method for replacing a row in the database.
| |||||||||||
Convenience method for replacing a row in the database.
| |||||||||||
Sets whether foreign key constraints are enabled for the database.
| |||||||||||
Sets the locale for this database.
| |||||||||||
This method was deprecated in API level 16. This method now does nothing. Do not use.
| |||||||||||
Sets the maximum size of the prepared-statement cache for this database.
| |||||||||||
Sets the maximum size the database will grow to.
| |||||||||||
Sets the database page size.
| |||||||||||
Marks the current transaction as successful.
| |||||||||||
Sets the database version.
| |||||||||||
Returns a string containing a concise, human-readable description of this object.
| |||||||||||
Convenience method for updating rows in the database.
| |||||||||||
Convenience method for updating rows in the database.
| |||||||||||
This method was deprecated in API level 3. if the db is locked more than once (becuase of nested transactions) then the lock will not be yielded. Use yieldIfContendedSafely instead.
| |||||||||||
Temporarily end the transaction to let other threads run.
| |||||||||||
Temporarily end the transaction to let other threads run.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Invoked when the garbage collector has detected that this instance is no longer reachable.
| |||||||||||
Called when the last reference to the object was released by a call to
releaseReference() or close() . |
Query範例
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, StringorderBy, String limit)
Added in API level 1
Query the given table, returning a
Cursor
over the result set.Parameters
table | The table name to compile the query against. |
---|---|
columns | A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used. |
selection | A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table. |
selectionArgs | You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings. |
groupBy | A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped. |
having | A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used. |
orderBy | How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered. |
limit | Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause. |
沒有留言:
張貼留言