This article reprinted to http://www.cnblogs.com/cokecoffe/archive/2012/05/31/2537105.html
Looking at the foreign site of the tutorial, wrote a small example, a contact program, including (name, address, telephone) three items, through two buttons, you can save information or query the database already have information.
The UI doesn't talk, it's easier. Paste the key code, the specific words or go to see the source code (is trying to pass, I git a bit of a problem).
Git:https://github.com/cokecoffe/ios-demo/tree/master/sqlite%e7%9a%84%e5%9f%ba%e6%9c%ac%e4%bd%bf%e7%94%a8
1/* Create a database from the path and create a table contact (ID nametext addresstext phonetext) */2 3-(void) Viewdidload 4 {5 [Super Viewdidloa D]; 6/additional setup after loading the view, typically from a nib. 7 8 NSString *docsdir; 9 Nsarray *dirpaths; Ten//Get the documents directory dirpaths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, N Suserdomainmask, YES); Docsdir = [Dirpaths objectatindex:0]; The path to the database file DatabasePath = [[NSString alloc] initwithstring: [Docsdir str Ingbyappendingpathcomponent: @ "contacts.db"]; Nsfilemanager *filemgr = [Nsfilemanager Defaultmanager]; if ([filemgr fileexistsatpath:databasepath] = = NO) ({#) const char *dbpath = [DatabasePath U Tf8string]; if (Sqlite3_open (DBPath, &contactdb) ==sqlite_ok), {+ char *errmsg; Onst char *sql_stmt = "CREATE TABLE IFNot EXISTS CONTACTS (ID INTEGER PRIMARY KEY autoincrement, NAME TEXT, ADDRESS text,phone TEXT) "; if (Sqlite3_exec (CONTACTDB, sql_stmt, NULL, NULL, &ERRMSG)!=SQLITE_OK) 29 {30 Status.text = @ "failed to create TABLE \ n"; 36} 37 (= "Failed to create/Open database") } 38 39} 40 41/* Save data to database only, when the save button is pressed */-(Ibaction) Savetodatabase: (ID) Sender sqlite3_stmt *sta Tement; *dbpath const CHAR = [DatabasePath utf8string]; if (Sqlite3_open (DBPath, &contactdb) ==sqlite_ok) {NSString *insertsql = [NSString stringwit hformat:@ "INSERT into CONTACTS (name,address,phone) VALUES (\"%@\ ", \"%@\ ", \"%@\ ")", Name.text,address.text, Phone.text]; N-const char *insert_stmt = [Insertsql utf8string]; SQLITE3_PREPARE_V2 (Contactdb, insert_stmt,-1, &statement, NULL); if (sqlite3_step (statement) ==sqlite_done) {Status.text = @ "stored in database"; Name.text = @ ""; Address.text = @ ""; Phone.text = @ ""; "Save Failed" by "Status.text" (s). Tatement); Sqlite3_close (CONTACTDB); 65} 66} 67 68/* Query data based on the name entered */-(Ibaction) Searchfromdatabase: (ID) Sender {*dbpath = [datab Asepath utf8string]; Sqlite3_stmt *statement; if (Sqlite3_open (DBPath, &contactdb) = = SQLITE_OK) (= NSString *querysql = [NSString s tringwithformat:@ "Select Address,phone from Contacts where name=\"%@\ "", Name.text]; *query_stmt const CHAR = [Querysql utf8string]; if (Sqlite3_prepare_v2 (Contactdb, query_stmt,-1, &statement, NULL) = = Sqlite_ok) 79 {80 if (sqlite3_step (statement) = = Sqlite_row) Bayi {nsstring *addressfield = [NSString allo C] InItwithutf8string: (const char *) Sqlite3_column_text (statement, 0)]; Address.text = Addressfield; NSString *phonefield = [[NSString alloc] initwithutf8string: (const char *) Sqlite3_c Olumn_text (statement, 1)]; Phone.text = Phonefield; Status.text = @ "results found"; [Addressfield release]; [Phonefield release]; "Status.text" = "no results found"; 94 Address.text = @ ""; 9 5 Phone.text = @ ""; Sqlite3_finalize (statement); 98} sqlite3_close (CONTACTDB); 101}102}
Easy use of SQLite under iOS