Details about the use of SQLite handles in iOS

Source: Internet
Author: User

1, design ideas: For the package of SQLite, the use of defined categories to achieve the simplicity of the method, in order to achieve low coupling effect

1 Controller code:2 #import "ViewController.h"3 #import "DatabaseHelper.h"4 5 @interfaceViewcontroller ()6 {7Sqlite3 *db;8 }9 @endTen  One @implementationViewcontroller A  --(void) Loadview { -  the [Super Loadview]; -Self.view.backgroundColor =[Uicolor Orangecolor]; - } -- (void) Viewdidload { + [Super Viewdidload]; -     //PART1: This section is an implemented help category +     //PART1.1 A[Databasehelper Opendatabasewithname:@"Personinfo.sqlite"withsqlite:db]; at     //PART1.2 -DB =[databasehelper Opendatabasewithname:@"Personinfo.sqlite"WITHSQLITE:DB];
-NSLog (@"isopenorcreat =%@"Db?@"sucess":@"Failure");
26
- //PART2: Used to determine whether the database is open
- if(db) {
in
- }
to
+ //PART3: Used to determine whether the database is open
-[Databasehelper Execsql:@"CREATE TABLE IF not EXISTS PERSONINFO (ID integer PRIMARY KEY autoincrement, Name text, age INTEGER, address TEXT) "WITHSQLITE:DB];

1 Operation Class Code:2 #import "DatabaseHelper.h"3 4 @implementationDatabasehelper5+ (Sqlite3 *) Opendatabasewithname: (NSString *) name Withsqlite: (Sqlite3 *) sqlite3{6     //PART5:7     //1. Get Documents File path8NSString *documentspath =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];9     //2, stitching to establish the database file pathTenNSString *databasepath =[Documentspath stringbyappendingpathcomponent:name]; One     //Print ANSLog (@"DatabasePath =%@", DatabasePath); -     //determine if it is turned on (turn off the database and log prompt when failure is turned on) -     if(Sqlite3_open ([DatabasePath utf8string], &sqlite3)! =SQLITE_OK) { the Sqlite3_close (sqlite3); -NSLog (@"Database open Failed"); -         returnNil; -     } +  - //PART4: + //Char *err; A //nsstring *sql = @ "CREATE TABLE IF not EXISTS PERSONINFO (ID INTEGER PRIMARY KEY autoincrement, name TEXT, age INT EGER, address TEXT) "; at //if (sqlite3_exec (sqlite3, [SQL Utf8string], NULL, NULL, &ERR)! = SQLITE_OK) { - //Sqlite3_close (sqlite3); - //NSLog (@ "Database operation data failed!"); - //NSLog (@ "%s", err); - //    } -  in     returnsqlite3; - } to  ++ (BOOL) Execsql: (NSString *) SQL Withsqlite: (sqlite3 *) sqlite3{ -     Char*err; the     //sqlite3_exec (), execute non-query SQL statement *     if(Sqlite3_exec (Sqlite3, [SQL Utf8string], NULL, NULL, &ERR)! =SQLITE_OK) { $ Sqlite3_close (sqlite3);Panax NotoginsengNSLog (@"database operation data failed!"); -         returnNO; the     } +     returnYES; A  the}

Questions:1, when the note off PART1.1 database can be written 2, when the PART1.1, PART4 after the database cannot be written 3, when the PART1.2, PART4 data Libraries can be written Conclusion:1. The existence of a handle must depend on the context, and the operation of the database must depend on the contents of the handle. 2, only the handle before and after the operation of the database can be achieved.

Details about the use of SQLite handles in iOS

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.