Most visited

Recently visited

Added in API level 1

SQLiteDatabase

public final class SQLiteDatabase
extends SQLiteClosable

java.lang.Object
   ↳ android.database.sqlite.SQLiteClosable
     ↳ android.database.sqlite.SQLiteDatabase


公开了管理SQLite数据库的方法。

SQLiteDatabase具有创建,删除,执行SQL命令以及执行其他常见数据库管理任务的方法。

有关创建和管理数据库的示例,请参阅SDK中的记事本示例应用程序。

数据库名称在应用程序中必须是唯一的,而不是跨所有应用程序

Localized Collation - ORDER BY

除SQLite默认的 BINARY整理器之外,Android还提供两个更新: LOCALIZED和系统的当前语言环境; UNICODE是Unicode排序算法,不针对当前语言环境。

Summary

Nested classes

interface SQLiteDatabase.CursorFactory

用于在调用查询时允许返回Cursor子类。

Constants

int CONFLICT_ABORT

发生约束冲突时,不会执行ROLLBACK,以便保留同一事务内先前命令的更改。

int CONFLICT_FAIL

发生约束冲突时,该命令将以返回代码SQLITE_CONSTRAINT中止。

int CONFLICT_IGNORE

发生约束冲突时,不会插入或更改包含约束冲突的一行。

int CONFLICT_NONE

未指定冲突操作时使用以下内容。

int CONFLICT_REPLACE

当发生UNIQUE约束冲突时,会在插入或更新当前行之前删除导致违反约束的预先存在的行。

int CONFLICT_ROLLBACK

发生约束冲突时,会立即发生ROLLBACK,从而结束当前事务,并且该命令将以SQLITE_CONSTRAINT的返回码中止。

int CREATE_IF_NECESSARY

打开标志:标记为 openDatabase(String, SQLiteDatabase.CursorFactory, int)以创建数据库文件(如果它尚不存在)。

int ENABLE_WRITE_AHEAD_LOGGING

打开标志:标记为 openDatabase(String, SQLiteDatabase.CursorFactory, int)以打开默认情况下启用预写日志记录的数据库文件。

int MAX_SQL_CACHE_SIZE

绝对最大值,可由 setMaxSqlCacheSize(int)设置。

int NO_LOCALIZED_COLLATORS

打开标志:标记为 openDatabase(String, SQLiteDatabase.CursorFactory, int)可以在不支持本地化的整理器的情况下打开数据库。

int OPEN_READONLY

打开标志:标记为 openDatabase(String, SQLiteDatabase.CursorFactory, int)以打开仅用于读取的数据库。

int OPEN_READWRITE

打开标志:标志为openDatabase(String, SQLiteDatabase.CursorFactory, int)打开数据库进行读写。 如果磁盘已满,甚至在您实际写入任何内容之前,这可能会失败。

int SQLITE_MAX_LIKE_PATTERN_LENGTH

LIKE或GLOB模式的最大长度在SQLite的默认LIKE和GLOB实现中使用的模式匹配算法对于某些病例可以表现出O(N ^ 2)性能(其中N是模式中的字符数)。

Public methods

void beginTransaction()

在EXCLUSIVE模式下开始交易。

void beginTransactionNonExclusive()

在IMMEDIATE模式下开始交易。

void beginTransactionWithListener(SQLiteTransactionListener transactionListener)

在EXCLUSIVE模式下开始交易。

void beginTransactionWithListenerNonExclusive(SQLiteTransactionListener transactionListener)

在IMMEDIATE模式下开始交易。

SQLiteStatement compileStatement(String sql)

将SQL语句编译为可重用的预编译语句对象。

static SQLiteDatabase create(SQLiteDatabase.CursorFactory factory)

创建一个支持内存的SQLite数据库。

int delete(String table, String whereClause, String[] whereArgs)

删除数据库中的行的便捷方法。

static boolean deleteDatabase(File file)

删除包含其日志文件和可能由数据库引擎创建的其他辅助文件的数据库。

void disableWriteAheadLogging()

此方法禁用由 enableWriteAheadLogging()启用的 enableWriteAheadLogging()

boolean enableWriteAheadLogging()

此方法可以在同一数据库上并行执行来自多个线程的查询。

void endTransaction()

结束交易。

void execSQL(String sql)

执行不是SELECT或任何其他返回数据的SQL语句的单个SQL语句。

void execSQL(String sql, Object[] bindArgs)

执行不是SELECT / INSERT / UPDATE / DELETE的单个SQL语句。

static String findEditTable(String tables)

查找可编辑的第一个表的名称。

List<Pair<StringString>> getAttachedDbs()

通过在数据库上执行'pragma database_list'返回包括主数据库在内的所有连接数据库的完整路径名的列表。

long getMaximumSize()

返回数据库可能增长到的最大大小。

long getPageSize()

返回当前数据库页面大小(以字节为单位)。

final String getPath()

获取数据库文件的路径。

Map<StringString> getSyncedTables()

此方法在API级别11中已被弃用。此方法不再用于任何有用的目的,并且已被弃用。

int getVersion()

获取数据库版本。

boolean inTransaction()

如果当前线程有一个待处理的事务,则返回true。

long insert(String table, String nullColumnHack, ContentValues values)

将行插入数据库的便捷方法。

long insertOrThrow(String table, String nullColumnHack, ContentValues values)

将行插入数据库的便捷方法。

long insertWithOnConflict(String table, String nullColumnHack, ContentValues initialValues, int conflictAlgorithm)

将行插入数据库的一般方法。

boolean isDatabaseIntegrityOk()

在给定数据库(以及所有附加数据库)上运行'pragma integrity_check',如果给定数据库(及其所有附加数据库)通过integrity_check,则返回true,否则返回false。

boolean isDbLockedByCurrentThread()

如果当前线程持有到数据库的活动连接,则返回true。

boolean isDbLockedByOtherThreads()

此方法在API级别16中已弃用。始终返回false。 不要使用这种方法。

boolean isOpen()

如果数据库当前打开,则返回true。

boolean isReadOnly()

如果数据库以只读方式打开,则返回true。

boolean isWriteAheadLoggingEnabled()

如果为此数据库启用预写日志记录,则返回true。

void markTableSyncable(String table, String deletedTable)

