Sqllite (III): sqlite3_prepare_v2, sqlite3_step

Source: Internet
Author: User

If you do not want to write a callback function and want to avoid the troublesome one-dimensional array traversal after sqlite3_get_table, use sqlite3_prepare_v2 to execute the SQL SELECT statement, it is very convenient for sqlite3_step to traverse the returned results of select execution.
Of course, you must understand that sqlite3_prepare_v2 can not only execute Table query selection, but also perform SQL Delete, insert, update and other operations conveniently. It can help you make SQL statement execution more elegant.

int sqlite3_prepare_v2(  sqlite3 *db,            /* Database handle */  const char *zSql,       /* SQL statement, UTF-8 encoded */  int nByte,              /* Maximum length of zSql in bytes. */  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */  const char **pzTail     /* OUT: Pointer to unused portion of zSql */);
int sqlite3_step(sqlite3_stmt*);

The following uses the selection query in IOS as an example to describe the usage of the two:

-(void)InitializeFilesTable{    const char * query = "SELECT * FROM [FileObjects]";    sqlite3_stmt * stmt;    int result = sqlite3_prepare_v2(mDatabase, query, -1, &stmt, NULL);    if(SQLITE_OK != result)    {        sqlite3_finalize(stmt);        // The table has not been created. Most likely, this is the first time we create the database.        // Therefore, create all tables in it        char * sql = "Create TABLE [FileObjects] ([FileId] VARCHAR(128),[FileExt] VARCHAR(128), [FileName] VARCHAR(128), [FileUrl] VARCHAR(128), [FileType] INT  );"; // NOXLATE        char * errMsg;        result = sqlite3_exec(mDatabase, sql, NULL, NULL, &errMsg);    }    else    {        // Select returns OK, initialize the memory model from the result        NSMutableDictionary * files = [NSMutableDictionary new];        while(sqlite3_step(stmt) == SQLITE_ROW)        {            FileObject * file = [FileObject new];                        const char * str = (const char *)sqlite3_column_text(stmt, 0);            file.FileId = str? [[NSString alloc] initWithUTF8String:str] : @"";                        str = (const char *)sqlite3_column_text(stmt, 1);            file.FileExt = str? [[NSString alloc] initWithUTF8String:str] : @"";                        str = (const char *)sqlite3_column_text(stmt, 2);            file.FileName = str? [[NSString alloc] initWithUTF8String:str] : @"";                        str = (const char *)sqlite3_column_text(stmt, 3);            file.FileUrl = str? [[NSString alloc] initWithUTF8String:str] : @"";                        file.FileType = sqlite3_column_int(stmt, 4);                        [files setObject:file forKey:file.FileId];        }                sqlite3_finalize(stmt);        [mFiles setDictionary:files];    }}

This includes calling sqlite3_exec. Sqlite3_exec can execute any SQL statement, including the transaction ("begin
Transaction "), rollback (" rollback "), commit (" commit "), and so on.

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.