| 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 createdNSString * Path=nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) [0]; Path=[path stringbyappendingpathcomponent:@"New.sqlite"]; NSLog (@"%@", path); //2> Open the current link if it exists and create if it does not exist if(Sqlite_ok==sqlite3_open (path. Utf8string, &SQLite)) {NSLog (@"Database created successfully! "); }Else{NSLog (@"Database creation failed! "); } }
2. Operation Database
/** * Create a table*/- (void) _createtable{NSString*create=@"CREATE table if not exists T_person (ID integer primary key autoincrement,name text,age Integer,tel text)"; [Self _execsql:create andtip:@"CREATE TABLE Actions"]; }/** * Insert Data operation * * @param name * @param age * @param Tel*/- (void) _insertname: (NSString *) name Andage: (int) 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 the SQL * @param tip to perform the action title*/- (void) _execsql: (NSString *) SQL Andtip: (NSString *) tip{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> Defining SQL statementsNSString * sql=@"Select Id,name,age,tel from T_person"; Sqlite3_stmt* stmt=NULL; //2> Checking the correctness of the grammar if(SQLITE_OK==SQLITE3_PREPARE_V2 (SQLITE, SQL. Utf8string,-1, &stmt, NULL)) { //3> Loop result set fetching data while(Sqlite3_step (stmt) = =Sqlite_row) { //4> Note: Take out the data can be encapsulated in the collection of alternate intId=sqlite3_column_int (stmt,0); ConstUnsignedChar*name=sqlite3_column_text (stmt,1); intAge=sqlite3_column_int (stmt,2); ConstUnsignedChar*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 SQLite uses for iOS development