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.