格式化回调函数的输出

时间:2013-02-16 16:28:10

标签: c++ sqlite

我有以下回调函数。

// This is the callback function to display the select data in the table
static int callback(void *NotUsed, int argc, char **argv, char **szColName)
{
  for(int i = 0; i < argc; i++)
  {
      std::cout << " = " << argv[i] << std::endl;
  }

  std::cout << "\n";

  return 0;
}

// prepare our sql statements
const char *pSQL[6];
pSQL[0] = "SELECT * FROM User";

// execute sql
for(int i = 0; i < 1; i++)
{
    rc = sqlite3_exec(db, pSQL[i], callback, 0, &szErrMsg);
    if(rc != SQLITE_OK)
    {
        std::cout << "SQL Error: " << szErrMsg << std::endl;
        sqlite3_free(szErrMsg);
        break;
    }
}

产生以下输出

1
mark
mark@email.com
98989898
mark
mark
ADMN
U
Where are you studying
SIM
0

2
ken
ken@email.com
92222898
ken
ken
ADMN
U
Where are you studying
SIM
0

3
joseph
joseph@email.com
92333718
joseph
joseph
ADMN
U
Where are you studying
SIM
0

4
ricky
ricky@email.com
81822801
ricky
ricky
ADMN
U
Where are you studying
SIM
0

5
ck
ck@email.com
94567828
ck
ck
ADMN
U
Where are you studying
SIM
0

我需要像这样横向格式化而不是垂直格式化:

1    mark      mark@email.com      98989898    mark      mark      ADMN    U    Where are you studying    SIM    0
2    ken       ken@email.com       92222898    ken       ken       ADMN    U    Where are you studying    SIM    0
3    joseph    joseph@email.com    92333718    joseph    joseph    ADMN    U    Where are you studying    SIM    0
4    ricky     ricky@email.com     81822801    ricky     ricky     ADMN    U    Where are you studying    SIM    0
5    ck        ck@email.com        94567828    ck        ck        ADMN    U    Where are you studying    SIM    0

我尝试在回调函数中使用它:

1) std::cout << argv[i] << "\t";
2) std::cout << argv[i] << setw(15);
3) std::cout << setw(15) << argv[i];

但它给我的全部是格式错误的行。

1   mark    test@email.com  98989898    mark    mark    ADMN    U   Where are you studying  SIM 0   
2   ken ken@email.com   92222898    ken ken ADMN    U   Where are you studying  SIM 0   
3   joseph  joseph@email.com    92333718    joseph  joseph  ADMN    U   Where are you studying  SIM 0   
4   ricky   ricky@email.com 81822801    ricky   ricky   ADMN    U   Where are you studying  SIM 0   
5   ck  ck@email.com    94567828    ck  ck  ADMN    U   Where are you studying  SIM 0

2 个答案:

答案 0 :(得分:0)

您需要std::setw和可能的std::left操纵者。

答案 1 :(得分:0)

请勿在{{1​​}}循环中使用std::endl。该操纵器使for循环中的每个项目都打印在一个新行上。