Phone also supports accessing the local database SQLite 3. Here we will briefly introduce how to use SQLite 3 on the iPhone.
- First, you need to reference the SQLite 3 development kit in the project. The following is the directory under the iPhone SDK 3.0:/developer/platforms/iphoneos. platform/developer/sdks/iphoneos3.0.sdk/usr/lib/libsqlite3.0.dylib
- Here, you need to use commands in advance to create a database file for SQLite 3, create your own table, and so on, and then add it to the project as a resource file, and then inProgramDuring the first running, you can copy to the program's documents or other directories. There are already many basic operations on SQLite 3 on the Internet.Article.
To use SQLite 3 on an iPhone, follow these steps:
- First, obtain the address of the SQLite 3 database file on the iPhone.
- Open the database file of SQLite 3
- Define SQL
- Parameters required for SQL Execution
- Execute the SQL file and obtain the result.
- Release resources
- Shut down the SQLite 3 database.
Combined Code To demonstrate.
// First, obtain the address of the sqlite3 database file on the iPhone.
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0 ];
Nsstring * Path = [documentsdirectory stringbyappendingpathcomponent: @" Database_name " ];
// Open the sqlite3 database file
Sqlite3 * database;
Sqlite3_open ([path utf8string], & database );
// Define SQL
Sqlite3_stmt * stmt;
Const Char * SQL = " Select * From table_name where PK =? And name =? " ;
Sqlite3_prepare_v2 (Database, SQL ,- 1 , & Stmt, null );
// Specify the first int Parameter
Sqlite3_bind_int (stmt, 1 ,1 );
// Specify the second string parameter
Sqlite3_bind_text (stmt, 2 , [Title utf8string],- 1 , Sqlite_transient );
// Execute the SQL file and obtain the result.
Sqlite3_step (stmt );
// Release resources
Sqlite3_finalize (stmt );
// Shut down the sqlite3 Database
Sqlite3_close (database );
Here is just a rough introduction. For more details, please refer to Apple's official documentation.