Database Fmdb-sql

Source: Internet
Author: User

1, first to import the third-party class library fmdatabase.

2. Get the sandbox address where the database files are stored.

#pragma mark-Create a database

-(void) Createandinitdatabase {

NSString *path = [NSString stringwithformat:@ "%@/documents/test.db", Nshomedirectory ()];

If the database file exists, open directly, or if not, create

_database = [[Fmdatabase alloc] initwithpath:path];

if (!_database.open) {

NSLog (@ "Open failed");

Return

}

NSLog (@ "open successfully");

Create

NSString *sql = @ "CREATE table if not exists stuinfo (SID varchar (+), username varchar (+), password varchar (32))";

Perform

Note: Execute SQL query statements using ExecuteQuery

Executing non-query statements using the Executeupdata method

BOOL B = [_database executeupdate:sql];

NSLog (@ "CREATE table%d", b);

}

#pragma mark-Insert data

-(void) InsertData {

NSString *sid = @ "01";

NSString *username = @ "Dage";

NSString *password = @ "1234";

SQL execution, using instead of changing data

NSString *sql = @ "INSERT into Stuinfo (Sid,username,password) VALUES (?,?,?)";

BOOL B = [_database Executeupdate:sql,sid,username,password];

NSLog (@ "Insert data =%d", b);

}

#pragma mark-query data

-(void) Searchdata {

NSString *sql = @ "Select *from stuinfo";

Returns the result value (understood as EXECL table, multi-row multi-column series data structure)

Fmresultset *resultset = [_database executequery:sql];

while ([ResultSet next]) {

NSLog (@ "sid =%@ name =%@ PW =%@", [ResultSet stringforcolumnindex:0],[resultset stringforcolumnindex:1],[resultset Stri Ngforcolumnindex:2]);

}

}

#pragma mark-Modify data

-(void) Updatadata {

NSString *username = @ "xiyoushijing";

NSString *sql = @ "Update stuinfo set sid= ' where username=?";

BOOL B = [_database executeupdate:sql,username];

NSLog (@ "Modify data =%d", b);

}

#pragma mark-delete data

-(void) DeleteData {

NSString *username = @ "xiyoushijing";

NSString *sql = @ "Delete from Stuinfo where username=?";

BOOL B = [_database executeupdate:sql,username];

NSLog (@ "Delete data =%d", b);

}

Database Fmdb-sql

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.