Previously: we learn about SQLite and standard SQLite commands : iOS : SQLite engine
SQLite Database Engine is a software library that provides a Relational Database Management System similar to MySQL or Oracle. It is ‘Server-Less’, ‘Self-Contained’ with ‘Zero Configuration’ and uses ‘Transactional SQL’. These features make it a perfect candidate for mobile devices. In IOS SQLite is easily embedded with the application, as it is simply a file format. It replaces the ‘plist’ implementation for storing and handling the complex data in IOS.
How to use SQLite in iOS?
The ‘libsqlite3.dylib’ library in framework section is required to be added in an application, which is a C++ wrapper that provides the API to the SQLite commands. To use the SQLite commands, it is mandatory to import the ‘sqlite3’ header file.
IOS SQLite Create Database and Table
Creating SQLite Database is similar to creating a text file. In iOS, a path of document directory is used to save the database file in application because only that folder is accessible by iOS application for user data.
To execute any query in SQLite database, first, a database is opened for querying, for this purpose a ‘sqlite3_open’ method is used with the path of a database and ‘sqlite3’ object as arguments. On successful execution, it returns the constant ‘SQLITE_OK’ equals to zero.
Finally, to create a table in database an iOS SQLite API method ‘sqlite3_exec’ is used with the open sqlite3 object, a SQL query of creating the table, callback function, the first argument to the callback function and error message container as arguments. On successful execution, it returns ‘SQLITE_OK’ or in case of failed execution, SQL error and missing database an ‘SQLITE_ERROR’ equal to ‘1’.