Ios-senior19-fmdb third-party applications

Source: Internet
Author: User

Code Edition

First step: Introduce the framework and introduce the Support class library (libsqlite3.0 Add)

#import <FMDB.h>

Declaring database objects

@property (Nonatomic,strong) fmdatabase *database;

Storage path

@property (Nonatomic,strong) NSString *filepath;

############## #创建表 ######################

-(void) CreateTable {

First step: Create SQL statements

NSString *createsql = @ "CREATE table if not exists t_student (ID integer primary key autoincrement not null,name text not n Ull,age integer not null,sex text not null) ";

Step two: Find the storage path

NSString *document = [Nssearchpathfordirectoriesindomains (nsdocumentdirctory,nsuserdomainmask,yes) objectAtIndex:0 ];

NSLog (@ "document =%@", document);

Self.filepath = [Document stringbyappendingpathcomponent:@ "Student.sqlite"];

Step three: Initialize the Fmdb object with a path

Self.database = [Fmdatabase DatabaseWithPath:self.filePath];

Fourth step: (Database execution related operations) need to determine the database opened when the execution of the statement

if ([Self.database open]) {

BOOL result = [Self.database executeupdate:createsql];

if (result) {

NSLog (@ "Build table success");

}else {

NSLog (@ "Build table failed");

}

}

Fifth step: Close the database

[Self.database Close];

}

########### #添加数据 ####################

Open Database

[Self.database Open];

Step two: Make the relevant operation

Nsarray *namearray = [Nsarray arraywithobjects: @ "Mbboy" @ "Boom Sky" @ "Xiao Ming"];

for (int i = 0; I <nameArray.count; i++) {

NSString *name = [NameArray objectatindex:i];

Insert statement

NSString *insertsql = @ "INSERT into t_student (name,age,sex) VALUES (?,?,?)";

BOOL result = [Self.database executeupdate:@ "INSERT into t_student (name,age,sex) VALUES (?,?,?)", name,@ "a", @ "boy"];

if (result) {

NSLog (@ "Insert success");

}else {

NSLog (@ "Insert failed");

}

Close the database

[self.database close];

}

############# #更改数据 ######################

Open Database

[Self.database];

Change the name for Mbboy.

BOOL result = [Self.database executeupdate: @ "Update t_student set name =? WHERE name =?", @ "Menglingxu", @ "Mbboy"];

if (result) {

NSLog (@ "Change succeeded");

} else {

NSLog (@ "Change failed");

}

Close the database

[Self.database Close];

}

############# #删除数据 #######################

Open Database

[Self.database Open];

EXECUTE statement

BOOL result = [self.database executeupdate:@ "delete from t_student where name =?", @ "Menglingxu"];

if (result) {

NSLog (@ "Delete succeeded");

}else {

NSLog (@ "Delete failed");

}

[Self.database Close];

################## #查询数据 ###################

Open Database

[Self.database Open];

Query the class used for the current result Fmresultset

Fmresultset *resultset = [self.database executeQuery: @ "Select *from t_student"];

Iterate through the desired result content

while ([ResultSet next]) {

nssting *name = [ResultSet objectforcolumnname: @ "name"];

Nsinteger age = [ResultSet intforcolumn:@ ' age '];

NSString *sex = [resultSet objectforcolumnname:@ "sex"];

}

Close the database

[Self.database Close];

}

############# #插入很多学生 ####################

Adding data as a queue is a common way to add Fmdb

Fmdb does not support simultaneous operation of multiple threads, all of which typically implement related operations serially

[Self.database Open];

First step: Create an action queue

Fmdatabasequeue *queue = [Fmdatabasequeue DatabaseQueueWithPath:self.filePath];

Identity: Record whether the operation was successful

__block BOOL issucceed = YES;

Step Two: Package the required events in the operations queue

[Queue intransaction:^ (Fmdatabase *db, BOOL *rollback) {

Serial queue

Issucceed = [db executeupdate:@ "insert into t_student (name, age, Sex) VALUES (?,?,?)", @ "Old King next Door" @ "@" @ "Nan"] && Issucceed;

Issucceed = [db executeupdate:@ "insert into t_student (name, age, Sex) VALUES (?,?,?)", @ "-1", @ "", @ "Unknow"] && Issucceed;

Issucceed = [db executeupdate:@ "insert into t_student (name, age, Sex) VALUES (?,?,?)" @ "ajar" @ "@" @ "Nan"] && Issucceed;

If there is an error, it will be returned

if (!issucceed) {

The parameters returned by the block rollback to be processed (a pointer of type bool).

*rollback = YES;

Return

}

}];

Close the database

[Self.database Close];

Ios-senior19-fmdb third-party applications

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.