此方法在API级别11中已被弃用。此方法不再用于任何有用的目的,并且已被弃用。

void markTableSyncable(String table, String foreignKey, String updateTable)

此方法在API级别11中已被弃用。此方法不再用于任何有用的目的,并且已被弃用。

boolean needUpgrade(int newVersion)

如果新版本代码大于当前数据库版本,则返回true。

static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags)

根据标志 OPEN_READWRITE OPEN_READONLY CREATE_IF_NECESSARY和/或 NO_LOCALIZED_COLLATORS打开数据库。

static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags, DatabaseErrorHandler errorHandler)

根据标志 OPEN_READWRITE OPEN_READONLY CREATE_IF_NECESSARY和/或 NO_LOCALIZED_COLLATORS打开数据库。

static SQLiteDatabase openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory)

相当于openDatabase(file.getPath(),工厂,CREATE_IF_NECESSARY)。

static SQLiteDatabase openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler)

等同于openDatabase(路径,工厂,CREATE_IF_NECESSARY,errorHandler)。

static SQLiteDatabase openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory)

相当于openDatabase(路径,工厂,CREATE_IF_NECESSARY)。

Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

查询给定的URL,在结果集上返回 Cursor

Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

查询给定的表格,在结果集上返回 Cursor

Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, CancellationSignal cancellationSignal)

查询给定的URL,在结果集上返回一个 Cursor

Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

查询给定的表格,在结果集上返回 Cursor

Cursor queryWithFactory(SQLiteDatabase.CursorFactory cursorFactory, boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, CancellationSignal cancellationSignal)

查询给定的URL,在结果集上返回 Cursor

