IOS Learning Note 17-fmdb app

Source: Internet
Author: User

First introduce

Create a database

NSString *path = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObject];

NSLog (@ "%@", Path);

NSString *databasepath = [path stringbyappendingstring:@ "/dsn.sqlite"];

/**

* Create a Fmdb database

**/

Fmdatabase *db = [[Fmdatabase Alloc]initwithpath:databasepath];

[DB Open];

//    /**

* Create a table

//     **/

NSString *createtable = @ "CREATE table Student (ID integer primary key autoincrement,name Text,password text)";

[DB executeupdate:createtable];

/**

* Insert Data

**/

NSString *insertsql = @ "INSERT into Student (Name,password) VALUES (' DSN ', ' 123456 ')";

BOOL B = [db executeupdate:insertsql];

if (b) {

NSLog (@ "add success");

}else{

NSLog (@ "Add failed");

}

/**

* parameter, return value, find

**/

Fmresultset *set = [db executequery:@ "select * from Student where name;?", @ "DSN"];

if (!set) {

NSLog (@ "Query failed");

}else{

while ([Set next]) {

int id = [set intforcolumnindex:0];

NSString *name = [set stringforcolumnindex:1];

NSString *password = [set stringforcolumnindex:2];

NSLog (@ "%d,%@,%@", Id,name,password);

}

}

//    /**

* with parameters, add

//     **/

//

for (int i = 0; i<5; i++) {

NSString *name = [NSString stringwithformat:@ "dsn%d", i+1];

NSString *password = [NSString stringwithformat:@ "%06d", Arc4random ()%100000];

//

[DB executeupdate:@ insert INTO Student (Name,password) VALUES (?,?) ", Name,password];

//    }

//

/**

* Enquiry

//     **/

//

NSString *selectsql = @ "SELECT * from Student";

Fmresultset *set = [db Executequery:selectsql];

NSLog (@ "%@", set);

while ([Set next]) {

int stuid = [set intforcolumnindex:0];

NSString *stuname = [set stringforcolumnindex:1];

NSString *stupass = [set stringforcolumnindex:2];

NSLog (@ "stuid =%d,name =%@,pass =%@", Stuid,stuname,stupass);

//    }

IOS Learning Note 17-fmdb app

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.