Common functions and statements of SQLite

Source: Internet
Author: User
Tags sqlite

SQLITE3.0 uses the C function interface, which is commonly used as follows:

Sqlite3_open ()//Open Database

Sqlite3_close ()//Close Database

Sqlite3_exec ()//Execute SQL statements, such as creating tables

SQLITE3_PREPARE_V2 ()//Compile SQL statements

Sqlite3_step ()//EXECUTE Query SQL statement

Sqlite3_finalize ()//END SQL statement

Sqlite3_bind_text ()//binding parameters

Sqlite3_column_text ()//Query the data on the field

Create a database table

Sqlite3 *sqlite = nil;

NSString *filepath = [Nshomedirectory () stringbyappendingformat:@ "/documents/%@", @ "Data.sqlite"];

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

if (result = SQLITE_OK) {

NSLog (@ "Failed to open data");

}

To create a table's SQL statement

NSString *sql = @ "CREATE TABLE usertable (userId text not NULL PRIMARY KEY unique,username text, age integer)";

Char *error;

Execute SQL statement

result = Sqlite3_exec (Sqlite,[sql utf8string],null, NULL, &error);

if (Result! = SQLITE_OK) {

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

}

Sqlite_close (SQLite);

Inserting data

Sqlite3 *sqlite = nil;

sqlite3_stmt = *stmt = nil;

NSString *filepath = [Nshomedirectory () stringbyappendingformat:@ "/documents/%@", @ "Data.sqlite"];

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

if (result = SQLITE_OK) {

NSLog (@ "Failed to open data");

}

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

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

NSString *userid = @ "1002";

NSString *username = @ "Zhang San";

int age = 3;

populating data in SQL

Sqlite3_bind_text (stmt, 1, [UserId utf8string], 1 NULL);

Sqite3_bind_text (stmt, 2, [UserName utf8string], -1,null);

Sqite3_bind_int (stmt, 3, age);

result = Sqlite3_step (stmt);

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

NSLog (@ "failed to execute SQL statement");

return NO;

}

Sqlite3_finalize (stmt);

Sqlite3_close (SQLite);

Querying data

Sqlite3 *sqlite = nil;

sqlite3_stmt = *stmt = nil;

NSString *filepath = [Nshomedirectory () stringbyappendingformat:@ "/documents/%@", @ "Data.sqlite"];

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

if (result = SQLITE_OK) {

NSLog (@ "Failed to open data");

}

NSString *sql = @ "Select UserId, username,age from usertable WHERE age;?";

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

int age = 1;

Sqlie3_bind_int (stmt, 1,age);

result = Sqlite3_step (stmt);

A list of data after iterating through a query

while (result = = Sqlite_row) {

Char *userid = (char*) sqlite3_column_text (stmt,0);

Char *username = (char*) sqlite3_column_text (stmt,1);

int age = Sqlite3_column_int (stmt,2);

  

NSString *userid = [NSString stringwithcstring:userid encoding:nsutf8stringencoding];

NSString *username = [NSString stringwithcstring:username encoding:nsutf8stringencoding];

NSLog (@ "-----user name:%@, User id:%@, Age:%d---", username,userid,age);

result = Sqlite3_step (stmt);

}

Sqlite3_finalize (stmt);

Sqlite3_close (SQLite);

Common functions and statements 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.