2010-02-09 619 views

回答

4

sqlite3_data_count返回当前正在执行的语句的值(列)数。 没有结果它返回0.

sqlite3_column_count另一方面,返回总是列数,有无结果。

您将使用哪一个取决于您是否必须获取列数。

+0

我还是很困惑。你的意思是sqlite3_data_count实际上返回结果集中的行数,而不是列? – 2010-02-09 11:01:10

12

我抬头的两个函数来源:

/* 
** Return the number of columns in the result set for the statement pStmt. 
*/ 
SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){ 
    Vdbe *pVm = (Vdbe *)pStmt; 
    return pVm ? pVm->nResColumn : 0; 
} 

/* 
** Return the number of values available from the current row of the 
** currently executing statement pStmt. 
*/ 
SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){ 
    Vdbe *pVm = (Vdbe *)pStmt; 
    if(pVm==0 || pVm->pResultSet==0) return 0; 
    return pVm->nResColumn; 
} 

现在很明显的区别是什么,所以NickD是正确的。

顺便说一句,文档是可怕的:"The sqlite3_data_count(P) the number of columns in the of the result set of prepared statement P."这甚至不是正确的英文,函数定义随附的评论好得多。

+0

有时我们必须检查来源:) +1检查出来。我同意这部分文档不够清楚。 – 2010-02-09 11:26:21

+0

对不起,这是有点偏离主题,但它是相关的。我觉得对于任何给定的预处理语句,其结果列计数将始终保持不变(即使使用'prepare_v2'来使语句的SQL文本内容始终保持不变)。所以我希望在最初准备语句之后一次调用'sqlite3_column_count'就足够了? (而不是每次调用'sqlite3_step'后调用它,例如我看到很多示例代码) – 2016-01-16 00:20:15