iOS Development-ui Learning-sqlite Database operations

Source: Internet
Author: User
Tags sqlite database

iOS Development-ui Learning-sqlite Database operations

SQLite is a lightweight database, it occupies a very low resource, in the embedded device, may only need hundreds of K of memory is enough, and it processing speed than the MySQL, PostgreSQL the two famous database is faster, Used in iOS and Android apps to perform offline caching of data, such as offline caching of news data.

Its basic operating procedures are:

1, first joined the SQLite Development Library Libsqlite3.dylib,

2. Create a new or open database,

3. Create a data table,

4. Insert Data,

5, query data and print,

6, close the database,

The following are the steps:

1. The header file required to import SQLite database:

After the import is complete:

2, the programming code:

1 //2 //VIEWCONTROLLER.M3 //SQLite manually creating databases and tables4 //5 //Created by Mac on 16/4/12.6 //copyright©2016 year MZW. All rights reserved.7 //8 9 #import "ViewController.h"Ten #import "FMDatabase.h" One @interfaceViewcontroller () { AFmdatabase *MyDB; -NSString *IDD; -NSString *Age ; theNSString *name; - } -  - @end +  - @implementationViewcontroller +  A- (void) Viewdidload { at [Super Viewdidload]; -      -      - //Create a database path -NSString *path =[nshomedirectory () stringbyappendingstring:@"/documents/mydb.sqlite"]; -      in //build the database based on the path you created -MyDB =[[Fmdatabase Alloc]initwithpath:path]; to      + //Create a string that creates a table mytable in the database -NSString *tablecreate =@"CREATE table if not EXISTS mytable (ID txt primary key,name text,age text)"; the      * //Open Database $     if([MyDB Open]) {Panax Notoginseng          - //Create a new table with a created string mytable theBOOL Createok =[MyDB executeupdate:tablecreate]; +          A      the         if(createok) { + //If you create a result, print a prompt to create a success -NSLog (@"Database Creation succeeded"); $              $ //add a record to the created mytable using the INSERT INTO statement in the SQL statement -BOOL Insertok = [MyDB executeupdate:@"INSERT INTO MyTable (id,age,name) VALUES (?,?,?)",@"1",@" at",@"Huangweiqiang"]; -              the //add multiple records to the created mytable using the INSERT INTO statement in the SQL statement -BOOL Insertsok = [MyDB executeupdate:@"INSERT INTO MyTable (id,age,name) Select ' 2 ', ' + ', ' Mazhongwei ' union ALL SELECT ' 3 ', ' + ', ' Xiangyiyao ' union ALL Select ' 4 ', ' Zhanglong ', '"];Wuyi              the //Delete a qualified statement by using the DELETE from statement in the SQL statement -[MyDB executeupdate:@"Delete from mytable where id = ' 4 '"]; Wu              - //update the records in the table using the update in the SQL statement About[MyDB executeupdate:@"Update mytable Set age = ' 100000 ' WHERE id = ' 3 '"]; $              - //use the query statement in the SQL statement to query the eligible statement, return the result as a collection, use the next function to traverse the result and assign a value to idd,age,name three variables -Fmresultset *rsset = [MyDB executeQuery:@"SELECT * FROM mytable where id = ' 3 '"]; -              while([Rsset next]) { AIDD = [Rsset stringforcolumn:@"ID"]; +Age = [Rsset stringforcolumn:@" Age"]; thename = [Rsset stringforcolumn:@"name"]; -             } $              the //Close the database when the operation is complete the [MyDB Close]; the              the //Print Query Results -NSLog (@"%@_%@_%@", idd,age,name); in           the}Else{ the //If creation fails, return failure prompt AboutNSLog (@"Database creation failed"); the         } the     } the      + } -  the Bayi  the @end

iOS Development-ui Learning-sqlite Database operations

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.