在SQLite C API中使用sqlite3_data_count()而不是sqlite3_column_count()有什么好处?

时间:2010-02-09 09:57:57

标签: c api sqlite

在阅读文档之后,似乎函数sqlite3_column_count完全相同,但没有sqlite3_data_count所具有的限制。

为什么我要在sqlite3_column_count上使用sqlite3_data_count?谢谢!

2 个答案:

答案 0 :(得分: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."这甚至不是正确的英语,函数定义附带的注释要好得多。

答案 1 :(得分:4)

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

另一方面,

sqlite3_column_count始终返回列数,包含或不包含结果。

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

相关问题