Sqlite3 database operations (iPhone Development)

Source: Internet
Author: User

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 );}

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.