IOS developmentStudy Notes iOSDatabaseProgramming is the content to be introduced in this article, mainly to understandIOS developmentInDatabaseFor more information, see. InIOS developmentUsually used in the processDatabase, AndSQLite3Supported by the iPhoneDatabase. The following is a brief introduction to the iPhoneSQLite3 DatabaseUsage:
SQLite3Is a lightweight database, fully written in C language, easy to use. It is a database embedded into the program process, and some other databases MySQL, ms SQL), it does not have independent processes.
1. Open the database
First declare a database variable
- sqlite3 *db = NULL;
Each SQLite3 is a file. To open a database, you only need to call the sqlite3_open function once:
- SQLITE_API int sqlite3_open(
- const char *filename, /* Database filename (UTF-8) */
- sqlite3 **ppDb /* OUT: SQLite db handle */
- );
Filename indicates the database path, ppDb indicates the returned sqlite3 database, and SQLITE3_ OK indicates normal. For example, open the "Test. db" database under the Document directory:
- int result = sqlite3_open([dbPath UTF8String], &db);
- NSAssert(result==SQLITE_OK, @”Can’t open database %@”, dbPath);
Note: If the database in the dbPath path does not exist, an empty database will be created when you open the database.
2. Execute SQL statements
SQLite3 has three ways to execute SQL statements:
Use the sqlite3_exec () function to execute non-query statements, such as create table, INSERT, REPLACE, and DELETE.
- SQLITE_API int sqlite3_exec(
- sqlite3*, /* An open database */
- const char *sql, /* SQL to be evaluated */
- int (*callback)(void*,int,char**,char**), /* Callback function */
- void *, /* 1st argument to callback */
- char **errmsg /* Error msg written here */
- );
For example, to create a table that represents a person, you can use the sqlite3_exec () function. The person table includes the following fields: name, gender, age, and birthday. The Code is as follows:
- char *errmsg = NULL;
- result = sqlite3_exec(db, “CREATE TABLE person (”
- “name TEXT,”
- “gender INTEGER,”
- “age INTEGER,”
- “birthday DATE)”, NULL, NULL, &errmsg);
- NSAssert(result==SQLITE_OK, @”Can’t CREATE TABLE: %s”, errmsg);
The sqlite3_get_table () function is applicable to the SELECT statement of the query statement. Because this function returns a string array, this function cannot be used to return binary data.
- SQLITE_API int sqlite3_get_table(
- sqlite3 *db, /* An open database */
- const char *zSql, /* SQL to be evaluated */
- char ***pazResult, /* Results of the query */
- int *pnRow, /* Number of result rows written here */
- int *pnColumn, /* Number of result columns written here */
- char **pzErrmsg /* Error msg written here */
- );
For example, we want to query all data in person:
- char **table = NULL;
- int row,col;
- result = sqlite3_get_table(db, “SELECT name,age,birthday from person”, &table, &row, &col, &errmsg);
- NSAssert(result==SQLITE_OK, @”SQL ERROR: %s”, errmsg);
The string array table contains all the table information. In this example, each row contains three elements: name, age, and birthday. The element of the first row of table is the field name, for example, table [0] is "name", table [1] is "age", and table [2] is "birthday ". The second row of the table is the actual data. For example, table [3] is "Zhang San", table [4] is "24", and table [5] is "12:00:01 ″.
You can use sqlite3_prepare (), sqlite3_step (), sqlite3_bind_xxx (), sqlite3_finalize (), and other functions to obtain data row by row and binary data.
Sqlite3_prepare () is used to compile an SQL statement and returns a sqlite3_stmt structure pointer. Sqlite3_stmt structure pointer is used in functions such as sqlite3_step (), sqlite3_bind_xxx (), and sqlite3_finalize. Sqlite3_step () is used to point the current record to the next one. Sqlite3_bind_xxx () and other functions obtain the values of each field in the current record. Finally, call sqlite3_finalize () to release some resources.
3. Other advanced applications
1) create a memory database
UsuallySQLite3 DatabaseIt is stored in disk files, so database read/write operations will be wonderful to read and write disk files. SQLite3 can also create a database in the memory, but note that the data created in the memory is easily lost when the program exits and the memory is released ). We know that disk read/write is much slower than memory read/write. Sometimes we have high requirements on read/write speed, but we are not interested in whether data is saved permanently, in this case, it is better to use the memory database.
It is easy to create a memory database. You only need to change the name of the opened database to ": memory.
- int result = sqlite3_open(“:memory:”, &db);
- NSAssert(result==SQLITE_OK, @”Can’t open database %@”, dbPath);
2) create a custom SQL statement Function
Sometimes we want to define a function to determine whether a person meets the marital condition that a man is older than 22 years old and a female is later than 20 years old). Unfortunately, the SQL statement does not provide such a complicated judgment. It is very convenient to create a UDF.
- static void db_func_canmarry(sqlite3_context *context, int argc, sqlite3_value **argv){
- int gender = sqlite3_value_int(argv[0]);
- int age = sqlite3_value_int(argv[1]);
- int canmarry = 0;
- if (gender == 0 && age > 22) {
- canmarry = 1;
- }
- else if (gender == 1 && age > 20){
- canmarry = 0;
- }
- sqlite3_result_int(context, canmarry);
- }
Define the function and add it to the Database SQL statement.
- sqlite3_create_function(db, “CANMARRY”, 2, SQLITE_UTF8, NULL, &db_func_canmarry, NULL, NULL);
In the future, we will execute the SQL statement SELECT * FROM person WHERE CANMARRY (gender, age) to get all the people who meet the wedding conditions.
3) Paging Query
DatabaseWhen querying, we often encounter the need to display results by page. At this time, it is very convenient to use the SQL statement LIMIT keyword. For example, 10 rows from 20 rows are returned.
- SELECT * FROM person WHERE CANMARRY(gender,age) LIMIT 20, 10
Summary:IOS developmentStudy Notes iOSDatabaseI hope this article will help you with programming! For more informationIOS developmentFor more information, see the IOS development channel.