Access the local database sqlite3 on iPhone/IOS

Source: Internet
Author: User

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:
    1. First, obtain the address of the SQLite 3 database file on the iPhone.
    2. Open the database file of SQLite 3
    3. Define SQL
    4. Parameters required for SQL Execution
    5. Execute the SQL file and obtain the result.
    6. Release resources
    7. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.