Basic operations for IOS SQLite

Source: Internet
Author: User

Add Libsqlite this library and introduce sqlite3.h header files before all operations

One, create database open database, close database

NSString *sqlpath =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; SQLPath= [_sqlpath stringbyappendingpathcomponent:@"Students.sqlite"]; Sqlite3*SQL; intresult = Sqlite3_open (self.sqlPath.UTF8String, &SQL); if(Result = =SQLITE_OK) {NSLog (@"Open Database succeeded"); Self.sql=SQL; returnYES; } NSLog (@"failed to open database");
    Sqlite3 *sql = self.sql;    if (sqlite3_close (sql) = = SQLITE_OK) {        NSLog (@ "Close database Success");        return YES;    }

Second, insert data

Stitching statements

    [Self opendatabase];    NSString *sqlstring = [NSString stringwithformat:@ "insert into t_student (name) VALUES (' Mao Xiao Dong ')"];    Sqlite3 *sql = self.sql;    char *error;    int result = sqlite3_exec (sql, Sqlstring.utf8string,null, NULL, &error);    if (result = = SQLITE_OK)        NSLog (@ "Insert succeeded");    [Self closeDataBase];

Temporary character mode

    NSString *sqlstring = @ "INSERT into t_student (name) VALUES (?)";    Sqlite3_stmt *stmt;    int result  = SQLITE3_PREPARE_V2 (Self.sql, Sqlstring.utf8string,-1, &stmt, NULL);    if (result = = SQLITE_OK) {        sqlite3_bind_text (stmt, 1, [NSString stringwithformat:@ "Ye Shenhong"]. Utf8string,-1, NULL);    }    int resultinsert = Sqlite3_step (stmt);    if (Resultinsert = = Sqlite_done)        NSLog (@ "Insert Success");        [Self closeDataBase];

Second, query

    [Self opendatabase];    NSString *sqlstring = [NSString stringwithformat:@ "select Id,name from T_student"];    Sqlite3_stmt *stmt;    int result = SQLITE3_PREPARE_V2 (Self.sql, Sqlstring.utf8string,-1, &stmt, NULL);    if (result = = SQLITE_OK) {        while (sqlite3_step (stmt) = = Sqlite_row) {            int studentid = sqlite3_column_int (stmt, 0);            NSString *studentname =  [NSString stringwithcstring: (const char *) Sqlite3_column_text (stmt, 1) encoding:nsutf8stringencoding];            NSLog (@ "ID =%d  name =%@", studentid,studentname);        }    }    Sqlite3_finalize (stmt);    [Self closeDataBase];

Three, delete

    [Self opendatabase];    NSString *sqlstring = [NSString stringwithformat:@ "Delete from t_student where id = 5"];    char *error;    int result = Sqlite3_exec (self.sql, sqlstring.utf8string, NULL, NULL, &ERROR);    if (result = = SQLITE_OK)        NSLog (@ "Success");    [Self closeDataBase];

Four, change

    [Self opendatabase];    NSString *sqlstring = [nsstring stringwithformat:@ "Update t_student set name = ' furry ' WHERE id = 3"];    char *error;    int result = Sqlite3_exec (self.sql, sqlstring.utf8string, NULL, NULL, &ERROR);    if (result = = SQLITE_OK)        NSLog (@ "modified successfully");    [Self closeDataBase];

  

Five, common methods

Open the database function,

SQLITE_API int sqlite_stdcall sqlite3_open (

Const char *filename, /* database filename (UTF-8) *///db file path

sqlite3 **ppdb /* Out:sqlite DB handle *//database handle

);

Close the database

SQLITE_API int sqlite_stdcall sqlite3_close (sqlite3*);

Execute SQL statements, can perform additions and deletions and other operations, but generally do not take to perform the check operation, because the return is not the data queried

SQLITE_API int sqlite_stdcall sqlite3_exec (

sqlite3*,/ * An open database *///handle

Const char *sql,/ * SQL to is evaluated *///sql statement

int (*callback) (void*,int,char **,Char* *),/ * callback function *///execution Complete callback

void *,/ * 1st argument to callback *///callback block first parameter

char **errmsg/ * ERROR msg written here *///fault message

);

Check SQL statements

SQLITE_API int sqlite_stdcall sqlite3_prepare_v2 (

sqlite3 *db,/ * Database handle *//DB handle

Const char *zsql, /* SQL statement, UTF-8 encoded *//SQL statement

int Nbyte, /* Maximum length of Zsql in bytes. */SQL statement maximum length, 1 is not checked

sqlite3_stmt **ppstmt,/ * out:statement handle */sqlite3_stmt instance, can get data

Const char **pztail/ * Out:pointer to unused portion of Zsql * /

);

Demo:http://pan.baidu.com/s/1mhdpqp6

Basic operations for IOS SQLite

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.