FMDB源码阅读(一)FMDatabase.h

 FMDatabase.h

 1 #if ! __has_feature(objc_arc)
 2     #define FMDBAutorelease(__v) ([__v autorelease]);
 3     #define FMDBReturnAutoreleased FMDBAutorelease
 4 
 5     #define FMDBRetain(__v) ([__v retain]);
 6     #define FMDBReturnRetained FMDBRetain
 7 
 8     #define FMDBRelease(__v) ([__v release]);
 9 
10     #define FMDBDispatchQueueRelease(__v) (dispatch_release(__v));
11 #else
12     // -fobjc-arc
13     #define FMDBAutorelease(__v)
14     #define FMDBReturnAutoreleased(__v) (__v)
15 
16     #define FMDBRetain(__v)
17     #define FMDBReturnRetained(__v) (__v)
18 
19     #define FMDBRelease(__v)
20 
21 // If OS_OBJECT_USE_OBJC=1, then the dispatch objects will be treated like ObjC objects
22 // and will participate in ARC.
23 // See the section on "Dispatch Queues and Automatic Reference Counting" in "Grand Central Dispatch (GCD) Reference" for details. 
24     #if OS_OBJECT_USE_OBJC
25         #define FMDBDispatchQueueRelease(__v)
26     #else
27         #define FMDBDispatchQueueRelease(__v) (dispatch_release(__v));
28     #endif
29 #endif

  这里的预编译主要是 FMDB 用来兼容 ARC 和 MRC 的。当在 MRC 时要做的 autorelease、retain、release、dispatch_release 操作,在 ARC 时开发者是都不需要去做的,系统自动帮我们完成了这些释放操作。这里的 dispatch_release 要针对一下,在 iOS 6.0 之前即使是在 ARC 下,GCD 也并没有参于 ARC ,所以需要手动使用 dispatch_release 去释放内存,且在 iOS 6.0 之前 OS_OBJECT_USE_OBJC 等于 0,在6.0 之后它等于1 ,且系统同时把 GCD 也像普通的 OC 对象一样纳入了自动引用计数范畴,就不需要使用 dispatch_release 去手动释放内存了。

1 #if !__has_feature(objc_instancetype)
2     #define instancetype id
3 #endif

  如果系统没有定义 instancetype,则定义使用 id 类型定义一个 instancetype 的宏。id 可以做参数可以做返回值,instancetype 只能做返回值。

1 typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary);

  定义一个只有一个字典做参数的名字是 FMDBExecuteStatementsCallbackBlock 的 block类型,这是一个当 FMDB 执行 SQL 语句的时候做回调的 block。

1  The three main classes in FMDB are:
2 
3  - `FMDatabase` - Represents a single SQLite database.  Used for executing SQL statements.
4  - `<FMResultSet>` - Represents the results of executing a query on an `FMDatabase`.
5  - `<FMDatabaseQueue>` - If you want to perform queries and updates on multiple threads, you'll want to use this class.

  在 FMDB 中有三个主要的类:

  • FMDatabase 代表一个 SQLite 数据库,用于执行 SQL 语句。
  • FMResultSet 代表数据库执行查询操作返回的结果类。
  • FMDatabaseQueue 如果要在多个线程上执行查询和更新,则需要使用这个类。

  另外:

  • FMDatabasePool 针对 FMDatabase 对象的一个 pool。
  • FMStatement 用于包装 "sqlite_stmt"(SQL 语句)的类。

  注意:

  不要实例化一个 "FMDatabase" 对象使用它在多个线程。应该使用 "FMDatabeseQueue"。

 属性:

 1 /** Whether should trace execution */
 2 
 3 @property (atomic, assign) BOOL traceExecution;
 4 
 5 /** Whether checked out or not */
 6 
 7 @property (atomic, assign) BOOL checkedOut;
 8 
 9 /** Crash on errors */
10 
11 @property (atomic, assign) BOOL crashOnErrors;
12 
13 /** Logs errors */
14 
15 @property (atomic, assign) BOOL logsErrors;
16 
17 /** Dictionary of cached statements */
18 
19 @property (atomic, retain, nullable) NSMutableDictionary *cachedStatements;

  Bool traceExecution 是否跟踪执行。

  Bool checkedOut 是否检查。

  Bool crashOnErrors 有Error 时崩溃。

  Bool logsErrors 崩溃日志。

  NSMutableDictonary *cachedStatements 缓存 SQL 语句的字典。

 FMDatabase 初始化方法:

 1 /** Create a `FMDatabase` object.
 2  
 3  An `FMDatabase` is created with a path to a SQLite database file.  This path can be one of these three:
 4 
 5  1. A file system path.  The file does not have to exist on disk.  If it does not exist, it is created for you.
 6  2. An empty string (`@""`).  An empty database is created at a temporary location.  This database is deleted with the `FMDatabase` connection is closed.
 7  3. `nil`.  An in-memory database is created.  This database will be destroyed with the `FMDatabase` connection is closed.
 8 
 9  For example, to create/open a database in your Mac OS X `tmp` folder:
10 
11     FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
12 
13  Or, in iOS, you might open a database in the app's `Documents` directory:
14 
15     NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
16     NSString *dbPath   = [docsPath stringByAppendingPathComponent:@"test.db"];
17     FMDatabase *db     = [FMDatabase databaseWithPath:dbPath];
18 
19  (For more information on temporary and in-memory databases, read the sqlite documentation on the subject: [http://www.sqlite.org/inmemorydb.html](http://www.sqlite.org/inmemorydb.html))
20 
21  @param inPath Path of database file
22 
23  @return `FMDatabase` object if successful; `nil` if failure.
24 
25  */
26 
27 + (instancetype)databaseWithPath:(NSString * _Nullable)inPath;

  创建一个 FMDatabase 对象。

  使用一个 SQLite 数据库文件路径创建 "FMDatabase"。这个路径可以是下面三种情况之一:

  • inPath 是一个文件系统路径。如果这个路径在磁盘上不存在,那么它会自动创建。
  • inpath 是一个空字符串(例如:@"")。在临时位置创建一个空的数据库。当 "FMDatabase" 连接关闭的时候这个数据库会被删除。
  • inpath 是 nil。在内存中创建一个数据库。当 "FMDatabase" 连接关闭的时候这个数据库会被销毁。

  如果成功返回一个 "FMDatabase" 对象,如果失败返回一个 nil。

  下面的初始化方法基本都一致:

