First day of SQLite: A series of actions for local SQLite via code

Source: Internet
Author: User
Tags sqlite tmp folder

Sqlitemanager data-tier---> Includes: library (user information) TMP (temporary file [user exits without time]) document

1.Documents:

Only user-generated files, other data, and files that other programs cannot recreate, should be saved under the <application_home>/documents directory and automatically backed up via icloud.

2.Library:

Data that can be re-downloaded or regenerated should be stored under the <application_home>/library/caches directory. For example, magazines, news, database cache files used by the map app, and downloadable content should be saved to this folder.

3.tmp:

Only temporary data that is used should be saved to the <application_home>/tmp folder. Although ICloud does not back up these files, you should be careful to remove them at any time after the app has finished using the data to avoid taking up space on your device

Referencing the LIBSQLITE3.TBD resource file and the FMDB folder

Then add Sqlitemanager

In Sqlitemanager. H file inserts the singleton and bool types to update the database method and (the array type of the method to get SQL) to put the resulting SQL into the array

1+(instancetype) share;2 /**3 * Update database data4  *5 * @param SQL SQL statements6  *7 * @return return success or failure8  */9-(BOOL) Run: (NSString *) SQL;Ten /** One * Find database Data A  * - * SQL statement @param SQL lookup -  * the * @return Returns the result of a query based on the SQL statement passed in -  */ --(Nsarray *) GetData: (NSString *) SQL;

Then implement each method in the Sqlitemanager.m file

First introduce the header file #import "Fmdatabase"

Add a Fmdatabase property FmDB, implement the Singleton method, Init initialize the method and add a Creatdb method

The following steps are implemented in the Creatdb method:

1. Specify the Nshomedirectory path

2. Initialize the FmDB and specify the database path at the time of creation

3. Open database {Perform database operation, if there is person, skip, no then create}

Then implement the update database operation and find the database data operation, the code is as follows:

#import "SQLiteManager.h"#import "FMDatabase.h"@interfaceSqlitemanager () @property (nonatomic,strong) fmdatabase*Fmdb;@end@implementationSqlitemanager+(instancetype) share{StaticSqlitemanager *cm =Nil; Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{cm=[[Sqlitemanager alloc] init];    }); returncm;}-(instancetype) init{if(self =[Super Init])    {[Self creatdb]; }    returnSelf ;}-(void) creatdb{//1. Specify the nshomediretory pathNSString *path = [NSString stringWithFormat:@"%@/documents/app.db", Nshomedirectory ()]; NSLog (@"Path =%@", path); //2. Initialize the FMDB and specify the database path at the time of CreationSelf.fmdb =[[Fmdatabase alloc] initwithpath:path]; //3. Open the Database    if([Self.fmdb Open]) {//4. Perform database operations, skip if current person is present, no then createNSString *sql =@"CREATE TABLE \"person\"(\ "person_id\" INTEGER PRIMARY KEY autoincrement, \ "name\" text, \ "tel\" text, \ "age\" text)";      [Self run:sql]; }Else{NSLog (@"failed to open database!"); return; }}-(BOOL) Run: (NSString *) sql{return[Self.fmdb executeupdate:sql];}-(Nsarray *) GetData: (NSString *) sql{Fmresultset*result =[Self.fmdb Executequery:sql]; Nsmutablearray*marr = [Nsmutablearray arraywithcapacity:0];  while([result next]) {nsdictionary*dic = [result resultdictionary];//Traverse[MArr Addobject:dic]; }    return[Nsarray Arraywitharray:marr]; returnNil;}@end

Add and query operations in the Viewcontroller file, adding "Sqlitemanager" header files first

Add Sqlitemanager Property

Add Self.sqlmanager = [Sqlitemanager share] in VD method;

Implementing add methods and Query methods

1 #import "ViewController.h"2 #import "SQLiteManager.h"3 @interfaceViewcontroller ()4@property (nonatomic,strong) Sqlitemanager *Sqlmanager;5 @end6 7 @implementationViewcontroller8 9- (void) Viewdidload {Ten [Super Viewdidload]; One     //additional setup after loading the view, typically from a nib. ASelf.sqlmanager =[Sqlitemanager share]; - } - #pragmaMark adds the-(Ibaction) TAPADDBTN: (ID) Sender { -NSString *sql =@"INSERT into person (Name,age,tel) VALUES (' Mark ', ' + ', ' 119 ')"; -     if([Self.sqlmanager run:sql]) { -NSLog (@"Add Success"); +     } -     Else +     { ANSLog (@"Add failed"); at     } - [Self.sqlmanager Run:sql]; - } - #pragmaMark Query --(Ibaction) TAPSEARCHBTN: (ID) Sender { -NSString *sql =@"SELECT * from person where name = ' Mark '"; in [Self.sqlmanager Getdata:sql]; -Nsarray *result =[Self.sqlmanager Getdata:sql]; toNSLog (@"query result =%@", result); + } -  the- (void) didreceivememorywarning { * [Super didreceivememorywarning]; $     //Dispose of any resources the can be recreated.Panax Notoginseng } -  the @end
View Code

First day of SQLite: A series of actions for local SQLite via code

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.