| First, the introduction of the toolkit |
Introduced the toolkit Libsqlite3.dylib, the Toolkit for the C language toolkit.
| Second, the Code operation database |
1. Create and link a database
-(void) _connectdb{ //1> Gets the sandbox path as the initialization path when the database was created NSString * Path=nssearchpathfordirectoriesindomains ( NSDocumentDirectory, Nsuserdomainmask, YES) [0]; Path=[path stringbyappendingpathcomponent:@ "New.sqlite"]; NSLog (@ "%@", path); 2> opens the current link if it exists, and if it does not, creates if (Sqlite_ok==sqlite3_open (path). Utf8string, &sqlite) { NSLog (@ "database created successfully!) "); } else { NSLog (@ "Database creation failed! "); } }
2. Operation Database
/** * CREATE table */-(void) _createtable{nsstring *[email protected] "CREATE table if not exists T_person (ID integer PRIMARY key Autoincrement,name text,age Integer,tel text) "; [Self _execsql:create andtip:@ "CREATE TABLE action"]; }/** * Insert Data operation * * @param name name * @param Age * @param tel phone */-(void) _insertname: (NSString *) name Andage: (In T) Age Andtel: (NSString *) tel{NSString * insert=[nsstring stringwithformat:@ "INSERT into T_person (Name,age,tel) VALUES ('%@ ',%d, '%@ ') ", Name,age,tel]; [Self _execsql:insert andtip:@ "insert operation"]; }/** * Perform database operations * * @param SQL to execute SQL * @param tip to perform the action title */-(void) _execsql: (NSString *) SQL Andtip: (NSString *) ti p{char * result; if (sqlite_ok==sqlite3_exec (SQLITE, SQL. Utf8string, NULL, NULL, &result)) {NSLog (@ "%@ success!) ", tip); }else{NSLog (@ "%@ failed! ", tip); }}
3. Querying the database
/** * read data */-(void) _readdata{ //1> Define SQL statement NSString * [email protected] "Select Id,name,age,tel from T_ Person "; SQLITE3_STMT * STMT=NULL; 2> Check the correctness of the syntax if (SQLITE_OK==SQLITE3_PREPARE_V2 (SQLITE, SQL). Utf8string,-1, &stmt, NULL) { //3> loop result set fetch data while (Sqlite3_step (stmt) ==sqlite_row) { //4> Note: The extracted data can be encapsulated into the set of alternate int id=sqlite3_column_int (stmt,0); Const unsigned char *name=sqlite3_column_text (stmt, 1); int Age=sqlite3_column_int (stmt, 2); Const unsigned char *tel=sqlite3_column_text (stmt, 3); NSString * names=[nsstring stringwithutf8string: (const char *) name]; NSString * tels=[nsstring stringwithutf8string: (const char *) Tel]; NSLog (@ "%d,%@,%d,%@", Id,names,age,tels);}}
Data for iOS development SQLite use