iOS database Operation flow

Source: Internet
Author: User

The latest version of SQLite is 3.0 and should be imported before using Libsqlite3.0.dylib

1. Import process

    

   

Process of manipulating databases in 2.iOS

Open Database

Preparing the SQL database

Execute SQL database

End of statement

Close the database

3.SQLite () uses C's function interface

    

4. Create a Database

  

-(void) createsql{nsfilemanager *manager = [Nsfilemanager Defaultmanager];        if ([manager Fileexistsatpath:kdatabasefilepath]) {NSLog (@ "database does not exist");    Return    } NSLog (@ "%@", Kdatabasefilepath);            [Manager Createfileatpath:kdatabasefilepath Contents:nil Attributes:nil];    Sqlite3 *sql =null;        int opendbresult = Sqlite3_open ([Kdatabasefilepath utf8string], &sql);    if (OPENDBRESULT==SQLITE_OK) {NSLog (@ "Database open successfully");                } else{NSLog (@ "open unsuccessful");        [Manager Removeitematpath:kdatabasefilepath Error:nil];    Return } nsstring *sqlstring = @ "CREATE TABLE user (id integer PRIMARY KEY autoincrement, username text not NULL uniqu                        E, password text not NULL); ";    char *errmsg = NULL;        int exeresult = sqlite3_exec (sql, [sqlString utf8string], NULL, NULL, &ERRMSG);            if (Exeresult = = SQLITE_OK) {NSLog (@ "table was created successfully");      } else{NSLog (@ "failed");  Sqlite3_close (SQL);        [Manager Removeitematpath:kdatabasefilepath Error:nil];    Return                } sqlite3_close (SQL); }
5. Database Insert Database

   

-(void) createsql{nsfilemanager *manager = [Nsfilemanager Defaultmanager];        if ([manager Fileexistsatpath:kdatabasefilepath]) {NSLog (@ "database does not exist");    Return    } NSLog (@ "%@", Kdatabasefilepath);            [Manager Createfileatpath:kdatabasefilepath Contents:nil Attributes:nil];    Sqlite3 *sql =null;        int opendbresult = Sqlite3_open ([Kdatabasefilepath utf8string], &sql);    if (OPENDBRESULT==SQLITE_OK) {NSLog (@ "Database open successfully");                } else{NSLog (@ "open unsuccessful");        [Manager Removeitematpath:kdatabasefilepath Error:nil];    Return } nsstring *sqlstring = @ "CREATE TABLE user (id integer PRIMARY KEY autoincrement, username text not NULL uniqu                        E, password text not NULL); ";    char *errmsg = NULL;        int exeresult = sqlite3_exec (sql, [sqlString utf8string], NULL, NULL, &ERRMSG);            if (Exeresult = = SQLITE_OK) {NSLog (@ "table was created successfully");      } else{NSLog (@ "failed");  Sqlite3_close (SQL);        [Manager Removeitematpath:kdatabasefilepath Error:nil];    Return                } sqlite3_close (SQL); }

  

iOS database Operation flow

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.