IOS: SQL, iossql

Source: Internet
Author: User

IOS: SQL, iossql

Although iOS also has SQL statements, it is rarely used (at least I am currently doing this ). Big Data is directly thrown to the backend, and small Plist is enough.

Let's take another step back. With FMDB, the native is also used less.

The following are the notes for learning SQL.

 

1. Create

1-1) Open: Database pointer, save address

sqlite3_open([path UTF8String], &new_sql)

1-2) create: Database pointer, create command, error command

NSString *command = @"CREATE TABLE IF NOT EXISTS UserTable (username TEXT primary key,password TEXT,age TEXT)";sqlite3_exec(new_sql, [command UTF8String], NULL, NULL, &new_error)

1-3) Close: Database pointer

sqlite3_close(new_sql);

 

2. Insert

2-1) Open: Database pointer, save address

Same as above

2-2) insert:

// INSERT: Command NSString * op = @ "insert into UserTable (username, password, age) VALUES (?,?,?) "; // Preparation: Database pointer, insert command, handle sqlite3_prepare (new_ SQL, [op UTF8String],-1, & new_stmt, NULL); // binding: handle, location, data sqlite3_bind_text (new_stmt, 1, [name UTF8String],-1, NULL); // next: handle sqlite3_step (new_stmt); // end: handle sqlite3_finalize (new_stmt );

2-3) Close: Database pointer

Same as above

 

3. Delete

3-1) Open: Database pointer, save address

Same as above

3-2) Delete:

// DELETE command: NSString * op = @ "delete from userTable WHERE userName =? "; // Preparation: Database pointer, delete command, handle sqlite3_prepare (new_ SQL, [op UTF8String],-1, & new_stmt, NULL); // binding: handle, location, data sqlite3_bind_text (new_stmt, 1, [name UTF8String],-1, NULL); // next: handle sqlite3_step (new_stmt); // end: handle sqlite3_finalize (new_stmt );

3-3) Close: Database pointer

Same as above

 

4. Select

4-1) Open: Database pointer, save address

Same as above

4-2) Select:

// SELECT command: 1) NSString * op = @ "SELECT username, password, age From UserTable where username =? "; 2) NSString * op = @" SELECT username, password, age From UserTable "; // preparation: Database pointer, selection command, handle sqlite3_prepare (new_ SQL, [op UTF8String], -1, & new_stmt, NULL); // binding: handle, location, data sqlite3_bind_text (new_stmt, 1, [selet_name UTF8String],-1, NULL); // next step: handle sqlite3_step (new_stmt); // traversal: while (result = SQLITE_ROW) {char * c_name = (char *) sqlite3_column_text (new_stmt, 0 ); char * c_password = (char *) sqlite3_column_text (new_stmt, 1); char * c_age = (char *) sqlite3_column_text (new_stmt, 2); NSString * s_name = [NSString stringWithCString: c_name encoding: encoding]; NSString * s_password = [NSString stringWithCString: c_password encoding: NSUTF8StringEncoding]; NSString * s_age = [NSString stringWithCString: c_age encoding: encoding]; NSLog (@ "% @, % @, % @", s_name, s_password, s_age); result = sqlite3_step (new_stmt); // NSLog (@ "% d ", new_stmt);} // end: handle sqlite3_finalize (new_stmt );

4-3) Close: Database pointer

Same as above

 

Appendix:

5. Insert a variant: Update

@"UPDATE UserTable SET password = ? where username = ?"

 

6. Select variants: Sort

@"SELECT * FROM userTable ORDER BY age ASC(DESC)"

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.