发布网友 发布时间:2024-03-08 14:21
共1个回答
热心网友 时间:2024-04-19 12:24
结果集的查询,需要用到SQLStatement对象。Statement对象表示一条SQL语句,可以理解为preparedstatement或者compiledstatement。一般使用sqlite3_prepare_v2()函数创建Statement对象。头文件中定义了2个成员变量:sqlite3*noteDB;NSString*databasePath;示例代码如下所示:-(void)initializeDataToDisplay{self.noteArray=[[NSMutableArrayalloc]init];constchar*dbpath=[databasePathUTF8String];sqlite3_stmt*statement;if(sqlite3_open(dbpath,¬eDB)==SQLITE_OK){NSString*querySql=[NSStringstringWithFormat:@"SELECTid,whattime,address,what,who,noteFROMNotebook"];constchar*query_stmt=[querySqlUTF8String];if(sqlite3_prepare_v2(noteDB,query_stmt,-1,&statement,NULL)==SQLITE_OK){while(sqlite3_step(statement)==SQLITE_ROW){NotebookInfo*notebookInfo=[[NotebookInfoalloc]init];notebookInfo.pk_id=sqlite3_column_int(statement,0);notebookInfo.whattime=[NSStringstringWithUTF8String:(char*)sqlite3_column_text(statement,1)];notebookInfo.what=[NSStringstringWithUTF8String:(char*)sqlite3_column_text(statement,3)];[noteArrayaddObject:notebookInfo];}}else{NSLog(@"Problemwithpreparestatement:%s",sqlite3_errmsg(noteDB));}//销毁Statement对象sqlite3_finalize(statement);}}将查询的结果存放在noteArray数组中,然后由TableView表视图显示数据记录。