Cursor queryWithFactory(SQLiteDatabase.CursorFactory cursorFactory, boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

查询给定的URL,在结果集上返回 Cursor

Cursor rawQuery(String sql, String[] selectionArgs, CancellationSignal cancellationSignal)

运行提供的SQL并在结果集上返回 Cursor

Cursor rawQuery(String sql, String[] selectionArgs)

运行提供的SQL并在结果集上返回 Cursor

Cursor rawQueryWithFactory(SQLiteDatabase.CursorFactory cursorFactory, String sql, String[] selectionArgs, String editTable, CancellationSignal cancellationSignal)

运行提供的SQL并在结果集上返回一个游标。

Cursor rawQueryWithFactory(SQLiteDatabase.CursorFactory cursorFactory, String sql, String[] selectionArgs, String editTable)

运行提供的SQL并在结果集上返回一个游标。

static int releaseMemory()

尝试释放SQLite存储的内存,但不需要正常运行。

long replace(String table, String nullColumnHack, ContentValues initialValues)

用于替换数据库中的行的便捷方法。

long replaceOrThrow(String table, String nullColumnHack, ContentValues initialValues)

用于替换数据库中的行的便捷方法。

void setForeignKeyConstraintsEnabled(boolean enable)

设置是否为数据库启用外键约束。

void setLocale(Locale locale)

设置此数据库的区域设置。

void setLockingEnabled(boolean lockingEnabled)

此方法在API级别16中已弃用。此方法现在不执行任何操作。 不使用。

void setMaxSqlCacheSize(int cacheSize)

为此数据库设置预备语句高速缓存的最大大小。

long setMaximumSize(long numBytes)

设置数据库将增长到的最大大小。

void setPageSize(long numBytes)

设置数据库页面大小。

void setTransactionSuccessful()

将当前交易标记为成功。

void setVersion(int version)

设置数据库版本。

String toString()

返回对象的字符串表示形式。

int update(String table, ContentValues values, String whereClause, String[] whereArgs)

更新数据库中的行的便捷方法。

int updateWithOnConflict(String table, ContentValues values, String whereClause, String[] whereArgs, int conflictAlgorithm)

更新数据库中的行的便捷方法。

void validateSql(String sql, CancellationSignal cancellationSignal)

通过编译来验证SQL SELECT语句是否有效。

boolean yieldIfContended()

此方法在API级别3中已被弃用。如果db被锁定多次(因为嵌套事务),则不会生成锁定。 改用yieldIfContendedSafely。

boolean yieldIfContendedSafely()

暂时结束事务让其他线程运行。

boolean yieldIfContendedSafely(long sleepAfterYieldDelay)

暂时结束事务让其他线程运行。

Protected methods

void finalize()

当垃圾收集确定没有更多对该对象的引用时,由对象上的垃圾回收器调用。

void onAllReferencesReleased()

当通过调用 releaseReference()close()释放对象的最后一个引用时调用。

Inherited methods

From class android.database.sqlite.SQLiteClosable
From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Constants

CONFLICT_ABORT

Added in API level 8
int CONFLICT_ABORT

发生约束冲突时,不会执行ROLLBACK,以便保留同一事务内先前命令的更改。 这是默认行为。

常量值:2(0x00000002)

CONFLICT_FAIL

Added in API level 8
int CONFLICT_FAIL

发生约束冲突时,该命令将以返回代码SQLITE_CONSTRAINT中止。 但是,在遇到约束冲突之前,命令对数据库所做的任何更改都会保留,并且不会退出。

常量值:3(0x00000003)

CONFLICT_IGNORE

Added in API level 8
int CONFLICT_IGNORE

发生约束冲突时,不会插入或更改包含约束冲突的一行。 但是该命令继续正常执行。 包含约束冲突的行之前和之后的其他行继续正常插入或更新。 没有错误返回。

常量值:4(0x00000004)

CONFLICT_NONE

Added in API level 8
int CONFLICT_NONE

未指定冲突操作时使用以下内容。

常量值:0(0x00000000)

CONFLICT_REPLACE

Added in API level 8
int CONFLICT_REPLACE

当发生UNIQUE约束冲突时,会在插入或更新当前行之前删除导致违反约束的预先存在的行。 因此插入或更新总是会发生。 该命令继续正常执行。 没有错误返回。 如果发生NOT NULL约束冲突,则NULL值将替换为该列的默认值。 如果该列没有默认值,则使用ABORT算法。 如果发生CHECK约束违规,则使用IGNORE算法。 当此冲突解决策略删除行以满足约束时,它不会调用这些行上的删除触发器。 此行为在未来版本中可能会更改。

常量值:5(0x00000005)

CONFLICT_ROLLBACK

Added in API level 8
int CONFLICT_ROLLBACK

发生约束冲突时,会立即发生ROLLBACK,从而结束当前事务,并且该命令将以SQLITE_CONSTRAINT的返回码中止。 如果没有事务处于活动状态(除了在每个命令上创建的隐含事务),则此算法与ABORT的工作方式相同。

常数值:1(0x00000001)

CREATE_IF_NECESSARY

Added in API level 1
int CREATE_IF_NECESSARY

打开标志:如果数据库文件不存在,则标记为 openDatabase(String, SQLiteDatabase.CursorFactory, int)以创建数据库文件。

常量值:268435456(0x10000000)

ENABLE_WRITE_AHEAD_LOGGING

Added in API level 16
int ENABLE_WRITE_AHEAD_LOGGING

打开标志:标记为openDatabase(String, SQLiteDatabase.CursorFactory, int)以打开默认情况下启用预写日志记录的数据库文件。 使用此标志比调用enableWriteAheadLogging()更有效。 预写式日志记录不能用于只读数据库,因此如果数据库以只读方式打开,则忽略该标志的值。

也可以看看:

常量值:536870912(0x20000000)

MAX_SQL_CACHE_SIZE

Added in API level 11
int MAX_SQL_CACHE_SIZE

绝对最大值,可由setMaxSqlCacheSize(int)设置。 每个准备好的语句都在1K到6K之间,具体取决于SQL语句和模式的复杂程度。 大型SQL缓存可能会占用大量内存。

常量值:100(0x00000064)

NO_LOCALIZED_COLLATORS

Added in API level 1
int NO_LOCALIZED_COLLATORS

打开标志:标记为 openDatabase(String, SQLiteDatabase.CursorFactory, int)可以在不支持本地化的整理器的情况下打开数据库。

这会导致不能创建整理器LOCALIZED 使用此标志来使用创建数据库的设置时,您必须保持一致。 如果这设置, setLocale(Locale)将不会执行任何操作。

常量值:16(0x00000010)

OPEN_READONLY

Added in API level 1
int OPEN_READONLY

打开标志:标记为openDatabase(String, SQLiteDatabase.CursorFactory, int)以打开仅用于读取的数据库。 如果磁盘可能已满,这是打开数据库的唯一可靠方法。

常数值:1(0x00000001)

OPEN_READWRITE

Added in API level 1
int OPEN_READWRITE

打开标志:标志为openDatabase(String, SQLiteDatabase.CursorFactory, int)打开数据库进行读写。 如果磁盘已满,甚至在您实际写入任何内容之前,这可能会失败。

请注意,此标志的值为0,所以它是默认值。

常量值:0(0x00000000)

SQLITE_MAX_LIKE_PATTERN_LENGTH

Added in API level 1
int SQLITE_MAX_LIKE_PATTERN_LENGTH

LIKE或GLOB模式的最大长度在SQLite的默认LIKE和GLOB实现中使用的模式匹配算法对于某些病例可以表现出O(N ^ 2)性能(其中N是模式中的字符数)。 为避免拒绝服务攻击,LIKE或GLOB模式的长度限于SQLITE_MAX_LIKE_PATTERN_LENGTH个字节。 此限制的默认值为50000.现代工作站可以相对快速地评估50000字节的病态LIKE或GLOB模式。 当模式长度达到数百万字节时,拒绝服务问题才会发挥作用。 尽管如此,由于大多数有用的LIKE或GLOB模式长度最多为几十个字节,如果偏执程序开发人员知道外部用户能够生成任意模式,则可能希望将此参数减少到几百个范围内。

常量值:50000(0x0000c350)

Public methods

beginTransaction

Added in API level 1
void beginTransaction ()

在EXCLUSIVE模式下开始交易。

事务可以嵌套。 当外部事务结束时,在该事务中完成的所有工作和所有嵌套事务将被提交或回滚。 如果任何事务没有被标记为干净(通过调用setTransactionSuccessful),则更改将被回滚。 否则他们会承诺。

这是交易的标准成语:

   db.beginTransaction();
   try {
     ...
     db.setTransactionSuccessful();
   } finally {
     db.endTransaction();
   }
 

beginTransactionNonExclusive

Added in API level 11
void beginTransactionNonExclusive ()

在IMMEDIATE模式下开始交易。 事务可以嵌套。 当外部事务结束时,在该事务中完成的所有工作和所有嵌套事务将被提交或回滚。 如果任何事务没有被标记为干净(通过调用setTransactionSuccessful),则更改将被回滚。 否则他们会承诺。

这是交易的标准成语:

   db.beginTransactionNonExclusive();
   try {
     ...
     db.setTransactionSuccessful();
   } finally {
     db.endTransaction();
   }
 

beginTransactionWithListener

Added in API level 5
void beginTransactionWithListener (SQLiteTransactionListener transactionListener)

在EXCLUSIVE模式下开始交易。

事务可以嵌套。 当外部事务结束时,在该事务中完成的所有工作和所有嵌套事务将被提交或回滚。 如果任何事务没有被标记为干净(通过调用setTransactionSuccessful),则更改将被回滚。 否则他们会承诺。

这是交易的标准成语:

   db.beginTransactionWithListener(listener);
   try {
     ...
     db.setTransactionSuccessful();
   } finally {
     db.endTransaction();
   }
 

Parameters
transactionListener SQLiteTransactionListener: listener that should be notified when the transaction begins, commits, or is rolled back, either explicitly or by a call to yieldIfContendedSafely().

beginTransactionWithListenerNonExclusive

Added in API level 11
void beginTransactionWithListenerNonExclusive (SQLiteTransactionListener transactionListener)

在IMMEDIATE模式下开始交易。 事务可以嵌套。 当外部事务结束时,在该事务中完成的所有工作和所有嵌套事务将被提交或回滚。 如果任何事务没有被标记为干净(通过调用setTransactionSuccessful),则更改将被回滚。 否则他们会承诺。

这是交易的标准成语:

   db.beginTransactionWithListenerNonExclusive(listener);
   try {
     ...
     db.setTransactionSuccessful();
   } finally {
     db.endTransaction();
   }
 

Parameters
transactionListener SQLiteTransactionListener: listener that should be notified when the transaction begins, commits, or is rolled back, either explicitly or by a call to yieldIfContendedSafely().

compileStatement

Added in API level 1
SQLiteStatement compileStatement (String sql)

将SQL语句编译为可重用的预编译语句对象。 这些参数与execSQL(String)相同。 您可以在报表中填入?s,并在您每次运行报表时填写bindString(int, String)bindLong(int, long) 语句可能不会返回大于1x1的结果集。

没有两个线程应该同时使用相同的 SQLiteStatement

Parameters
sql String: The raw SQL statement, may contain ? for unknown values to be bound later.
Returns
SQLiteStatement A pre-compiled SQLiteStatement object. Note that SQLiteStatements are not synchronized, see the documentation for more details.
Throws
SQLException

create

Added in API level 1
SQLiteDatabase create (SQLiteDatabase.CursorFactory factory)

创建一个支持内存的SQLite数据库。 数据库关闭时其内容将被销毁。

将数据库的语言环境设置为系统的当前语言环境。 如果您想要别的东西,请致电setLocale(Locale)

Parameters
factory SQLiteDatabase.CursorFactory: an optional factory class that is called to instantiate a cursor when query is called
Returns
SQLiteDatabase a SQLiteDatabase object, or null if the database can't be created

delete

Added in API level 1
int delete (String table, 
                String whereClause, 
                String[] whereArgs)

删除数据库中的行的便捷方法。

Parameters
table String: the table to delete from
whereClause String: the optional WHERE clause to apply when deleting. Passing null will delete all rows.
whereArgs String: You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.
Returns
int the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass "1" as the whereClause.

deleteDatabase

Added in API level 16
boolean deleteDatabase (File file)

删除包含其日志文件和可能由数据库引擎创建的其他辅助文件的数据库。

Parameters
file File: The database file path.
Returns
boolean True if the database was successfully deleted.

disableWriteAheadLogging

Added in API level 16
void disableWriteAheadLogging ()

此方法禁用由 enableWriteAheadLogging()启用的 enableWriteAheadLogging()

Throws
IllegalStateException if there are transactions in progress at the time this method is called. WAL mode can only be changed when there are no transactions in progress.

也可以看看:

enableWriteAheadLogging

Added in API level 11
boolean enableWriteAheadLogging ()

此方法可以在同一数据库上并行执行来自多个线程的查询。 它通过打开数据库的多个连接并为每个查询使用不同的数据库连接来完成此操作。 数据库日志模式也被更改为使写入可以与读取同时进行。

如果未预先写入日志记录(默认设置),则不可能同时在数据库上执行读取和写入操作。 在修改数据库之前,编写者会隐式地获取数据库上的排他锁,从而防止读者在写入完成之前访问数据库。

相反,如果启用预写式日志记录(通过调用此方法),则写入操作将发生在单独的日志文件中,以便同时进行读取操作。 写入过程中,其他线程上的读者会在写入之前感知数据库的状态。 写入完成后,其他线程上的读者将会感知数据库的新状态。

无论何时一个数据库将同时被多个线程同时访问和修改,启用预写式日志记录是一个不错的主意。 但是,预写式日志记录使用的内存比普通日志记录多得多,因为有多个连接到同一个数据库。 因此,如果一个数据库只能被单个线程使用,或者如果优化并发性并不重要,那么应该禁用预写式日志记录。

调用此方法后,只要数据库保持打开状态,就会启用并行查询。 要禁止并行执行查询,请调用disableWriteAheadLogging()或关闭数据库并重新打开它。

用于并行执行查询的最大连接数取决于设备内存以及可能的其他属性。

如果一个查询是一个事务的一部分,那么它就在该事务开始的同一个数据库句柄上执行。

作者应该使用beginTransactionNonExclusive()beginTransactionWithListenerNonExclusive(SQLiteTransactionListener)开始交易。 非独占模式允许数据库文件被执行查询的其他线程读取。

如果数据库有任何附加的数据库,那么并行执行查询是不可能的。 同样,只读数据库或内存数据库不支持预写日志记录。 在这种情况下, enableWriteAheadLogging()返回false。

启用预写日志的最佳方法是将ENABLE_WRITE_AHEAD_LOGGING标志传递给openDatabase(String, SQLiteDatabase.CursorFactory, int) 这比调用enableWriteAheadLogging()更有效。


     SQLiteDatabase db = SQLiteDatabase.openDatabase("db_filename", cursorFactory,
             SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING,
             myDatabaseErrorHandler);
     db.enableWriteAheadLogging();
 

另一种启用预写日志记录的方法是在打开数据库后调用enableWriteAheadLogging()


     SQLiteDatabase db = SQLiteDatabase.openDatabase("db_filename", cursorFactory,
             SQLiteDatabase.CREATE_IF_NECESSARY, myDatabaseErrorHandler);
     db.enableWriteAheadLogging();
 

有关如何预写日志记录工作的更多详细信息,另请参阅 SQLite Write-Ahead Logging

Returns
boolean True if write-ahead logging is enabled.
Throws
IllegalStateException if there are transactions in progress at the time this method is called. WAL mode can only be changed when there are no transactions in progress.

也可以看看:

endTransaction

Added in API level 1
void endTransaction ()

结束交易。 请参阅beginTransaction以获取有关如何使用此内容以及何时提交和回滚事务的说明。

execSQL

Added in API level 1
void execSQL (String sql)

执行不是SELECT或任何其他返回数据的SQL语句的单个SQL语句。

它无法返回任何数据(例如受影响的行数)。 相反,我们鼓励你使用insert(String, String, ContentValues)update(String, ContentValues, String, String[]) ,等人,在可能的情况。

当使用enableWriteAheadLogging()enableWriteAheadLogging()由该类自动管理。 因此,请勿使用“PRAGMA journal_mode”设置journal_mode “声明,如果你的应用程序正在使用enableWriteAheadLogging()

Parameters
sql String: the SQL statement to be executed. Multiple statements separated by semicolons are not supported.
Throws
SQLException if the SQL string is invalid

execSQL

Added in API level 1
void execSQL (String sql, 
                Object[] bindArgs)

执行不是SELECT / INSERT / UPDATE / DELETE的单个SQL语句。

对于INSERT语句,请改用下列任何一种。

对于UPDATE语句,请改用以下任何一种语句。

对于DELETE语句,请改用以下任何一种。

例如,以下是使用此方法的最佳人选:

  • ALTER TABLE
  • CREATE or DROP table / trigger / view / index / virtual table
  • REINDEX
  • RELEASE
  • SAVEPOINT
  • PRAGMA that returns no data

当使用enableWriteAheadLogging()enableWriteAheadLogging()由该类自动管理。 因此,请勿使用“PRAGMA journal_mode”设置journal_mode “声明,如果你的应用程序正在使用enableWriteAheadLogging()

Parameters
sql String: the SQL statement to be executed. Multiple statements separated by semicolons are not supported.
bindArgs Object: only byte[], String, Long and Double are supported in bindArgs.
Throws
SQLException if the SQL string is invalid

findEditTable

Added in API level 1
String findEditTable (String tables)

查找可编辑的第一个表的名称。

Parameters
tables String: a list of tables
Returns
String the first table listed

getAttachedDbs

Added in API level 11
List<Pair<StringString>> getAttachedDbs ()

通过在数据库上执行'pragma database_list'返回包括主数据库在内的所有连接数据库的完整路径名的列表。

Returns
List<Pair<StringString>> ArrayList of pairs of (database name, database file path) or null if the database is not open.

getMaximumSize

Added in API level 1
long getMaximumSize ()

返回数据库可能增长到的最大大小。

Returns
long the new maximum database size

getPageSize

Added in API level 1
long getPageSize ()

返回当前数据库页面大小(以字节为单位)。

Returns
long the database page size, in bytes

getPath

Added in API level 1
String getPath ()

获取数据库文件的路径。

Returns
String The path to the database file.

getSyncedTables

Added in API level 1
Map<StringString> getSyncedTables ()

此方法在API级别11中已弃用。
此方法不再用于任何有用的目的,并已被弃用。

已过时。

Returns
Map<StringString>

getVersion

Added in API level 1
int getVersion ()

获取数据库版本。

Returns
int the database version

inTransaction

Added in API level 1
boolean inTransaction ()

如果当前线程有一个待处理的事务,则返回true。

Returns
boolean True if the current thread is in a transaction.

insert

Added in API level 1
long insert (String table, 
                String nullColumnHack, 
                ContentValues values)

将行插入数据库的便捷方法。

Parameters
table String: the table to insert the row into
nullColumnHack String: optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.
values ContentValues: this map contains the initial column values for the row. The keys should be the column names and the values the column values
Returns
long the row ID of the newly inserted row, or -1 if an error occurred

insertOrThrow

Added in API level 1
long insertOrThrow (String table, 
                String nullColumnHack, 
                ContentValues values)

将行插入数据库的便捷方法。

Parameters
table String: the table to insert the row into
nullColumnHack String: optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.
values ContentValues: this map contains the initial column values for the row. The keys should be the column names and the values the column values
Returns
long the row ID of the newly inserted row, or -1 if an error occurred
Throws
SQLException
SQLException

insertWithOnConflict

Added in API level 8
long insertWithOnConflict (String table, 
                String nullColumnHack, 
                ContentValues initialValues, 
                int conflictAlgorithm)

将行插入数据库的一般方法。

Parameters
table String: the table to insert the row into
nullColumnHack String: optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided initialValues is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your initialValues is empty.
initialValues ContentValues: this map contains the initial column values for the row. The keys should be the column names and the values the column values
conflictAlgorithm int: for insert conflict resolver
Returns
long the row ID of the newly inserted row OR -1 if either the input parameter conflictAlgorithm = CONFLICT_IGNORE or an error occurred.

isDatabaseIntegrityOk

Added in API level 11
boolean isDatabaseIntegrityOk ()

在给定数据库(以及所有附加数据库)上运行'pragma integrity_check',如果给定数据库(及其所有附加数据库)通过integrity_check,则返回true,否则返回false。

如果结果为false,则此方法记录integrity_check命令执行报告的错误。

请注意,数据库中的'pragma integrity_check'可能需要很长时间。

Returns
boolean true if the given database (and all its attached databases) pass integrity_check, false otherwise.

isDbLockedByCurrentThread

Added in API level 1
boolean isDbLockedByCurrentThread ()

如果当前线程持有到数据库的活动连接,则返回true。

此方法的名称来自于与数据库有活动连接的时间,意味着该线程在数据库上持有实际锁定。 如今,不再有真正的“数据库锁”,尽管如果线程无法获取数据库连接来执行特定操作,它们可能会阻塞。

Returns
boolean True if the current thread is holding an active connection to the database.

isDbLockedByOtherThreads

Added in API level 1
boolean isDbLockedByOtherThreads ()

此方法在API级别16中已被弃用。
始终返回false。 不要使用这种方法。

始终返回false。

不再有数据库锁的概念,所以这个方法总是返回false。

Returns
boolean False.

isOpen

Added in API level 1
boolean isOpen ()

如果数据库当前打开,则返回true。

Returns
boolean True if the database is currently open (has not been closed).

isReadOnly

Added in API level 1
boolean isReadOnly ()

如果数据库以只读方式打开,则返回true。

Returns
boolean True if database is opened as read only.

isWriteAheadLoggingEnabled

Added in API level 16
boolean isWriteAheadLoggingEnabled ()

如果为此数据库启用预写日志记录,则返回true。

Returns
boolean True if write-ahead logging has been enabled for this database.

也可以看看:

markTableSyncable

Added in API level 1
void markTableSyncable (String table, 
                String deletedTable)

此方法在API级别11中已弃用。
此方法不再用于任何有用的目的,并已被弃用。

将此表标记为可同步。 当此表中发生更新时,将设置_sync_dirty字段以确保正确的同步操作。

Parameters
table String: the table to mark as syncable
deletedTable String: The deleted table that corresponds to the syncable table

markTableSyncable

Added in API level 1
void markTableSyncable (String table, 
                String foreignKey, 
                String updateTable)

此方法在API级别11中已弃用。
此方法不再用于任何有用的目的,并已被弃用。

将此表标记为可同步,其中_sync_dirty位于另一个表中。 当此表中发生更新时,updateTable中的行的_sync_dirty字段和foreignKey中的_id将被设置为确保正确的同步操作。

Parameters
table String: an update on this table will trigger a sync time removal
foreignKey String: this is the column in table whose value is an _id in updateTable
updateTable String: this is the table that will have its _sync_dirty

needUpgrade

Added in API level 1
boolean needUpgrade (int newVersion)

如果新版本代码大于当前数据库版本,则返回true。

Parameters
newVersion int: The new version code.
Returns
boolean True if the new version code is greater than the current database version.

openDatabase

Added in API level 1
SQLiteDatabase openDatabase (String path, 
                SQLiteDatabase.CursorFactory factory, 
                int flags)

根据标志 OPEN_READWRITE OPEN_READONLY CREATE_IF_NECESSARY和/或 NO_LOCALIZED_COLLATORS打开数据库。

将数据库的语言环境设置为系统的当前语言环境。 如果您想要别的东西,请致电setLocale(Locale)

Parameters
path String: to database file to open and/or create
factory SQLiteDatabase.CursorFactory: an optional factory class that is called to instantiate a cursor when query is called, or null for default
flags int: to control database access mode
Returns
SQLiteDatabase the newly opened database
Throws
SQLiteException if the database cannot be opened

openDatabase

Added in API level 11
SQLiteDatabase openDatabase (String path, 
                SQLiteDatabase.CursorFactory factory, 
                int flags, 
                DatabaseErrorHandler errorHandler)

根据标志 OPEN_READWRITE OPEN_READONLY CREATE_IF_NECESSARY和/或 NO_LOCALIZED_COLLATORS打开数据库。

将数据库的语言环境设置为系统的当前语言环境。 如果您想要别的东西,请致电setLocale(Locale)

接受输入参数:当sqlite报告数据库损坏时,用于处理腐败的具体实例 DatabaseErrorHandler

Parameters
path String: to database file to open and/or create
factory SQLiteDatabase.CursorFactory: an optional factory class that is called to instantiate a cursor when query is called, or null for default
flags int: to control database access mode
errorHandler DatabaseErrorHandler: the DatabaseErrorHandler obj to be used to handle corruption when sqlite reports database corruption
Returns
SQLiteDatabase the newly opened database
Throws
SQLiteException if the database cannot be opened

openOrCreateDatabase

Added in API level 1
SQLiteDatabase openOrCreateDatabase (File file, 
                SQLiteDatabase.CursorFactory factory)

相当于openDatabase(file.getPath(),工厂,CREATE_IF_NECESSARY)。

Parameters
file File
factory SQLiteDatabase.CursorFactory
Returns
SQLiteDatabase

openOrCreateDatabase

Added in API level 11
SQLiteDatabase openOrCreateDatabase (String path, 
                SQLiteDatabase.CursorFactory factory, 
                DatabaseErrorHandler errorHandler)

等同于openDatabase(路径,工厂,CREATE_IF_NECESSARY,errorHandler)。

Parameters
path String
factory SQLiteDatabase.CursorFactory
errorHandler DatabaseErrorHandler
Returns
SQLiteDatabase

openOrCreateDatabase

Added in API level 1
SQLiteDatabase openOrCreateDatabase (String path, 
                SQLiteDatabase.CursorFactory factory)

相当于openDatabase(路径,工厂,CREATE_IF_NECESSARY)。

Parameters
path String
factory SQLiteDatabase.CursorFactory
Returns
SQLiteDatabase

query

Added in API level 1
Cursor query (boolean distinct, 
                String table, 
                String[] columns, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having, 
                String orderBy, 
                String limit)

查询给定的URL,并在结果集上返回 Cursor

Parameters
distinct boolean: true if you want each row to be unique, false otherwise.
table String: The table name to compile the query against.
columns String: 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 String: 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 String: 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 String: 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 String: 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 String: 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 String: Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
Returns
Cursor A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.

也可以看看:

query

Added in API level 1
Cursor query (String table, 
                String[] columns, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having, 
                String orderBy, 
                String limit)

查询给定的表,将 Cursor返回到结果集上。

Parameters
table String: The table name to compile the query against.
columns String: 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 String: 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 String: 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 String: 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 String: 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 String: 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 String: Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
Returns
Cursor A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.

也可以看看:

query

Added in API level 16
Cursor query (boolean distinct, 
                String table, 
                String[] columns, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having, 
                String orderBy, 
                String limit, 
                CancellationSignal cancellationSignal)

查询给定的URL,在结果集上返回 Cursor

Parameters
distinct boolean: true if you want each row to be unique, false otherwise.
table String: The table name to compile the query against.
columns String: 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 String: 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 String: 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 String: 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 String: 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 String: 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 String: Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
cancellationSignal CancellationSignal: A signal to cancel the operation in progress, or null if none. If the operation is canceled, then OperationCanceledException will be thrown when the query is executed.
Returns
Cursor A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.

也可以看看:

query

Added in API level 1
Cursor query (String table, 
                String[] columns, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having, 
                String orderBy)

查询给定的表,在结果集上返回 Cursor

Parameters
table String: The table name to compile the query against.
columns String: 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 String: 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 String: 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 String: 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 String: 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 String: 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.
Returns
Cursor A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.

也可以看看:

queryWithFactory

Added in API level 16
Cursor queryWithFactory (SQLiteDatabase.CursorFactory cursorFactory, 
                boolean distinct, 
                String table, 
                String[] columns, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having, 
                String orderBy, 
                String limit, 
                CancellationSignal cancellationSignal)

查询给定的URL,在结果集上返回一个 Cursor

Parameters
cursorFactory SQLiteDatabase.CursorFactory: the cursor factory to use, or null for the default factory
distinct boolean: true if you want each row to be unique, false otherwise.
table String: The table name to compile the query against.
columns String: 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 String: 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 String: 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 String: 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 String: 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 String: 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 String: Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
cancellationSignal CancellationSignal: A signal to cancel the operation in progress, or null if none. If the operation is canceled, then OperationCanceledException will be thrown when the query is executed.
Returns
Cursor A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.

也可以看看:

queryWithFactory

Added in API level 1
Cursor queryWithFactory (SQLiteDatabase.CursorFactory cursorFactory, 
                boolean distinct, 
                String table, 
                String[] columns, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having, 
                String orderBy, 
                String limit)

查询给定的URL,返回结果集 Cursor

Parameters
cursorFactory SQLiteDatabase.CursorFactory: the cursor factory to use, or null for the default factory
distinct boolean: true if you want each row to be unique, false otherwise.
table String: The table name to compile the query against.
columns String: 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 String: 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 String: 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 String: 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 String: 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 String: 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 String: Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
Returns
Cursor A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.

也可以看看:

rawQuery

Added in API level 16
Cursor rawQuery (String sql, 
                String[] selectionArgs, 
                CancellationSignal cancellationSignal)

运行提供的SQL并在结果集上返回 Cursor

Parameters
sql String: the SQL query. The SQL string must not be ; terminated
selectionArgs String: You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.
cancellationSignal CancellationSignal: A signal to cancel the operation in progress, or null if none. If the operation is canceled, then OperationCanceledException will be thrown when the query is executed.
Returns
Cursor A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.

rawQuery

Added in API level 1
Cursor rawQuery (String sql, 
                String[] selectionArgs)

运行提供的SQL并在结果集上返回 Cursor

Parameters
sql String: the SQL query. The SQL string must not be ; terminated
selectionArgs String: You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.
Returns
Cursor A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.

rawQueryWithFactory

Added in API level 16
Cursor rawQueryWithFactory (SQLiteDatabase.CursorFactory cursorFactory, 
                String sql, 
                String[] selectionArgs, 
                String editTable, 
                CancellationSignal cancellationSignal)

运行提供的SQL并在结果集上返回一个游标。

Parameters
cursorFactory SQLiteDatabase.CursorFactory: the cursor factory to use, or null for the default factory
sql String: the SQL query. The SQL string must not be ; terminated
selectionArgs String: You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.
editTable String: the name of the first table, which is editable
cancellationSignal CancellationSignal: A signal to cancel the operation in progress, or null if none. If the operation is canceled, then OperationCanceledException will be thrown when the query is executed.
Returns
Cursor A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.

rawQueryWithFactory

Added in API level 1
Cursor rawQueryWithFactory (SQLiteDatabase.CursorFactory cursorFactory, 
                String sql, 
                String[] selectionArgs, 
                String editTable)

运行提供的SQL并在结果集上返回一个游标。

Parameters
cursorFactory SQLiteDatabase.CursorFactory: the cursor factory to use, or null for the default factory
sql String: the SQL query. The SQL string must not be ; terminated
selectionArgs String: You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.
editTable String: the name of the first table, which is editable
Returns
Cursor A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.

releaseMemory

Added in API level 1
int releaseMemory ()

尝试释放SQLite存储的内存,但不需要正常运行。 通常这个内存将来自页面缓存。

Returns
int the number of bytes actually released

replace

Added in API level 1
long replace (String table, 
                String nullColumnHack, 
                ContentValues initialValues)

用于替换数据库中的行的便捷方法。

Parameters
table String: the table in which to replace the row
nullColumnHack String: optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided initialValues is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your initialValues is empty.
initialValues ContentValues: this map contains the initial column values for the row.
Returns
long the row ID of the newly inserted row, or -1 if an error occurred

replaceOrThrow

Added in API level 1
long replaceOrThrow (String table, 
                String nullColumnHack, 
                ContentValues initialValues)

用于替换数据库中的行的便捷方法。

Parameters
table String: the table in which to replace the row
nullColumnHack String: optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided initialValues is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your initialValues is empty.
initialValues ContentValues: this map contains the initial column values for the row. The key
Returns
long the row ID of the newly inserted row, or -1 if an error occurred
Throws
SQLException
SQLException

setForeignKeyConstraintsEnabled

Added in API level 16
void setForeignKeyConstraintsEnabled (boolean enable)

设置是否为数据库启用外键约束。

默认情况下,数据库不强制外键约束。 此方法允许应用程序启用外键约束。 每次打开数据库时都必须调用它,以确保为会话启用外键约束。

在调用 openOrCreateDatabase(File, SQLiteDatabase.CursorFactory)onConfigure(SQLiteDatabase)回调 openOrCreateDatabase(File, SQLiteDatabase.CursorFactory)后,调用此方法的好时机就是正确的。

当禁用外键约束时,数据库不检查对数据库的更改是否会违反外键约束。 同样,当禁用外键约束时,数据库将不会执行级联删除或更新触发器。 因此,数据库状态可能变得不一致。 要执行数据库完整性检查,请致电isDatabaseIntegrityOk()

在事务处理过程中不能调用此方法。

有关外键约束支持的更多详细信息,另请参阅 SQLite Foreign Key Constraints

Parameters
enable boolean: True to enable foreign key constraints, false to disable them.
Throws
IllegalStateException if the are transactions is in progress when this method is called.

setLocale

Added in API level 1
void setLocale (Locale locale)

设置此数据库的区域设置。 如果此数据库的NO_LOCALIZED_COLLATORS标志已设置或已打开, NO_LOCALIZED_COLLATORS任何操作。

Parameters
locale Locale: The new locale.
Throws
SQLException if the locale could not be set. The most common reason for this is that there is no collator available for the locale you requested. In this case the database remains unchanged.

setLockingEnabled

Added in API level 1
void setLockingEnabled (boolean lockingEnabled)

此方法在API级别16中已被弃用。
这种方法现在什么都不做。 不使用。

通过在关键部分使用锁定来控制SQLiteDatabase是否成为线程安全的。 这非常昂贵,所以如果你知道你的数据库只能被单个线程使用,那么你应该将它设置为false。 默认值是true。

Parameters
lockingEnabled boolean: set to true to enable locks, false otherwise

setMaxSqlCacheSize

Added in API level 11
void setMaxSqlCacheSize (int cacheSize)

为此数据库设置预备语句高速缓存的最大大小。 (缓存的大小=存储在缓存中的编译的sql语句的数量)。

最大高速缓存大小只能从当前大小增加(默认值= 10)。 如果使用小于当前最大值的方式调用此方法,则会抛出IllegalStateException。

这种方法是线程安全的。

Parameters
cacheSize int: the size of the cache. can be (0 to MAX_SQL_CACHE_SIZE)
Throws
IllegalStateException if input cacheSize > MAX_SQL_CACHE_SIZE.

setMaximumSize

Added in API level 1
long setMaximumSize (long numBytes)

设置数据库将增长到的最大大小。 最大尺寸不能设置为当前尺寸以下。

Parameters
numBytes long: the maximum database size, in bytes
Returns
long the new maximum database size

setPageSize

Added in API level 1
void setPageSize (long numBytes)

设置数据库页面大小。 页面大小必须是2的幂。 如果有任何数据已写入数据库文件,则此方法不起作用,并且必须在创建数据库后立即调用该方法。

Parameters
numBytes long: the database page size, in bytes

setTransactionSuccessful

Added in API level 1
void setTransactionSuccessful ()

将当前交易标记为成功。 在调用this和调用endTransaction之间不要做更多的数据库工作。 在这种情况下尽可能少做非数据库工作。 如果在这个和endTransaction之间遇到任何错误,事务仍然会被提交。

Throws
IllegalStateException if the current thread is not in a transaction or the transaction is already marked as successful.

setVersion

Added in API level 1
void setVersion (int version)

设置数据库版本。

Parameters
version int: the new database version

toString

Added in API level 1
String toString ()

返回对象的字符串表示形式。 一般来说, toString方法返回一个“文本表示”该对象的字符串。 结果应该是一个简洁但内容丰富的表述,对于一个人来说很容易阅读。 建议所有子类重写此方法。

ObjecttoString方法返回一个字符串,其中包含对象为实例的类的名称,符号字符“ @ ”和对象的哈希代码的无符号十六进制表示形式。 换句话说,这个方法返回一个字符串,其值等于:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns
String a string representation of the object.

update

Added in API level 1
int update (String table, 
                ContentValues values, 
                String whereClause, 
                String[] whereArgs)

更新数据库中的行的便捷方法。

Parameters
table String: the table to update in
values ContentValues: a map from column names to new column values. null is a valid value that will be translated to NULL.
whereClause String: the optional WHERE clause to apply when updating. Passing null will update all rows.
whereArgs String: You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.
Returns
int the number of rows affected

updateWithOnConflict

Added in API level 8
int updateWithOnConflict (String table, 
                ContentValues values, 
                String whereClause, 
                String[] whereArgs, 
                int conflictAlgorithm)

更新数据库中的行的便捷方法。

Parameters
table String: the table to update in
values ContentValues: a map from column names to new column values. null is a valid value that will be translated to NULL.
whereClause String: the optional WHERE clause to apply when updating. Passing null will update all rows.
whereArgs String: You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.
conflictAlgorithm int: for update conflict resolver
Returns
int the number of rows affected

validateSql

Added in API level 24
void validateSql (String sql, 
                CancellationSignal cancellationSignal)

通过编译来验证SQL SELECT语句是否有效。 如果SQL语句无效,则此方法将抛出SQLiteException

Parameters
sql String: SQL to be validated
cancellationSignal CancellationSignal: A signal to cancel the operation in progress, or null if none. If the operation is canceled, then OperationCanceledException will be thrown when the query is executed.
Throws
SQLiteException if sql is invalid

yieldIfContended

Added in API level 1
boolean yieldIfContended ()

此方法在API级别3中已被弃用。
如果数据库被锁定不止一次(因为嵌套事务),那么锁不会被放弃。 改用yieldIfContendedSafely。

暂时结束事务让其他线程运行。 该交易目前被认为是成功的。 在调用此函数之前,不要调用setTransactionSuccessful。 当这返回一个新的事务将被创建但未被标记为成功。

Returns
boolean true if the transaction was yielded

yieldIfContendedSafely

Added in API level 3
boolean yieldIfContendedSafely ()

暂时结束事务让其他线程运行。 该交易目前被认为是成功的。 在调用此函数之前,不要调用setTransactionSuccessful。 当这返回一个新的事务将被创建但未被标记为成功。 这假定没有嵌套事务(beginTransaction只被调用一次),并且如果不是这种情况将会抛出异常。

Returns
boolean true if the transaction was yielded

yieldIfContendedSafely

Added in API level 5
boolean yieldIfContendedSafely (long sleepAfterYieldDelay)

暂时结束事务让其他线程运行。 该交易目前被认为是成功的。 在调用此函数之前,不要调用setTransactionSuccessful。 当这返回一个新的事务将被创建但未被标记为成功。 这假定没有嵌套事务(beginTransaction只被调用一次),并且如果不是这种情况将会抛出异常。

Parameters
sleepAfterYieldDelay long: if > 0, sleep this long before starting a new transaction if the lock was actually yielded. This will allow other background threads to make some more progress than they would if we started the transaction immediately.
Returns
boolean true if the transaction was yielded

Protected methods

finalize

Added in API level 1
void finalize ()

当垃圾收集确定没有更多对该对象的引用时,由对象上的垃圾回收器调用。 子类会覆盖finalize方法以处置系统资源或执行其他清理。

的常规协定finalize是,它被调用,如果当在Java TM虚拟机已确定不再有由该目的可以通过还没有死亡,除了作为一个动作的结果的任何线程访问的任何手段取决于某些其他可以完成的对象或类别的最终定稿。 finalize方法可以采取任何行动,包括使该对象再次可用于其他线程; 然而, finalize的通常目的是在对象被不可撤销地丢弃之前执行清理操作。 例如,表示输入/输出连接的对象的finalize方法可能会执行显式I / O事务,以在永久丢弃该对象之前中断连接。

Objectfinalize方法Object执行特殊操作; 它只是正常返回。 Object子类可能会覆盖此定义。

Java编程语言不保证哪个线程将为任何给定对象调用finalize方法。 但是,保证调用finalize的线程在调用finalize时不会保留任何用户可见的同步锁。 如果finalize方法引发未捕获的异常,则忽略该异常,并终止该对象的终止。

在为某个对象调用 finalize方法之后,在Java虚拟机再次确定不再有任何方法可以通过尚未死亡的任何线程访问此对象之前,不会采取进一步的操作,包括可能的操作通过准备完成的其他对象或类别,此时该对象可能被丢弃。

对于任何给定的对象,Java虚拟机永远不会多次调用 finalize方法。

finalize方法抛出的任何异常 finalize导致终止此对象的终止,但会被忽略。

Throws
Throwable

onAllReferencesReleased

Added in API level 1
void onAllReferencesReleased ()

当通过调用 releaseReference()close()释放对象的最后一个引用时调用。

Hooray!