1 + (instancetype)databaseWithURL:(NSURL * _Nullable)url;
2 
3 - (instancetype)initWithPath:(NSString * _Nullable)path;
4 
5 - (instancetype)initWithURL:(NSURL * _Nullable)url;

 打开和关闭数据库:

 1 /** Opening a new database connection
 2  
 3  The database is opened for reading and writing, and is created if it does not already exist.
 4 
 5  @return `YES` if successful, `NO` on error.
 6 
 7  @see [sqlite3_open()](http://sqlite.org/c3ref/open.html)
 8  @see openWithFlags:
 9  @see close
10  */
11 
12 - (BOOL)open;

  打开一个新的数据库连接。

  数据库被打开用来读和写,如果数据库不存在则创建它。

  如果成功返回 "YES",错误返回 "NO"。

 1 /** Opening a new database connection with flags and an optional virtual file system (VFS)
 2 
 3  @param flags one of the following three values, optionally combined with the `SQLITE_OPEN_NOMUTEX`, `SQLITE_OPEN_FULLMUTEX`, `SQLITE_OPEN_SHAREDCACHE`, `SQLITE_OPEN_PRIVATECACHE`, and/or `SQLITE_OPEN_URI` flags:
 4 
 5  `SQLITE_OPEN_READONLY`
 6 
 7  The database is opened in read-only mode. If the database does not already exist, an error is returned.
 8  
 9  `SQLITE_OPEN_READWRITE`
10  
11  The database is opened for reading and writing if possible, or reading only if the file is write protected by the operating system. In either case the database must already exist, otherwise an error is returned.
12  
13  `SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE`
14  
15  The database is opened for reading and writing, and is created if it does not already exist. This is the behavior that is always used for `open` method.
16   
17  @return `YES` if successful, `NO` on error.
18 
19  @see [sqlite3_open_v2()](http://sqlite.org/c3ref/open.html)
20  @see open
21  @see close
22  */
23 
24 - (BOOL)openWithFlags:(int)flags;

  使用标志和一个可选的虚拟文件系统打开一个新的数据库连接。

  falgs 可以有下面三个选项:

  "SQLITE_OPEN_READONLY" 数据库以只读模式打开,如果这个数据库不存在则返回一个 Error。

  "SQLITE_OPEN_READWRITE" 如果可能的话,数据库被打开以便进行读和写,或者当文件被系统写保护的时候只能进行读取。在这两种情况下,数据库必须存在,否则会返回错误。

  "SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE" 数据库被打开以便进行读和写,如果数据库不存在则创建。这个 flag 总是用于打开方法的行为。

 1 /** Opening a new database connection with flags and an optional virtual file system (VFS)
 2  
 3  @param flags one of the following three values, optionally combined with the `SQLITE_OPEN_NOMUTEX`, `SQLITE_OPEN_FULLMUTEX`, `SQLITE_OPEN_SHAREDCACHE`, `SQLITE_OPEN_PRIVATECACHE`, and/or `SQLITE_OPEN_URI` flags:
 4  
 5  `SQLITE_OPEN_READONLY`
 6  
 7  The database is opened in read-only mode. If the database does not already exist, an error is returned.
 8  
 9  `SQLITE_OPEN_READWRITE`
10  
11  The database is opened for reading and writing if possible, or reading only if the file is write protected by the operating system. In either case the database must already exist, otherwise an error is returned.
12  
13  `SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE`
14  
15  The database is opened for reading and writing, and is created if it does not already exist. This is the behavior that is always used for `open` method.
16  
17  @param vfsName   If vfs is given the value is passed to the vfs parameter of sqlite3_open_v2.
18  
19  @return `YES` if successful, `NO` on error.
20  
21  @see [sqlite3_open_v2()](http://sqlite.org/c3ref/open.html)
22  @see open
23  @see close
24  */
25 
26 - (BOOL)openWithFlags:(int)flags vfs:(NSString * _Nullable)vfsName;

  这个函数比上个函数多了一个可选的文件系统作参数。sqlite3_open_v2。

 1 /** Closing a database connection
 2  
 3  @return `YES` if success, `NO` on error.
 4  
 5  @see [sqlite3_close()](http://sqlite.org/c3ref/close.html)
 6  @see open
 7  @see openWithFlags:
 8  */
 9 
10 - (BOOL)close;

  关闭一个数据库连接。

 1 /** Test to see if we have a good connection to the database.
 2  
 3  This will confirm whether:
 4  
 5  - is database open
 6  - if open, it will try a simple SELECT statement and confirm that it succeeds.
 7 
 8  @return `YES` if everything succeeds, `NO` on failure.
 9  */
