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