Chapter 12. Database Storage Using SQLite3

WHAT YOU WILL LEARN IN THIS CHAPTER

  • How to use the SQLite3 database in your Xcode project

  • How to create and open a SQLite3 database

  • How to use the various SQLite3 functions to execute SQL strings

  • How to use bind variables to insert values into a SQL string

In the previous chapter, you learned about data persistence using files. For simple applications, you can write the data you want to persist to a simple text file. For more structured data, you can use a property list. For large and complex data, it is more efficient to store them using a database. The iPhone comes with the SQLite3 database library, which you can use to store your data. With your data stored in a database, your application can populate a Table view or store a large amount of data in a structured manner.

This chapter shows you how to use the embedded SQLite3 database in your applications.

USING SQLITE3

To use a SQLite3 database in your application, you first need to add the libsqlite3.dylib library to your Xcode project. Use the following Try It Out to find out how. You will need to download the code files indicated for this and the rest of the Try It Out features in this chapter.

Note

Chapter 13 discusses the various folders that you can access within your application's sandbox.

CREATING AND OPENING A DATABASE

After the necessary library is added to the project, you can open a database for usage. You will use the various C functions included with SQLite3 to create or open a database, as demonstrated in the following Try It Out.

Examining the Database Created

If the database is created successfully, it can be found in the Documents folder of your application's sandbox. As discussed in Chapter 13, you can locate the Documents folder of your application on the iPhone Simulator in the ~/Library/Application Support/iPhone Simulator/User/Applications/<App_ID>/Documents/ folder. Figure 12-2 shows the database.sql file.

Figure 12-2

Figure 12.2. Figure 12-2

Creating a Table

After the database is created, you can create a table to store some data. In the following Try It Out, you learn how to create a table with two text fields. For illustration purposes, create a table named Contacts, with two fields called email and name.

Note

For a jumpstart in the SQL language, check out the SQL tutorial at: http://w3schools.com/sql/default.asp.

Inserting Records

After the table is created, you can insert some records into it. The following Try It Out shows you how to write two rows of records into the table created in the previous section.

Bind Variables

One of the common tasks involved in formulating SQL strings is the need to insert values into the query string and making sure that the string is well formulated and that it does not contain invalid characters. Earlier in the Inserting Records section, you saw that to insert a row into the database, you had to formulate your SQL statement like this:

NSString *sql = [NSString stringWithFormat:
        @"INSERT OR REPLACE INTO '%@' ('%@', '%@') VALUES ('%@','%@')",
        tableName, field1, field2, field1Value, field2Value];

    char *err;
    if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK)
    {
        sqlite3_close(db);
        NSAssert(0, @"Error updating table.");
    }

SQLite supports a feature known as bind variables to help you formulate your SQL string. For example, the preceding SQL string can be formulated as follows using bind variables:

NSString *sqlStr = [NSString stringWithFormat:
        @"INSERT OR REPLACE INTO '%@' ('%@', '%@') VALUES (?,?)",
        tableName, field1, field2];

    const char *sql = [sqlStr UTF8String];

Here, the ? is a placeholder for you to replace with the actual value of the query. In the preceding statement, assuming that tableName is Contacts, field1 is email, and field2 is name, the sql is now:

INSERT OR REPLACE INTO Contacts ('email', 'name') VALUES (?,?)

Note

Note that the ? can be inserted only into the VALUES and WHERE section of the SQL statement; you cannot insert it into a table name, for example. The following statement would be invalid: INSERT OR REPLACE INTO ? ("email", "name") VALUES (?,?).

To substitute the values for the ?, you need to create a sqlite3_stmt object and use the To substitute the values for the sqlite3_prepare_v2() function to compile the SQL string into a binary form and then insert the placeholder values using the sqlite3_bind_text() function, like this:

sqlite3_stmt *statement;

if (sqlite3_prepare_v2(db, sql, −1, &statement, nil) == SQLITE_OK) {
        sqlite3_bind_text(statement, 1, [field1Value UTF8String], −1, NULL);
        sqlite3_bind_text(statement, 2, [field2Value UTF8String], −1, NULL);
    }

Note

To bind integer values, use the sqlite3_bind_int() function.

After the preceding call, the SQL string looks like this:

INSERT OR REPLACE INTO Contacts ('email', 'name') VALUES
    ('[email protected]', 'user0')

To execute the SQL statement, you use the sqlite3_step() function, followed by the sqlite3_finalize() function to delete the prepared SQL statement:

if (sqlite3_step(statement) != SQLITE_DONE)
        NSAssert(0, @"Error updating table.");

    sqlite3_finalize(statement);

Note

Note that in the previous section, you used the sqlite3_exec() function to execute SQL statements. In this example, you actually use a combination of sqlite3_prepare(), sqlite3_step(), and sqlite3_finalize() functions to do the same thing. In fact, the sqlite3_exec() function is actually a wrapper for these three functions. For nonquery SQL statements (such as for creating tables, inserting rows, and so on), it is always better to use the sqlite3_exec() function.

Retrieving Records

Now that the records have been successfully inserted into the table, it is time to get them out. This is a good way to ensure that they really have been saved. The following Try It Out shows you how to retrieve your records.

SUMMARY

This chapter provides a whirlwind introduction to the SQLite3 database used in the iPhone. With SQLite3, you can now store all your structured data in an efficient manner and perform complex aggregations on your data.

EXERCISES

  1. Explain the difference between the sqlite3_exec() function and the other three functions: sqlite3_prepare(), sqlite3_step(), and sqlite3_finalize().

  2. How do you obtain a C-style string from an NSString object?

  3. Write the code segment to retrieve a set of rows from a table.

  • WHAT YOU HAVE LEARNED IN THIS CHAPTER

TOPIC

KEY CONCEPTS

Use a SQLite3 database in your application

Need to add a reference to the libsqlite3.dylib to your project.

Obtain a C string from a NSString object

Use the UTF8String method of the NSString class.

Create and open a SQLite3 database

Use the sqlite3_open() C function.

Execute a SQL query

Use the sqlite3_exec() C function.

Close a database connection

Use the sqlite3_close() C function.

Use bind variables

Create a sqlite3_stmt object.

Use the sqlite3_prepare_v2() C function to prepare the statement.

Use the sqlite3_bind_text() (or sqlite3_bind_int(), and so on) C function to insert the values into the statement.

Use the sqlite3_step() C function to execute the statement.

Use the sqlite3_finalize() C function to delete the statement from memory.

Retrieve records

Use the sqlite3_step() C function to retrieve each individual row.

Retrieve columns from a row

Use the sqlite3_column_text() (or sqlite3_column_int(), and so on) C function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.18.65