The following is an operation on the sqlite3 database. The first step is to import the class, which is the most basic, but do not forget. I handled the operations on the database separately. I looked comfortable and understood that it would take a long time to call the database directly when necessary. I can also have a proxy, but I have never studied it, let's take a good look at this in the future. Let's summarize this for your convenience. If you forget to look for it, you don't have to forget it soon!
# Import </usr/include/sqlite3.h> # define kfilename @ "data. sqlite3 "1. open Database Operation-(void) opendb {nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); nsstring * documentsdirectory = [paths objectatindex: 0]; nsstring * datafilepath = [documentsdirectory stringbyappendingpathcomponent: kfilename]; If (sqlite3_open ([datafilepath utf8string], & database )! = Sqlite_ OK) {sqlite3_close (database) ;}} 2. create a table, structure, type-(void) initdb {char * errormsg; nsstring * SQL = @ "CREATE TABLE if not exists user (user_id integer primary key, username text, password text); "; if (sqlite3_exec (Database, [SQL utf8string], null, null, & errormsg )! = Sqlite_ OK) {sqlite3_close (database) ;}} 3. execute the add method-(void) insertusername :( nsstring *) username insertpassword :( nsstring *) password {[self opendb]; // call to open the database [self initdb]; // call the create table char * errormsg; char * update = "insert or replace into user (username, password) values (?,?); "; // Add statement nsstring * SQL = [nsstring stringwithformat: @" select username, password from user "]; // query statement SQL = @" delete from user ;"; // Delete the statement sqlite3_stmt * Statement; If (statement (Database, update,-1, & statement, nil) = sqlite_ OK) {sqlite3_bind_text (statement, 1, [username utf8string], -1, sqlite_transient); sqlite3_bind_text (statement, 2, [Password utf8string],-1, sqlite_transient);} If (sqlite3_s TEP (statement )! = Sqlite_done) nsassert1 (0, @ "error updating table: % s", errormsg); sqlite3_finalize (statement); sqlite3_close (database );}