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