IOS sqlite3 Basic Application

Source: Internet
Author: User

In iOS, persistence makes good use of several methods. We have already introduced two methods: simple file writing and serialization and file writing, now we will introduce the basic application of the embedded database sqlite3 in IOS. Before using sqlite3, you must add the libsqlite3.dylib class library to your project.

//////////////////////////////////////// //////////////////////////////////////// //////////

-(Nsstring *) datafilepath {
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0];
Return [documentsdirectory stringbyappendingpathcomponent: kfilename];
}

Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0]; // first obtain the path of the document folder in the application sandbox.

Return [documentsdirectory stringbyappendingpathcomponent: kfilename] // return the path of the specified file

//////////////////////////////////////

Open Database

Sqlite3 * database;
If (sqlite3_open ([filepath utf8string], & database )){
Sqlite3_close (database );
Nsassert (0, @ "failed to open database ");
}

///////////////////

Create a database

Char * errormsg;
 
Nsstring * createsql = @ "create table if not exists fields (row integer primary key, field_data text );";
If (sqlite3_exec (Database, [createsql utf8string], null, null, & errormsg )! = Sqlite_ OK ){
Sqlite3_close (database );
Nsassert1 (0, @ "error creating table: % s", errormsg );
}
/////////////////////////

Query
Nsstring * query = @ "select row, field_data from fields order by row ";
Sqlite3_stmt * statement;
If (sqlite3_prepare_v2 (Database, [query 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, 1 );

// Nsstring * fieldname = [[nsstring alloc] initwithformat: @ "field & D", row];
// Nsstring * fieldvalue = [[nsstring alloc] initwithuf8string: rowdata];

// Uitextfield * field = [self valueforkey: fieldname];
// Field. Text = fieldvalue;
// [Fieldname release];
// [Fieldvalue release];
}
Sqlite3_finalize (statement );
}
Sqlite3_close (database );

//////////////////////////////////////// ////////////

Insert update
Sqlite3 * database;
If (sqlite3_open ([[self datafilepath] utf8string], & database )){
Sqlite3_close (database );
Nsassert (0, @ "failed to open database ");
}
 
For (INT I = 1; I <= 4; I ++ ){
Nsstring * fieldname = [[nsstring alloc] initwithformat: @ "field % d", I];
Uitextfield * field = [self valueforkey: fieldname];
[Fieldname release];

Char * errormsg;
Char * update = "insert or replace into fields (row, field_data) values (?,?); "; // The value inserted here can be replaced with nsstring, but the best practice is to use binding. This is the best choice if special characters are encountered.

Sqlite3_stmt * stmt;
If (sqlite3_prepare_v2 (Database, update,-1, & stmt, nil) = sqlite_ OK ){
Sqlite3_bind_int (stmt, 1, I );
Sqlite3_bind_text (stmt, 2, [[field text] utf8string],-1, null );
}
If (sqlite3_step (stmt )! = Sqlite_done ){
Nsassert (0, @ "error updating table: % s", errormsg );
}
Sqlite3_finalize (stmt );
}
Sqlite3_close (database );

This is the most basic application method of sqlite3 in IOS. For more information, see this document.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.