Data SQLite uses for iOS development

Source: Internet
Author: User

First, the introduction of the toolkit

Introduced the toolkit Libsqlite3.dylib, the Toolkit for the C language toolkit.

Second, the Code operation database
1. Create and link a database
-(void) _connectdb{     //1> Gets the sandbox path as the initialization path when the database was created    NSString * Path=nssearchpathfordirectoriesindomains ( NSDocumentDirectory, Nsuserdomainmask, YES) [0];    Path=[path stringbyappendingpathcomponent:@ "New.sqlite"];    NSLog (@ "%@", path);    2> opens the current link if it exists, and if it does not, creates if    (Sqlite_ok==sqlite3_open (path). Utf8string, &sqlite) {        NSLog (@ "database created successfully!) ");    } else {        NSLog (@ "Database creation failed! ");    }    }
2. Operation Database
 
/** * CREATE table  */-(void) _createtable{nsstring *[email protected] "CREATE table if not exists T_person (ID integer PRIMARY key    Autoincrement,name text,age Integer,tel text) ";    [Self _execsql:create andtip:@ "CREATE TABLE action"]; }/** * Insert Data operation * * @param name name * @param Age * @param tel phone */-(void) _insertname: (NSString *) name Andage: (In  T) Age Andtel: (NSString *) tel{NSString * insert=[nsstring stringwithformat:@ "INSERT into T_person (Name,age,tel)    VALUES ('%@ ',%d, '%@ ') ", Name,age,tel];    [Self _execsql:insert andtip:@ "insert operation"]; }/** * Perform database operations * * @param SQL to execute SQL * @param tip to perform the action title */-(void) _execsql: (NSString *) SQL Andtip: (NSString *) ti    p{char * result; if (sqlite_ok==sqlite3_exec (SQLITE, SQL. Utf8string, NULL, NULL, &result)) {NSLog (@ "%@ success!)    ", tip); }else{NSLog (@ "%@ failed!    ", tip); }}
3. Querying the database
/** *  read data */-(void) _readdata{    //1> Define SQL statement    NSString * [email protected] "Select Id,name,age,tel from T_ Person ";        SQLITE3_STMT * STMT=NULL;    2> Check the correctness of the syntax    if (SQLITE_OK==SQLITE3_PREPARE_V2 (SQLITE, SQL). Utf8string,-1, &stmt, NULL) {            //3> loop result set fetch data while        (Sqlite3_step (stmt) ==sqlite_row) {            //4> Note: The extracted data can be encapsulated into the set of alternate            int id=sqlite3_column_int (stmt,0);            Const unsigned char *name=sqlite3_column_text (stmt, 1);            int Age=sqlite3_column_int (stmt, 2);            Const unsigned char *tel=sqlite3_column_text (stmt, 3);            NSString * names=[nsstring stringwithutf8string: (const char *) name];            NSString * tels=[nsstring stringwithutf8string: (const char *) Tel];            NSLog (@ "%d,%@,%d,%@", Id,names,age,tels);}}        

Data for iOS development SQLite use

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.