打印表列名称

时间:2013-02-17 10:27:17

标签: c++ sqlite callback tablecolumn

我目前有这个功能可以打印出表格的每一行

static int callback(void *NotUsed, int argc, char **argv, char **szColName)
{
    for(int i = 0; i < argc; i++)
    {
        cout.width(17); cout << left << argv[i];
    }

    std::cout << "\n";

    return 0;
}

如何打印出szColName,这样它只出现在顶部,而不是多次出现? 试过这个:

static int callback(void *NotUsed, int argc, char **argv, char **szColName)
{
    int n = sizeof(szColName) / sizeof(szColName[0]);
    for (int i = 0; i < n; i++)
    {
        cout.width(17); cout << left << szColName[i];
    }
    printf("\n");
    for(int i = 0; i < argc; i++)
    {
        cout.width(17); cout << left << argv[i];
    }

    std::cout << "\n";

    return 0;
}

但是在输出行值

之后每次输出

1 个答案:

答案 0 :(得分:0)

您可能希望声明一个static bool内部回调来记录它是否已打印出列名。或者,如果您希望能够重置它......

如:

static bool firstline = true;

static int callback(void *NotUsed, int argc, char **argv, char **szColName)
{
if (firstline){
    int n = sizeof(szColName) / sizeof(szColName[0]);//this is incorrect but fixing
                                                 // it requires changing the prototype. 
                                                  //See the comments below
    for (int i = 0; i < n; i++)
    {
        cout.width(17); cout << szColName[i] << left;
    }
    printf("\n");
    firstline=false;
}
for(int i = 0; i < argc; i++)
{
    cout.width(17); cout << argv[i] << left;
}

std::cout << "\n";

return 0;
}
int main(){
    for(int x=0;x<10;++x)callback( ... , ... , ... ); // give whatever argument you need to give

    firstline = true;  //reset the variable so that next time you call it, the col names will appear
    for(int x=0;x<10;++x)callback(...,...,...);// now the col names will appear again.
}

我认为您提供的内容会正确打印出行和列名称。我只添加了一个变量来检查是否需要打印列名。