From: http://www.jguoer.com/blog/index.php/archives/171
Before developing an applicationProgramOf course, it is necessary to store data in a regular practical database. on mobile devices, we can store data using files, databases, and other methods, in order to prevent users from using other programs for modification, I think the database method is a good method. On the iPhone, we can use SQLite for data persistence. It is also worth mentioning that Firefox is saved in the database mode, and SQLite is also used.
In iPhone development, we need to first add an SQLite library, which is supported by xcode. In frameworks on the left, select Add and then select existing frameworks, in the pop-up window, select the library libsqlite3.0.dylib of SQLite.
Then, we can use SQLite to save, query, and delete data on the iPhone.
Now we can write a SQLite helper file to facilitateCodeThe header file (sqlitehelper. h) is as follows.
# Import < Foundation / Foundation. h >
# Import"Sqlite3.h"
# DefineKfilename @ "mydatabase. SQL"
@ Interface sqlitehelper: nsobject {
Sqlite3*Database;
}
//Create a table
-(Bool) createtable;
//Insert data
-(Bool) insertmaintable :( nsstring*) Username insertpassword :( nsstring*) Password;
//Query a table
-(Bool) checkifhasuser;
@ End
The code file is as follows.
# Import " Sqlitehelper. h "
@ Implementation sqlitehelper
-(Bool) createtable
{
Nsarray*Path=Nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring*Paths=[[Path objectatindex:0] Stringbyappendingpathcomponent: kfilename];
Nsfilemanager*Filemanager=[Nsfilemanager defaultmanager];
Bool filefinded=[Filemanager fileexistsatpath: Paths];
Nslog (@"Database file path is % @", Paths );
If(Filefinded)
{
Nslog (@"Database file existed");
If(Sqlite3_open ([paths utf8string],&Database)! =Sqlite_ OK)
{
Sqlite3_close (database );
Nslog (@"Open failed");
ReturnNo;
}
}
Else
{
Nslog (@"Database file is not existed");
If(Sqlite3_open ([paths utf8string],&Database)! =Sqlite_ OK)
{
Sqlite3_close (database );
Nslog (@"Open failed");
ReturnNo;
}
}
Char *Errormsg;
Nsstring*Createsql= @"Create Table if not exists fields (userid integer primary key, username text, Password text)";
If(Sqlite3_exec (Database, [createsql utf8string], null, null,&Errormsg)! =Sqlite_ OK)
{
Sqlite3_close (database );
Nslog (@"Open failed or init filed");
ReturnNo;
}
ReturnYes;
}
-(Bool) insertmaintable :( nsstring*) Username insertpassword :( nsstring*) Password
{
Char *Errormsg;
Nsstring*Createsql= @"Create Table if not exists fields (userid integer primary key, username text, Password text)";
If(Sqlite3_exec (Database, [createsql utf8string], null, null,&Errormsg)! =Sqlite_ OK)
{
Sqlite3_close (database );
Nslog (@"Open failed or init filed");
ReturnNo;
}
Nsstring*Insertdata=[[Nsstring alloc] initwithformat:@"Insert or replace into fields (userid, username, password) values (% d, '% @', '% @')",0, Username, password];
If(Sqlite3_exec (Database, [insertdata utf8string], null, null,&Errormsg)! =Sqlite_ OK)
{
Sqlite3_close (database );
Nslog (@"Open failed or failed to insert");
ReturnNo;
}
ReturnYes;
}
-(Bool) checkifhasuser
{
Nsstring*Getusercountsql= @"Select * from fields";
Sqlite3_stmt*Statement;
Nslog (@"Checkifhasuser");
If (Sqlite3_prepare_v2 (Database, [getusercountsql utf8string], - 1 , & Statement, nil) = Sqlite_ OK)
{
// While (sqlite3_step (statement) = sqlite_row)
// {
// Int ROW = sqlite3_column_int (statement, 0 );
// Char * rowdata = (char *) sqlite3_column_text (statement, 2 );
// Nsstring * fieldname = [[nsstring alloc] initwithformat: @ "show % d", row];
// Nsstring * fieldvalue = [[nsstring alloc] initwithuf8string: rowdata];
//
// Nslog (@ "fieldname is: % @, fieldvalue is: % @", fieldname, fieldvalue );
// Return [[nsstring alloc] initwithformat: @ "fieldname is: % @, fieldvalue is: % @", fieldname, fieldvalue];
//
// [Fieldname release];
// [Fieldvalue release];
// }
// Sqlite3_finalize (statement );
If (Sqlite3_step (statement) = Sqlite_row)
{
Nslog ( @" Have user " );
Return Yes;
}
}
Nslog (@"No user");
ReturnNo;
}
@ End
checkifhasuser is used to check data. In this method, I comment on the obtained data, because we only check the data, so no data is required, check whether data exists. Although the above Code does not have too many comments, the Code itself is very simple and the context is very clear, so I will not write too many comments.