10 
11 @property (nonatomic, readonly) BOOL goodConnection;

  测试与当前的数据库是否有一个良好的连接。

  这将证实是否:

  • 数据库是否打开
  • 如果打开,它将尝试一个简单的 SELECT 语句并确认它执行成功。

 执行更新:

  SQL 语句中除过 SELECT 语句都可以称之为更新操作。包括 CREATEUPDATEINSERTALTERCOMMITBEGINDETACHDROPENDEXPLAINVACUUMREPLACE 等。一般只要不是以 SELECT 开头的 SQL 语句,都是更新语句。

  执行更新语句后会返回一个 BOOL 值,返回 YES 表示执行更新语句成功,返回 NO 表示出现错误,可以通过调用 -lastErrorMessage 和 -lastErrorCode 方法获取更多错误信息。

 1 /** Execute single update statement
 2  
 3  This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html), [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html) to bind values to `?` placeholders in the SQL with the optional list of parameters, and [`sqlite_step`](http://sqlite.org/c3ref/step.html) to perform the update.
 4 
 5  The optional values provided to this method should be objects (e.g. `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects), not fundamental data types (e.g. `int`, `long`, `NSInteger`, etc.). This method automatically handles the aforementioned object types, and all other object types will be interpreted as text values using the object's `description` method.
 6 
 7  @param sql The SQL to be performed, with optional `?` placeholders.
 8  
 9  @param outErr A reference to the `NSError` pointer to be updated with an auto released `NSError` object if an error if an error occurs. If `nil`, no `NSError` object will be returned.
10  
11  @param ... Optional parameters to bind to `?` placeholders in the SQL statement. These should be Objective-C objects (e.g. `NSString`, `NSNumber`, etc.), not fundamental C data types (e.g. `int`, `char *`, etc.).
12 
13  @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
14 
15  @see lastError
16  @see lastErrorCode
17  @see lastErrorMessage
18  @see [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html)
19  */
20 
21 - (BOOL)executeUpdate:(NSString*)sql withErrorAndBindings:(NSError * _Nullable *)outErr, ...;

   执行单个更新语句。

  此方法执行单个 SQL 更新语句(即不返回结果的任何 SQL 语句,如 “更新”、“插入”或“删除”。这个方法采用 ['sqlite3_prepare_v2']、['sqlite3_bind'] 去绑定值用 '?' 做占位符在 SQL 语句的可选的参数列表里,并['sqlite_step'] 执行更新。

  只有这些可选的对象类型的值才能在这个方法里面使用(例如:"NSString"、"NSNumber"、"NSNull"、"NSDate" 和 "NSData" 对象),不能使用基本数据类型(例如:"int"、"long"、"NSInteger" 等等)。这个方法自动处理上述对象类型,所有其他对象类型将被解释为文本值,使用对象的描述方法。

  参数 sql: 表示要执行的 SQL 语句,使用 '?' 做占位符。

  参数 outErr: 表示一个参考的 'NSError' 指针,如果有错误时会自动释放 'NSError' 对象。如果有错误将更新其值,把错误信息返回,如果没有错误则原值返回。

  可选参数绑定到 '?' SQL 语句中的占位符,这些参数应该是 Objective-C 对象(例如: 'NSString'、'NSNumber' 等等),不是基本的 C 数据类型(例如: 'int'、'char *' 等等)。

  返回 'YES',表示执行成功了,返回 'NO' 表示执行失败了。如果失败了,可以使用 '<lastError>'、'<lastErrorCode>' 或 '<lastErrorMessage>' 获得更多的关于失败的诊断信息。 

1 /** Execute single update statement
2  
3  @see executeUpdate:withErrorAndBindings:
4  
5  @warning **Deprecated**: Please use `<executeUpdate:withErrorAndBindings>` instead.
6  */
7 
8 - (BOOL)update:(NSString*)sql withErrorAndBindings:(NSError * _Nullable*)outErr, ...  __deprecated_msg("Use executeUpdate:withErrorAndBindings: instead");;

  已经被弃用的执行更新语句的方法。

1 - (BOOL)executeUpdate:(NSString*)sql, ...;

  执行单个更新语句。

 

 1 /** Execute single update statement
 2 
 3  This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html) and [`sqlite_step`](http://sqlite.org/c3ref/step.html) to perform the update. Unlike the other `executeUpdate` methods, this uses printf-style formatters (e.g. `%s`, `%d`, etc.) to build the SQL. Do not use `?` placeholders in the SQL if you use this method.
 4 
 5  @param format The SQL to be performed, with `printf`-style escape sequences.
 6 
 7  @param ... Optional parameters to bind to use in conjunction with the `printf`-style escape sequences in the SQL statement.
 8 
 9  @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
10 
11  @see executeUpdate:
12  @see lastError
13  @see lastErrorCode
14  @see lastErrorMessage
15  
16  @note This method does not technically perform a traditional printf-style replacement. What this method actually does is replace the printf-style percent sequences with a SQLite `?` placeholder, and then bind values to that placeholder. Thus the following command
17 
18     [db executeUpdateWithFormat:@"INSERT INTO test (name) VALUES (%@)", @"Gus"];
19 
20  is actually replacing the `%@` with `?` placeholder, and then performing something equivalent to `<executeUpdate:>`
21 
22     [db executeUpdate:@"INSERT INTO test (name) VALUES (?)", @"Gus"];
23 
24  There are two reasons why this distinction is important. First, the printf-style escape sequences can only be used where it is permissible to use a SQLite `?` placeholder. You can use it only for values in SQL statements, but not for table names or column names or any other non-value context. This method also cannot be used in conjunction with `pragma` statements and the like. Second, note the lack of quotation marks in the SQL. The `VALUES` clause was _not_ `VALUES ('%@')` (like you might have to do if you built a SQL statement using `NSString` method `stringWithFormat`), but rather simply `VALUES (%@)`.
25  */
26 
27 - (BOOL)executeUpdateWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

  和上述方法类似,只是这里不使用 '?' 做占位符。

  该方法的示例:

1 [db executeUpdateWithFormat:@"INSERT INTO test (name) VALUES (%@)", @"Gus"];

  上述方法的示例(用 ?做占位符):

1 [db executeUpdate:@"INSERT INTO test (name) VALUES (?)", @"Gus"];
 1 /** Execute single update statement
 2  
 3  This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html) and [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html) binding any `?` placeholders in the SQL with the optional list of parameters.
 4  
 5  The optional values provided to this method should be objects (e.g. `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects), not fundamental data types (e.g. `int`, `long`, `NSInteger`, etc.). This method automatically handles the aforementioned object types, and all other object types will be interpreted as text values using the object's `description` method.
 6  
 7  @param sql The SQL to be performed, with optional `?` placeholders.
 8  
 9  @param arguments A `NSArray` of objects to be used when binding values to the `?` placeholders in the SQL statement.
10  
11  @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
12  
13  @see executeUpdate:values:error:
14  @see lastError
15  @see lastErrorCode
16  @see lastErrorMessage
17  */
18 
19 - (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments;

  这里是用 '?' 做占位符,把绑定的值放在 arguments 数组里面。

 1 /** Execute single update statement
 2  
 3  This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html) and [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html) binding any `?` placeholders in the SQL with the optional list of parameters.
 4  
 5  The optional values provided to this method should be objects (e.g. `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects), not fundamental data types (e.g. `int`, `long`, `NSInteger`, etc.). This method automatically handles the aforementioned object types, and all other object types will be interpreted as text values using the object's `description` method.
 6  
 7  This is similar to `<executeUpdate:withArgumentsInArray:>`, except that this also accepts a pointer to a `NSError` pointer, so that errors can be returned.
 8 
 9  In Swift, this throws errors, as if it were defined as follows:
10  
11  `func executeUpdate(sql: String, values: [Any]?) throws -> Bool`
12  
13  @param sql The SQL to be performed, with optional `?` placeholders.
14  
15  @param values A `NSArray` of objects to be used when binding values to the `?` placeholders in the SQL statement.
16 
17  @param error A `NSError` object to receive any error object (if any).
18 
19  @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
20  
21  @see lastError
22  @see lastErrorCode
23  @see lastErrorMessage
24  
25  */
26 
27 - (BOOL)executeUpdate:(NSString*)sql values:(NSArray * _Nullable)values error:(NSError * _Nullable __autoreleasing *)error;

  这里和上述方法类似,只是多了一个 NSError 参数,用来记录错误信息返回。

 1 /** Execute single update statement
 2 
 3  This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html) and [`sqlite_step`](http://sqlite.org/c3ref/step.html) to perform the update. Unlike the other `executeUpdate` methods, this uses printf-style formatters (e.g. `%s`, `%d`, etc.) to build the SQL.
 4 
 5  The optional values provided to this method should be objects (e.g. `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects), not fundamental data types (e.g. `int`, `long`, `NSInteger`, etc.). This method automatically handles the aforementioned object types, and all other object types will be interpreted as text values using the object's `description` method.
 6 
 7  @param sql The SQL to be performed, with optional `?` placeholders.
 8 
 9  @param arguments A `NSDictionary` of objects keyed by column names that will be used when binding values to the `?` placeholders in the SQL statement.
10 
11  @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
12 
13  @see lastError
14  @see lastErrorCode
15  @see lastErrorMessage
16 */
17 
18 - (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments;

  和上述方法类似,字典 arguments 的 key 值做绑定值。

 1 /** Execute single update statement
 2 
 3  This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html) and [`sqlite_step`](http://sqlite.org/c3ref/step.html) to perform the update. Unlike the other `executeUpdate` methods, this uses printf-style formatters (e.g. `%s`, `%d`, etc.) to build the SQL.
 4 
 5  The optional values provided to this method should be objects (e.g. `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects), not fundamental data types (e.g. `int`, `long`, `NSInteger`, etc.). This method automatically handles the aforementioned object types, and all other object types will be interpreted as text values using the object's `description` method.
 6 
 7  @param sql The SQL to be performed, with optional `?` placeholders.
 8 
 9  @param args A `va_list` of arguments.
10 
11  @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
12 
13  @see lastError
14  @see lastErrorCode
15  @see lastErrorMessage
16  */
17 
18 - (BOOL)executeUpdate:(NSString*)sql withVAList: (va_list)args;

  

 1 /** Execute multiple SQL statements
 2  
 3  This executes a series of SQL statements that are combined in a single string (e.g. the SQL generated by the `sqlite3` command line `.dump` command). This accepts no value parameters, but rather simply expects a single string with multiple SQL statements, each terminated with a semicolon. This uses `sqlite3_exec`. 
 4 
 5  @param  sql  The SQL to be performed
 6  
 7  @return      `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
 8 
 9  @see executeStatements:withResultBlock:
10  @see [sqlite3_exec()](http://sqlite.org/c3ref/exec.html)
11 
12  */
13 
14 - (BOOL)executeStatements:(NSString *)sql;

  执行多个 SQL 语句。

  这个执行一系列的 SQL 语句,结合在一个单一的字符串(例如:由 'sqlite3' 命令行生成的 SQL 转储命令)。它不接受任何值参数,而是简单的期望一个包含多个SQL 语句的字符串,每个字符串以分号结束。这里使用 'sqlite3_exec'。  

 1 /** Execute multiple SQL statements with callback handler
 2  
 3  This executes a series of SQL statements that are combined in a single string (e.g. the SQL generated by the `sqlite3` command line `.dump` command). This accepts no value parameters, but rather simply expects a single string with multiple SQL statements, each terminated with a semicolon. This uses `sqlite3_exec`.
 4 
 5  @param sql       The SQL to be performed.
 6  @param block     A block that will be called for any result sets returned by any SQL statements. 
 7                   Note, if you supply this block, it must return integer value, zero upon success (this would be a good opportunity to use SQLITE_OK),
 8                   non-zero value upon failure (which will stop the bulk execution of the SQL).  If a statement returns values, the block will be called with the results from the query in NSDictionary *resultsDictionary.
 9                   This may be `nil` if you don't care to receive any results.
10 
11  @return          `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`,
12                   `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
13  
14  @see executeStatements:
15  @see [sqlite3_exec()](http://sqlite.org/c3ref/exec.html)
16 
17  */
18 
19 - (BOOL)executeStatements:(NSString *)sql withResultBlock:(__attribute__((noescape)) FMDBExecuteStatementsCallbackBlock _Nullable)block;

  执行多个 SQL 语句,且带有一个返回执行结果的回调 block。

  任何 SQL 语句返回的结果集都将调用的 block。如果你实现这个 block,它必须返回整数,零成功后(这是一个很好的机会,利用 SQLITE_OK),失败时的非零值(这将阻止 SQL 的大量执行)。如果一个语句返回值,该 block 将被称为来自 NSDictionary *resultsDictionary 查询结果。如果你不想收到结果,它可能为 nil。

 1 /** Last insert rowid
 2  
 3  Each entry in an SQLite table has a unique 64-bit signed integer key called the "rowid". The rowid is always available as an undeclared column named `ROWID`, `OID`, or `_ROWID_` as long as those names are not also used by explicitly declared columns. If the table has a column of type `INTEGER PRIMARY KEY` then that column is another alias for the rowid.
 4  
 5  This routine returns the rowid of the most recent successful `INSERT` into the database from the database connection in the first argument. As of SQLite version 3.7.7, this routines records the last insert rowid of both ordinary tables and virtual tables. If no successful `INSERT`s have ever occurred on that database connection, zero is returned.
 6  
 7  @return The rowid of the last inserted row.
 8  
 9  @see [sqlite3_last_insert_rowid()](http://sqlite.org/c3ref/last_insert_rowid.html)
10 
11  */
12 
13 @property (nonatomic, readonly) int64_t lastInsertRowId;

  最后插入的 "rowid"。

  在 SQLite 表的每一个入口都有一个唯一的64位有符号整数键称为 "rowid"。rowid 是可作为一个未命名列 "ROWID"、"OID" 或 "_ROWID_" 只要这些名字是不是也被显式声明的列使用。如果表中有一列式 "INTEGER PRIMARY KEY" 那列是另一个别名为 ROWID 。

  这个例程返回rowid的最近成功插入到数据库` `从数据库连接在第一个参数。由于SQLite版本3.7.7,这个例程记录普通表和虚表的最后插入数据。如果在该数据库连接上没有成功的“插入”,则返回零。

  返回值是最后插入的 rowid。

 1 /** The number of rows changed by prior SQL statement.
 2  
 3  This function returns the number of database rows that were changed or inserted or deleted by the most recently completed SQL statement on the database connection specified by the first parameter. Only changes that are directly specified by the INSERT, UPDATE, or DELETE statement are counted.
 4  
 5  @return The number of rows changed by prior SQL statement.
 6  
 7  @see [sqlite3_changes()](http://sqlite.org/c3ref/changes.html)
 8  
 9  */
10 
11 @property (nonatomic, readonly) int changes;

  通过前面的 SQL 语句更改的行数。

  此函数返回由第一个参数指定的数据库连接中最近完成的 SQL 语句更改或插入或删除的数据库行的数目。只有插入、更新或删除语句直接指定的更改才算在内。

 检索结果:

 1 /** Execute select statement
 2 
 3  Executing queries returns an `<FMResultSet>` object if successful, and `nil` upon failure.  Like executing updates, there is a variant that accepts an `NSError **` parameter.  Otherwise you should use the `<lastErrorMessage>` and `<lastErrorMessage>` methods to determine why a query failed.
 4  
 5  In order to iterate through the results of your query, you use a `while()` loop.  You also need to "step" (via `<[FMResultSet next]>`) from one record to the other.
 6  
 7  This method employs [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html) for any optional value parameters. This  properly escapes any characters that need escape sequences (e.g. quotation marks), which eliminates simple SQL errors as well as protects against SQL injection attacks. This method natively handles `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects. All other object types will be interpreted as text values using the object's `description` method.
 8 
 9  @param sql The SELECT statement to be performed, with optional `?` placeholders.
10 
11  @param ... Optional parameters to bind to `?` placeholders in the SQL statement. These should be Objective-C objects (e.g. `NSString`, `NSNumber`, etc.), not fundamental C data types (e.g. `int`, `char *`, etc.).
12 
13  @return A `<FMResultSet>` for the result set upon success; `nil` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
14  
15  @see FMResultSet
16  @see [`FMResultSet next`](<[FMResultSet next]>)
17  @see [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html)
18  
19  @note You cannot use this method from Swift due to incompatibilities between Swift and Objective-C variadic implementations. Consider using `<executeQuery:values:>` instead.
20  */
21 
22 - (FMResultSet * _Nullable)executeQuery:(NSString*)sql, ...;

  执行 SELECT 语句。

  执行查询如果成功返回一个 '<FMResultSet>'对象,如果失败返回 nil。和执行更新类似,有一个变种这里接受一个'nserror ** ' 参数。否则,你应该使用 '<lastErrorMessage >' 和 '<lastErrorMessage>' 方法确定一个查询失败的原因。

  为了遍历你的查询结果,你需要用 'while()' 循环。你也需要 'step'(via '<[FMResultSet next]>')从一个记录到其他。

  该方法采用 ['sqlite3_bind'] 任何可选参数。这可以适当地转义任何需要转义序列的字符(例如引号),这消除了简单的 SQL 错误,也防止了 SQL 注入攻击。这种方法来处理 'NSString'、'NSNumber'、'nsnull'、'NSDate'和 'NSData' 对象。所有其他对象类型将被解释为文本值,使用对象的“描述”方法。 

   参数 sql: 要执行的 SELECT 语句,用 '?' 做占位符。

1 - (FMResultSet * _Nullable)executeQueryWithFormat:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);

  使用示例:

1 [db executeQueryWithFormat:@"SELECT * FROM test WHERE name=%@", @"Gus"];

  使用示例(用 '?' 做占位符):

1 [db executeQuery:@"SELECT * FROM test WHERE name=?", @"Gus"];

  和上面的更新语句及其类似。

1 - (FMResultSet * _Nullable)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments;
1 - (FMResultSet * _Nullable)executeQuery:(NSString *)sql values:(NSArray * _Nullable)values error:(NSError * _Nullable __autoreleasing *)error;
1 - (FMResultSet * _Nullable)executeQuery:(NSString *)sql withParameterDictionary:(NSDictionary * _Nullable)arguments;
1 // Documentation forthcoming.
2 - (FMResultSet * _Nullable)executeQuery:(NSString *)sql withVAList:(va_list)args;

 Transactions:

 1 /** Begin a transaction
 2  
 3  @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
 4  
 5  @see commit
 6  @see rollback
 7  @see beginDeferredTransaction
 8  @see isInTransaction
 9  */
10 
11 - (BOOL)beginTransaction;

  开始一个事务。

 1 - (BOOL)beginTransaction;
 2 
 3 /** Begin a deferred transaction
 4  
 5  @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
 6  
 7  @see commit
 8  @see rollback
 9  @see beginTransaction
10  @see isInTransaction
11  */
12 
13 - (BOOL)beginDeferredTransaction;

  开始一个延期的事务。

 1 /** Commit a transaction
 2 
 3  Commit a transaction that was initiated with either `<beginTransaction>` or with `<beginDeferredTransaction>`.
 4  
 5  @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
 6  
 7  @see beginTransaction
 8  @see beginDeferredTransaction
 9  @see rollback
10  @see isInTransaction
11  */
12 
13 - (BOOL)commit;

  提交一个事务。

 1 /** Rollback a transaction
 2 
 3  Rollback a transaction that was initiated with either `<beginTransaction>` or with `<beginDeferredTransaction>`.
 4 
 5  @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
 6  
 7  @see beginTransaction
 8  @see beginDeferredTransaction
 9  @see commit
10  @see isInTransaction
11  */
12 
13 - (BOOL)rollback;

  回滚一个事务。

1 /** Identify whether currently in a transaction or not
2   
3  @see beginTransaction
4  @see beginDeferredTransaction
5  @see commit
6  @see rollback
7  */
8 
9 @property (nonatomic, readonly) BOOL isInTransaction;

  标识当前是否是在事务中。

 缓存语句和结果集:

1 /** Clear cached statements */
2 
3 - (void)clearCachedStatements;

  清除缓存语句。

1 /** Close all open result sets */
2 
3 - (void)closeOpenResultSets;

  关闭所有打开的结果集。

1 /** Whether database has any open result sets
2  
3  @return `YES` if there are open result sets; `NO` if not.
4  */
5 
6 @property (nonatomic, readonly) BOOL hasOpenResultSets;

  数据库是否有任何打开的结果集。

1 /** Whether should cache statements or not
2   */
3 
4 @property (nonatomic) BOOL shouldCacheStatements;

  是否应该缓存语句。

1 /** Interupt pending database operation
2  
3  This method causes any pending database operation to abort and return at its earliest opportunity
4  
5  @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
6  
7  */
8 
9 - (BOOL)interrupt;

  中断挂起的数据库操作。

  此方法会导致任何挂起的数据库操作提前终止并返回。

 加密方法:

 1 /** Set encryption key.
 2  
 3  @param key The key to be used.
 4 
 5  @return `YES` if success, `NO` on error.
 6 
 7  @see https://www.zetetic.net/sqlcipher/
 8  
 9  @warning You need to have purchased the sqlite encryption extensions for this method to work.
10  */
11 
12 - (BOOL)setKey:(NSString*)key;

  设置加密密钥。

 1 /** Reset encryption key
 2 
 3  @param key The key to be used.
 4 
 5  @return `YES` if success, `NO` on error.
 6 
 7  @see https://www.zetetic.net/sqlcipher/
 8 
 9  @warning You need to have purchased the sqlite encryption extensions for this method to work.
10  */
11 
12 - (BOOL)rekey:(NSString*)key;

  重置加密密钥。

 1 /** Set encryption key using `keyData`.
 2  
 3  @param keyData The `NSData` to be used.
 4 
 5  @return `YES` if success, `NO` on error.
 6 
 7  @see https://www.zetetic.net/sqlcipher/
 8  
 9  @warning You need to have purchased the sqlite encryption extensions for this method to work.
10  */
11 
12 - (BOOL)setKeyWithData:(NSData *)keyData;

  设置加密密钥。使用一个 NSData 。

 1 /** Reset encryption key using `keyData`.
 2 
 3  @param keyData The `NSData` to be used.
 4 
 5  @return `YES` if success, `NO` on error.
 6 
 7  @see https://www.zetetic.net/sqlcipher/
 8 
 9  @warning You need to have purchased the sqlite encryption extensions for this method to work.
10  */
11 
12 - (BOOL)rekeyWithData:(NSData *)keyData;

  重置。

 通用查询方法:

1 /** The path of the database file
2  */
3 
4 @property (nonatomic, readonly, nullable) NSString *databasePath;

  数据库文件的路径。

1 /** The file URL of the database file.
2  */
3 
4 @property (nonatomic, readonly, nullable) NSURL *databaseURL;

  数据库文件的文件URL。

1 /** The underlying SQLite handle 
2  
3  @return The `sqlite3` pointer.
4  
5  */
6 
7 @property (nonatomic, readonly) void *sqliteHandle;

  底层 SQLite 处理。

 检索错误代码:

 1 /** Last error message
 2  
 3  Returns the English-language text that describes the most recent failed SQLite API call associated with a database connection. If a prior API call failed but the most recent API call succeeded, this return value is undefined.
 4 
 5  @return `NSString` of the last error message.
 6  
 7  @see [sqlite3_errmsg()](http://sqlite.org/c3ref/errcode.html)
 8  @see lastErrorCode
 9  @see lastError
10  
11  */
12 
13 - (NSString*)lastErrorMessage;

  最后一个错误消息。

  返回英语语言文字描述最近的失败 SQLite API 调用一个数据库连接相关。如果前面的API调用失败,但最近的API调用成功,这个返回值是未定义的。

 1 /** Last error code
 2  
 3  Returns the numeric result code or extended result code for the most recent failed SQLite API call associated with a database connection. If a prior API call failed but the most recent API call succeeded, this return value is undefined.
 4  
 5  @return Integer value of the last error code.
 6  
 7  @see [sqlite3_errcode()](http://sqlite.org/c3ref/errcode.html)
 8  @see lastErrorMessage
 9  @see lastError
10  
11  */
12 
13 - (int)lastErrorCode;

  最后一个错误码。

  返回数字结果代码或最近的扩展结果代码失败 SQLite API 调用一个数据库连接相关。如果前面的 API 调用失败,但最近的 API 调用成功,这个返回值是未定义的。

 1 /** Last extended error code
 2  
 3  Returns the numeric extended result code for the most recent failed SQLite API call associated with a database connection. If a prior API call failed but the most recent API call succeeded, this return value is undefined.
 4  
 5  @return Integer value of the last extended error code.
 6  
 7  @see [sqlite3_errcode()](http://sqlite.org/c3ref/errcode.html)
 8  @see [2. Primary Result Codes versus Extended Result Codes](http://sqlite.org/rescode.html#primary_result_codes_versus_extended_result_codes)
 9  @see [5. Extended Result Code List](http://sqlite.org/rescode.html#extrc)
10  @see lastErrorMessage
11  @see lastError
12  
13  */
14 
15 - (int)lastExtendedErrorCode;

  最后扩展错误码。

  返回最近的数字扩展结果代码失败 SQLite API 调用一个数据库连接相关。如果前面的 API 调用失败,但最近的 API 调用成功,这个返回值是未定义的。

 1 /** Had error
 2 
 3  @return `YES` if there was an error, `NO` if no error.
 4  
 5  @see lastError
 6  @see lastErrorCode
 7  @see lastErrorMessage
 8  
 9  */
10 
11 - (BOOL)hadError;

  是否有错误。如果有错误返回 YES,如果没有则返回 NO。

 1 /** Last error
 2 
 3  @return `NSError` representing the last error.
 4  
 5  @see lastErrorCode
 6  @see lastErrorMessage
 7  
 8  */
 9 
10 - (NSError *)lastError;

  最后一个错误。返回 NSError 代表最后的错误。

1 // description forthcoming
2 @property (nonatomic) NSTimeInterval maxBusyRetryTimeInterval;

  描述即将到来的。

 保存点:

 1 /** Start save point
 2  
 3  @param name Name of save point.
 4  
 5  @param outErr A `NSError` object to receive any error object (if any).
 6  
 7  @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
 8  
 9  @see releaseSavePointWithName:error:
10  @see rollbackToSavePointWithName:error:
11  */
12 
13 - (BOOL)startSavePointWithName:(NSString*)name error:(NSError * _Nullable *)outErr;

  开始保存点。

  参数 name: 保存点的名字。

  参数 outError: 一个 'NSError' 对象接收任何 NSError 对象。

 1 /** Release save point
 2 
 3  @param name Name of save point.
 4  
 5  @param outErr A `NSError` object to receive any error object (if any).
 6  
 7  @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
 8 
 9  @see startSavePointWithName:error:
10  @see rollbackToSavePointWithName:error:
11  
12  */
13 
14 - (BOOL)releaseSavePointWithName:(NSString*)name error:(NSError * _Nullable *)outErr;

  释放保存点。

 1 /** Roll back to save point
 2 
 3  @param name Name of save point.
 4  @param outErr A `NSError` object to receive any error object (if any).
 5  
 6  @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
 7  
 8  @see startSavePointWithName:error:
 9  @see releaseSavePointWithName:error:
10  
11  */
12 
13 - (BOOL)rollbackToSavePointWithName:(NSString*)name error:(NSError * _Nullable *)outErr;

  回滚到保存点。

 1 /** Start save point
 2 
 3  @param block Block of code to perform from within save point.
 4  
 5  @return The NSError corresponding to the error, if any. If no error, returns `nil`.
 6 
 7  @see startSavePointWithName:error:
 8  @see releaseSavePointWithName:error:
 9  @see rollbackToSavePointWithName:error:
10  
11  */
12 
13 - (NSError * _Nullable)inSavePoint:(__attribute__((noescape)) void (^)(BOOL *rollback))block;

  开始保存点。block 在保存点内执行的代码块。

 SQLite 库的状态:

1 /** Test to see if the library is threadsafe
2 
3  @return `NO` if and only if SQLite was compiled with mutexing code omitted due to the SQLITE_THREADSAFE compile-time option being set to 0.
4 
5  @see [sqlite3_threadsafe()](http://sqlite.org/c3ref/threadsafe.html)
6  */
7 
8 + (BOOL)isSQLiteThreadSafe;

  测试看看当前数据库是否是线程安全的。

1 /** Run-time library version numbers
2  
3  @return The sqlite library version string.
4  
5  @see [sqlite3_libversion()](http://sqlite.org/c3ref/libversion.html)
6  */
7 
8 + (NSString*)sqliteLibVersion;

  返回当前 SQLite 库的版本。

1 + (NSString*)FMDBUserVersion;
2 
3 + (SInt32)FMDBVersion;

  返回 FMDB 使用的版本,返回 FMDB 版本。

 创建 SQL 功能:

 1 /** Adds SQL functions or aggregates or to redefine the behavior of existing SQL functions or aggregates.
 2  
 3  For example:
 4  
 5     [db makeFunctionNamed:@"RemoveDiacritics" arguments:1 block:^(void *context, int argc, void **argv) {
 6         SqliteValueType type = [self.db valueType:argv[0]];
 7         if (type == SqliteValueTypeNull) {
 8             [self.db resultNullInContext:context];
 9              return;
10         }
11         if (type != SqliteValueTypeText) {
12             [self.db resultError:@"Expected text" context:context];
13             return;
14         }
15         NSString *string = [self.db valueString:argv[0]];
16         NSString *result = [string stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:nil];
17         [self.db resultString:result context:context];
18     }];
19 
20     FMResultSet *rs = [db executeQuery:@"SELECT * FROM employees WHERE RemoveDiacritics(first_name) LIKE 'jose'"];
21     NSAssert(rs, @"Error %@", [db lastErrorMessage]);
22  
23  @param name Name of function.
24 
25  @param arguments Maximum number of parameters.
26 
27  @param block The block of code for the function.
28 
29  @see [sqlite3_create_function()](http://sqlite.org/c3ref/create_function.html)
30  */
31 
32 - (void)makeFunctionNamed:(NSString *)name arguments:(int)arguments block:(void (^)(void *context, int argc, void * _Nonnull * _Nonnull argv))block;

  添加 SQL 函数或聚合或重新定义现有 SQL 函数或聚合的行为。

1 - (void)makeFunctionNamed:(NSString *)name maximumArguments:(int)count withBlock:(void (^)(void *context, int argc, void * _Nonnull * _Nonnull argv))block __deprecated_msg("Use makeFunctionNamed:arguments:block:");

  废弃的方法。

1 typedef NS_ENUM(int, SqliteValueType) {
2     SqliteValueTypeInteger = 1,
3     SqliteValueTypeFloat   = 2,
4     SqliteValueTypeText    = 3,
5     SqliteValueTypeBlob    = 4,
6     SqliteValueTypeNull    = 5
7 };

  定义一个表示 SqliteValue 类型的枚举。

1 - (SqliteValueType)valueType:(void *)argv;
1 /**
2  Get integer value of parameter in custom function.
3  
4  @param value The argument whose value to return.
5  @return The integer value.
6  
7  @see makeFunctionNamed:arguments:block:
8  */
9 - (int)valueInt:(void *)value;

  在自定义函数中根据参数值返回一个 int。

1 /**
2  Get long value of parameter in custom function.
3  
4  @param value The argument whose value to return.
5  @return The long value.
6  
7  @see makeFunctionNamed:arguments:block:
8  */
9 - (long long)valueLong:(void *)value;

  long。

1 /**
2  Get double value of parameter in custom function.
3  
4  @param value The argument whose value to return.
5  @return The double value.
6  
7  @see makeFunctionNamed:arguments:block:
8  */
9 - (double)valueDouble:(void *)value;

  double。

1 /**
2  Get `NSData` value of parameter in custom function.
3  
4  @param value The argument whose value to return.
5  @return The data object.
6  
7  @see makeFunctionNamed:arguments:block:
8  */
9 - (NSData * _Nullable)valueData:(void *)value;

  NSData。

1 /**
2  Get string value of parameter in custom function.
3  
4  @param value The argument whose value to return.
5  @return The string value.
6  
7  @see makeFunctionNamed:arguments:block:
8  */
9 - (NSString * _Nullable)valueString:(void *)value;

  NSString。

1 /**
2  Return null value from custom function.
3  
4  @param context The context to which the null value will be returned.
5  
6  @see makeFunctionNamed:arguments:block:
7  */
8 - (void)resultNullInContext:(void *)context NS_SWIFT_NAME(resultNull(context:));
1 /**
2  Return integer value from custom function.
3  
4  @param value The integer value to be returned.
5  @param context The context to which the value will be returned.
6  
7  @see makeFunctionNamed:arguments:block:
8  */
9 - (void)resultInt:(int) value context:(void *)context;

  返回 int。

1 /**
2  Return long value from custom function.
3  
4  @param value The long value to be returned.
5  @param context The context to which the value will be returned.
6  
7  @see makeFunctionNamed:arguments:block:
8  */
9 - (void)resultLong:(long long)value context:(void *)context;

  返回 long。

1 /**
2  Return double value from custom function.
3  
4  @param value The double value to be returned.
5  @param context The context to which the value will be returned.
6  
7  @see makeFunctionNamed:arguments:block:
8  */
9 - (void)resultDouble:(double)value context:(void *)context;

  返回 double。

 1 /**
 2  Return `NSData` object from custom function.
 3  
 4  @param data The `NSData` object to be returned.
 5  @param context The context to which the value will be returned.
 6  
 7  @see makeFunctionNamed:arguments:block:
 8  */
 9 - (void)resultData:(NSData *)data context:(void *)context;
10 
11 /**
12  Return string value from custom function.
13  
14  @param value The string value to be returned.
15  @param context The context to which the value will be returned.
16  
17  @see makeFunctionNamed:arguments:block:
18  */
19 - (void)resultString:(NSString *)value context:(void *)context;
20 
21 /**
22  Return error string from custom function.
23  
24  @param error The error string to be returned.
25  @param context The context to which the error will be returned.
26  
27  @see makeFunctionNamed:arguments:block:
28  */
29 - (void)resultError:(NSString *)error context:(void *)context;
30 
31 /**
32  Return error code from custom function.
33  
34  @param errorCode The integer error code to be returned.
35  @param context The context to which the error will be returned.
36  
37  @see makeFunctionNamed:arguments:block:
38  */
39 - (void)resultErrorCode:(int)errorCode context:(void *)context;

  和上述方法类似,返回不同类型的值。

1 /**
2  Report memory error in custom function.
3  
4  @param context The context to which the error will be returned.
5  
6  @see makeFunctionNamed:arguments:block:
7  */
8 - (void)resultErrorNoMemoryInContext:(void *)context NS_SWIFT_NAME(resultErrorNoMemory(context:));

  在自定义函数中报告内存错误。

1 /**
2  Report that string or BLOB is too long to represent in custom function.
3  
4  @param context The context to which the error will be returned.
5  
6  @see makeFunctionNamed:arguments:block:
7  */
8 - (void)resultErrorTooBigInContext:(void *)context NS_SWIFT_NAME(resultErrorTooBig(context:));

  报告字符串或项太长不能在自定义函数中表示。

 日期格式:

 1 /** Generate an `NSDateFormatter` that won't be broken by permutations of timezones or locales.
 2  
 3  Use this method to generate values to set the dateFormat property.
 4  
 5  Example:
 6 
 7     myDB.dateFormat = [FMDatabase storeableDateFormat:@"yyyy-MM-dd HH:mm:ss"];
 8 
 9  @param format A valid NSDateFormatter format string.
10  
11  @return A `NSDateFormatter` that can be used for converting dates to strings and vice versa.
12  
13  @see hasDateFormatter
14  @see setDateFormat:
15  @see dateFromString:
16  @see stringFromDate:
17  @see storeableDateFormat:
18 
19  @warning Note that `NSDateFormatter` is not thread-safe, so the formatter generated by this method should be assigned to only one FMDB instance and should not be used for other purposes.
20 
21  */
22 
23 + (NSDateFormatter *)storeableDateFormat:(NSString *)format;

  生成一个 'NSDateFormatter' 不受时区或位置破坏。
  使用这种方法生成的值来设置日期格式属性。

  注意: 'NSDateFormatter' 不是线程安全的,所以该方法生成的格式化程序应该只分配给一个 FMDB 实例,不得用于其他用途。

 1 /** Test whether the database has a date formatter assigned.
 2  
 3  @return `YES` if there is a date formatter; `NO` if not.
 4  
 5  @see hasDateFormatter
 6  @see setDateFormat:
 7  @see dateFromString:
 8  @see stringFromDate:
 9  @see storeableDateFormat:
10  */
11 
12 - (BOOL)hasDateFormatter;

  判断数据库是否有指定的日期格式。

 1 /** Set to a date formatter to use string dates with sqlite instead of the default UNIX timestamps.
 2  
 3  @param format Set to nil to use UNIX timestamps. Defaults to nil. Should be set using a formatter generated using FMDatabase::storeableDateFormat.
 4  
 5  @see hasDateFormatter
 6  @see setDateFormat:
 7  @see dateFromString:
 8  @see stringFromDate:
 9  @see storeableDateFormat:
10  
11  @warning Note there is no direct getter for the `NSDateFormatter`, and you should not use the formatter you pass to FMDB for other purposes, as `NSDateFormatter` is not thread-safe.
12  */
13 
14 - (void)setDateFormat:(NSDateFormatter *)format;

  设置日期格式的日期使用字符串而不是默认的 Unix 时间戳,SQLite。

  设置为 nil 使用Unix时间戳。默认为 nil。应设置使用格式化程序 storeabledateformat FMDatabase:返回的 NSDateFormatter 对象。

  注意: 有没有直接的 'NSDateFormatter',你不应该使用格式化程序传递给其他用途 FMDB,作为 'NSDateFormatter' 不是线程安全的。

 1 /** Convert the supplied NSString to NSDate, using the current database formatter.
 2  
 3  @param s `NSString` to convert to `NSDate`.
 4  
 5  @return The `NSDate` object; or `nil` if no formatter is set.
 6  
 7  @see hasDateFormatter
 8  @see setDateFormat:
 9  @see dateFromString:
10  @see stringFromDate:
11  @see storeableDateFormat:
12  */
13 
14 - (NSDate * _Nullable)dateFromString:(NSString *)s;

  将参数 NSString 转化为 NSDate,使用当前数据库格式。

 1 /** Convert the supplied NSDate to NSString, using the current database formatter.
 2  
 3  @param date `NSDate` of date to convert to `NSString`.
 4 
 5  @return The `NSString` representation of the date; `nil` if no formatter is set.
 6  
 7  @see hasDateFormatter
 8  @see setDateFormat:
 9  @see dateFromString:
10  @see stringFromDate:
11  @see storeableDateFormat:
12  */
13 
14 - (NSString *)stringFromDate:(NSDate *)date;

  将参数 NSDate 转化为 NSString,使用当前数据库格式。

 Objective-C wrapper for 'sqlite3_stmt':

1  This is a wrapper for a SQLite `sqlite3_stmt`. Generally when using FMDB you will not need to interact directly with `FMStatement`, but rather with `<FMDatabase>` and `<FMResultSet>` only.
2  
3  ### See also
4  
5  - `<FMDatabase>`
6  - `<FMResultSet>`
7  - [`sqlite3_stmt`](http://www.sqlite.org/c3ref/stmt.html)

  这是一个 SQLite 'sqlite3_stmt' 包装。通常使用 FMDB 时你不需要直接使用 'FMStatement',而是仅使用 '<FMDatabase>' 和 '<FMResultSet>'。

 

 FMStatement 类:

 1 @interface FMStatement : NSObject {
 2     void *_statement;
 3     NSString *_query;
 4     long _useCount;
 5     BOOL _inUse;
 6 }
 7 
 8 ///-----------------
 9 /// @name Properties
10 ///-----------------
11 
12 /** Usage count */
13 
14 @property (atomic, assign) long useCount;
15 
16 /** SQL statement */
17 
18 @property (atomic, retain) NSString *query;
19 
20 /** SQLite sqlite3_stmt
21  
22  @see [`sqlite3_stmt`](http://www.sqlite.org/c3ref/stmt.html)
23  */
24 
25 @property (atomic, assign) void *statement;
26 
27 /** Indication of whether the statement is in use */
28 
29 @property (atomic, assign) BOOL inUse;
30 
31 ///----------------------------
32 /// @name Closing and Resetting
33 ///----------------------------
34 
35 /** Close statement */
36 
37 - (void)close;
38 
39 /** Reset statement */
40 
41 - (void)reset;
42 
43 @end

  成员变量和属性。

  int useCount; 使用计数。

  NSString *query; SQL 语句。

  void *statement; 声明陈述。

  BooL inUse; 指示语句是否在使用中的指示。

 

  最后是:关闭语句和重置语句。

END

posted @ 2017-06-26 02:07  鳄鱼不怕牙医不怕  阅读(1254)  评论(0编辑  收藏  举报