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 createdNSString * Path=nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) [0]; Path=[path stringbyappendingpathcomponent:@"New.sqlite"]; NSLog (@"%@", path); //2> Open the current link if it exists and create if it does not exist    if(Sqlite_ok==sqlite3_open (path. Utf8string, &SQLite)) {NSLog (@"Database created successfully! "); }Else{NSLog (@"Database creation failed! "); }    }
2. Operation Database
/** * Create a table*/- (void) _createtable{NSString*create=@"CREATE table if not exists T_person (ID integer primary key autoincrement,name text,age Integer,tel text)"; [Self _execsql:create andtip:@"CREATE TABLE Actions"]; }/** * Insert Data operation * * @param name * @param age * @param Tel*/- (void) _insertname: (NSString *) name Andage: (int) 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 the SQL * @param tip to perform the action title*/- (void) _execsql: (NSString *) SQL Andtip: (NSString *) tip{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> Defining SQL statementsNSString * sql=@"Select Id,name,age,tel from T_person"; Sqlite3_stmt* stmt=NULL; //2> Checking the correctness of the grammar    if(SQLITE_OK==SQLITE3_PREPARE_V2 (SQLITE, SQL. Utf8string,-1, &stmt, NULL)) {            //3> Loop result set fetching data         while(Sqlite3_step (stmt) = =Sqlite_row) {            //4> Note: Take out the data can be encapsulated in the collection of alternate            intId=sqlite3_column_int (stmt,0); ConstUnsignedChar*name=sqlite3_column_text (stmt,1); intAge=sqlite3_column_int (stmt,2); ConstUnsignedChar*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 SQLite uses for iOS development

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.