The basic operation of adding and deleting the database of IOS database FMDN

Source: Internet
Author: User

Fmdb (https://github.com/ccgus/fmdb) Words do not say the first to add this library, in will add in, OK ready to work on the code.

#import "DDViewController.h"

#import "FMDB.h"


@interface ddviewcontroller ()

{

fmdatabase *db;

}


@end


@implementation Ddviewcontroller

/**

* to have a location to store the database iOS document Path,document is a writable folder in iOS and to create a list of databases

*/

-(void) viewdidload

{

[superviewdidload];

db = [Fmdatabase new];

nsarray *paths =nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, YES);

nsstring *documentdirectory = [Paths objectatindex:0];

//dbpath: The database path, in Document .

nsstring *dbpath = [documentdirectory stringbyappendingpathcomponent:@ "test.db"];

// Create A DB instance here is the following: if a "test.db" file does not exist in the path , SQLite will automatically create " Test.db "

db= [fmdatabasedatabasewithpath:d Bpath];

if (! [db open]) {

NSLog(@ "Could not open db.") );

return ;

}

// set up cache for database to improve query efficiency

[dbsetshouldcachestatements:YES];

1. Create a table the name of the table to be User

[self creattable];

//2. adding data to a table

[selfinsertdata:@ " straw hat " age: +];

//3. View Results

[self ResultSet];


}

// Create a table

-(void) creattable

{

// when creating a table, it's best to judge your work before you do it.

if (! db) {

NSLog (@ " database does not exist ");

}

if (! [db open]) {

[dbopen];

}

// to determine if there is a table with this name as User if not created

if (! [db tableexists:@ "User"]) {

// Create a table named User with two fields of type string name,age of type integer

[dbexecuteupdate:@ "CREATE TABLE User (Name text,age integer)"];

[Self insertdata:@ " straw Hat " age:24];


}

}

// Insert Data

-(void) InsertData: (nsstring *) name Age: (int.) Age

{

// Insert data using type text in OC corresponding to integer type nsstring integer corresponding to int

[dbexecuteupdate:@ "INSERT into User (name,age) VALUES (?,?)" , name,[nsnumbernumberwithint: Age]];


}

// Query Results

-(void) ResultSet

{

// Returns the result of the first satisfying condition in the database

nsstring *aa=[dbstringforquery:@ "Select Name from User WHERE age =? " ,@ "+"];

NSLog(@ "AA is%@", aa);

fmresultset *rs=[dbexecuteQuery:@ "SELECT * FROM User "];

Rs=[db executequery:@ "SELECT * from User WHERE age =?", @ "23"];

while ([Rs next]) {

NSLog(@ "%@%@", [rsstringforcolumn:@ "Name"],[ RSstringforcolumn:@ "age"]);

}

[Rsclose];


}

// Modify table Contents

-(void) Modify

{

[dbexecuteupdate:@ "UPDATE User SET Name =? WHERE Name =? @" John Doe ",@ " Zhang San "];

}

-(void) didreceivememorywarning


{

[superdidreceivememorywarning];

//Dispose of any resources, can be recreated.

}


@end


IOS Database Fmdn database additions and deletions to change the basic operation

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.