Creation and editing of SQLite

Source: Internet
Author: User
Tags stmt

Create a database statement

-(void) Creatdata

{

Sqlite3 *sqlite = nil;

NSString *filepath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents/data.file"];

Open Database

int result = Sqlite3_open ([FilePath utf8string], &sqlite);

if (Result!=sqlite_ok) {

NSLog (@ "Create failed!!! ");

return;

}

To create a table's SQL statement

NSString *sql = @ "CREATE TABLE IF not EXISTS usertable (userName text PRIMARY KEY, password text,email text)";

Execute SQL statement

Char *error;

result = Sqlite3_exec (SQLite, [SQL Utf8string], NULL, NULL, &ERROR);

if (Result! = SQLITE_OK) {

NSLog (@ "Failed to create database:%s", error);

return;

}

Plug in a piece of data

INSERT OR REPLACE into usertable (username,password,email) VALUES (?,?,?);

Update One piece of data

UPDATE usertable Set password = ' where userName = ';

Querying data

SELECT UserName, password,eamil from usertable where userName = ';

Delete data

DELETE from usertable WHERE username = ';

Close the database

Sqlite3_close (SQLite);

}

**************************

-(void) EditData

{

Sqlite3 *sqlite = nil;

Handle statement

Sqlite3_stmt *stmt =nil;

Database

NSString *filepath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents/data.file"];

int result = Sqlite3_open ([FilePath utf8string], &sqlite);

if (Result!=sqlite_ok) {

NSLog (@ "Open database failed!!! ");

return;

}

Creating SQL statements

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

Compiling SQL statements

SQLITE3_PREPARE_V2 (SQLite, [SQL Utf8string],-1, &stmt, NULL);

NSString *username = @ "Zhang San";

NSString *password = @ "123456";

NSString *email = @ "MXYD.QQ";

Binding Populate SQL statements

Sqlite3_bind_text (stmt, 1, [UserName utf8string],-1, NULL);

Sqlite3_bind_text (stmt, 2, [Password utf8string],-1, NULL);

Sqlite3_bind_text (stmt, 3, [email utf8string],-1, NULL);

SQL Edit Statement

Execute SQL statement

result = Sqlite3_step (stmt);

if (result = = Sqlite_error | | result = = sqlite_misuse) {

NSLog (@ "Compiling database Error!!!" ");

Return

}

Close Handle Statement

Sqlite3_finalize (stmt);

Close the database

Sqlite3_close (SQLite);

NSLog (@ "Data inserted successfully!!! ");

}

Creation and editing of SQLite

Related Article

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.