1 //2 //HMVIEWCONTROLLER.M3 //Application of 02-sqlite4 //5 //Created by Apple on 14-7-24.6 //Copyright (c) 2014 Heima. All rights reserved.7 //8 9 #import "HMViewController.h"Ten #import<sqlite3.h> One A @interfaceHmviewcontroller () --(ibaction) insert; --(ibaction) update; the-(ibaction) Delete; --(ibaction) Select; - //db is the symbol of the database, if you want to do crud, you have to operate DB this instance -@property (nonatomic, assign) Sqlite3 *db; + @end - + @implementationHmviewcontroller A at- (void) Viewdidload - { - [Super Viewdidload]; - - //get the path to the database file -NSString *doc =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; inNSString *filename = [Doc stringbyappendingpathcomponent:@"Students.sqlite"]; - //Convert OC String to C language string to Const Char*cfilename =filename. utf8string; + //1. Open the database (if the database file does not exist, the Sqlite3_open function automatically creates the database file) - intresult = Sqlite3_open (cFileName, &_db); the if(Result = = SQLITE_OK) {//Open Success *NSLog (@"successfully opened database"); $ Panax Notoginseng //2. Create a watch - Const Char*sql ="CREATE TABLE IF not EXISTS t_student (ID integer PRIMARY KEY autoincrement, name text NOT NULL, age integer NOT NULL); "; the Char*erromsg =NULL; +result = Sqlite3_exec (self.db, SQL, NULL, NULL, &erromsg); A if(Result = =SQLITE_OK) { theNSLog (@"successful creation of a table"); +}Else { - //printf ("Genesis failure--%s--%s-%d", Erromsg, __file__, __line__); $NSLog (@"Create a table failure--%s--%@-%d", erromsg, [NSString stringwithutf8string:__file__], __line__); $ } -}Else { -NSLog (@"failed to open database"); the } - }Wuyi the-(ibaction) Insert { - for(inti =0; i< -; i++) { Wu //1. Splicing SQL statements -NSString *name = [NSString stringWithFormat:@"jack-%d", Arc4random_uniform ( -)]; About intAge = Arc4random_uniform ( -) + -; $NSString *sql = [NSString stringWithFormat:@"INSERT into T_student (name, age) VALUES ('%@ ',%d);", name, age]; - - //2. Execute SQL statements - Char*erromsg =NULL; ASqlite3_exec (self.db, SQL. Utf8string, NULL, NULL, &erromsg); + if(erromsg) { theNSLog (@"failed to insert data--%s", erromsg); -}Else { $NSLog (@"successfully inserting data"); the } the } the } the --(ibaction) Update { in //different than insert only SQL statement the } the About-(ibaction) Delete { the //different than insert only SQL statement the } the +-(ibaction) Select { - Const Char*sql ="SELECT ID, name, age from T_student WHERE age <=;"; the //Preparation before queryingBayi //-1 means the system automatically calculates the length of the SQL statement the //sqlite3_stmt: Used to fetch data theSqlite3_stmt *stmt =NULL; - if(SQLITE3_PREPARE_V2 (self.db, SQL,-1, &stmt, NULL) = = SQLITE_OK) {//no problem with SQL statements -NSLog (@"no problem with query statements"); the the //each time the Sqlite3_step function is adjusted, stmt points to the next record the while(Sqlite3_step (stmt) = = Sqlite_row) {//Find a record the //Remove Data - the //Remove the value of the No. 0 column field (the value of type int) the intID = Sqlite3_column_int (stmt,0); the 94 //Remove the value of the 1th column field (the value of Tex type) the ConstUnsignedChar*name = Sqlite3_column_text (stmt,1); the the //Remove the value of the 2nd column field (the value of type int)98 intAge = Sqlite3_column_int (stmt,2); About -NSLog (@"%d%s%d", ID, name, age);101 }102}Else {103NSLog (@"problem with query statement");104 } the }106 @end