使用C ++编写程序时出现Postgres SQL pqxx错误

时间:2016-03-10 04:26:00

标签: c++ sql postgresql csv

我正在尝试解析具有43个总属性的1001行CSV文件。我的最终结果应该是一个包含43个属性和1000个元组的表。我希望我的程序跳过第一行,在第一行只是标题之前(并且不需要)。 CSV具有以下格式:

第1行:HOUSEID,VARSTRAT,WTHHFIN,DRVRCNT,CDIVMSAR,CENSUS_D,CENSUS_R,HH_HISP,HH_RACE,HHFAMINC,HHRELATD,HHRESP,HHSIZE,HHSTATE,HHSTFIPS,HHVEHCNT,HOMEOWN,HOMETYPE,MSACAT,MSASIZE,NUMADLT,RAIL, RESP_CNT,SCRESP,TRAVDAY,都市,URBANSIZE,URBRUR,WRKCOUNT,TDAYDATE,FLAG100,LIF_CYC,CNTTDHH,HBHUR,HTRESDN,HTHTNRNT,HTPPOPDN,HTEEMPDN,HBRESDN,HBHTNRNT,HBPPOPDN,HH_CBSA,HHC_MSA

其余部分:

线路2: 20000017,48,264.19469357,2,33,03,02,02,01,18,-1,02,3,IN,18,3,01,01,03,03,3,02,3,-1,04, 04,06,02,1,200904,01,10,13,TC,300,5,300,75,300,5,300,XXXXX,XXXX

第3行:(格式相同)

第4行:(格式相同) 等

第1000行: 我的代码的目标是将每一行存储在行内,然后解析该行以获取要存储在字符串“column”中的每个属性。然后,在每一行之后,我将计数重置为等于1。 这是我的代码:

#include <iostream>
#include <pqxx/pqxx> 
#include <stdlib.h>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;
using namespace pqxx;

int main(int argc, char* argv[])
{
  string user_name = getenv("USER");
  string host = "/home/" + user_name + "/postgres";
  string connection_string = "dbname=postgres user=" + user_name + " host=" +            host + " port=5432";
  connection C(connection_string.c_str());

  if (C.is_open()) 
  {
    cout << "Database was opened successfully: " << C.dbname() << endl;
  }
  else 
  {
    cout << "Database couldn't be opened" << endl;
    return 1;
  }
  /* database openeed */
  string HHV2PUB = "CREATE TABLE HOUSEHOLD(HOUSEID INT, VARSTRAT INT, WTHHFIN   DOUBLE PRECISION,   DRVRCNT INT, CDIVMSAR INT, CENSUS_D INT, CENSUS_R INT, HH_HISP INT, HH_RACE INT, HHFAMINC INT, HHRELATD INT, HHRESP INT, HHSIZE INT, HHSTATE CHAR(2), HHSTFIPS INT, HHVEHCNT INT, HOMEOWN INT, HOMETYPE INT, MSACAT INT, MSASIZE INT, NUMADLT INT, RAIL INT, RESP_CNT INT, SCRESP INT, TRAVDAY INT, URBAN INT, URBANSIZE INT, URBRUR INT, WRKCOUNT INT, TDAYDATE INT, FLAG100 INT, LIF_CYC INT, CNTTDHH INT, HBHUR VARCHAR(2), HTRESDN INT, HTHTNRNT INT, HTPPOPDN INT, HTEEMPDN INT, HBRESDN INT, HBHTNRNT INT, HBPPOPDN INT, HH_CBSA INT, HHC_MSA INT);";



  ifstream inputFile("HHV2PUB.CSV");
  string line;
  string line1;
  std::getline(inputFile, line1);
  string blah("\'*\'"); //DO I NEED THESE?????
  string blah2("1337");
  string blah3("9999");
  string sql; 

  while (std::getline(inputFile, line))
  {
    int count = 1;
    std::stringstream lineStream(line);
    string column;
    sql = "INSERT INTO HOUSEHOLD VALUES (";
    while (std::getline(lineStream, column, ','))
    {
      cout << count << endl;
      if (count == 14 || count == 34) 
      {
        if (count != 34) 
        { //count == 14
          sql += "\'";
          sql += column;
          sql += "\'";
          count++;
          sql+= ", ";
          continue;
        }
        else 
        { //count == 34
          if (column[0] == '-' && column[1] == '9' && count == 34)
          { 
            sql+= blah;
            count++;
            sql+= ", ";
            continue;
          }
          else if (count == 34) 
          {
            sql+= "\'";
            sql+= column;
            sql += "\'";
            count++;
            sql+= ", ";
            continue;
          }
        }
      }
      if ((column == "XXXXX") && (count == 42)) 
      {
        cout << "dddd" << endl;
        sql+= blah2;
        count++;
        sql+= ", ";
        continue;
      }

      if (count == 43) { cout << column << endl;}
      cout << "cccccccccccccc" << endl; 
      if ((column == "XXXX") && (count == 43)) //WHY DOESN"T IT ENTER HERE? BOH CONDITIONS ARE TRUE BUT PROGRAM DOESNT GO IN HERE.
      {
        cout << "HELLLLO" << endl;
        sql+= blah3;
        count++;
        cout << "Count is " << count << endl;
        continue;
      }
      //else if (count == 43) 
      //{
      //  sql+= column;
      //  count++;
      //  continue;
        // dont append the ","
      //}
      sql += column;
      count++;
      sql += ", ";
      continue; 
    }
    cout << sql << endl;
    if (count == 44) 
    {
      cout << "***********************" << endl; 
      sql+= "); "; 
      //count = 1;
    }
    //cout << count << endl;
    //cout << sql << endl;
    work W(C);
    W.exec(sql);
    W.commit();  
  }
  C.disconnect(); 
  return 0;
}

“输出: [kcheng94 @ pc12 Ascii] $ a.out 数据库成功打开:postgres 1 cccccccccccccc 2 cccccccccccccc 3 cccccccccccccc 4 cccccccccccccc 五 cccccccccccccc 6 cccccccccccccc 7 cccccccccccccc 8 cccccccccccccc 9 cccccccccccccc 10 cccccccccccccc 11 cccccccccccccc 12 cccccccccccccc 13 cccccccccccccc 14 15 cccccccccccccc 16 cccccccccccccc 17 cccccccccccccc 18 cccccccccccccc 19 cccccccccccccc 20 cccccccccccccc 21 cccccccccccccc 22 cccccccccccccc 23 cccccccccccccc 24 cccccccccccccc 25 cccccccccccccc 26 cccccccccccccc 27 cccccccccccccc 28 cccccccccccccc 29 cccccccccccccc 三十 cccccccccccccc 31 cccccccccccccc 32 cccccccccccccc 33 cccccccccccccc 34 35 cccccccccccccc 36 cccccccccccccc 37 cccccccccccccc 38 cccccccccccccc 39 cccccccccccccc 40 cccccccccccccc 41 cccccccccccccc 42 DDDD 43 XXXX cccccccccccccc 插入家庭价值观(20000017,48,264.19469357,2,33,03,02,02,01,18,-1,02,3,'IN',18,3,01,01,03,03,3, 02,3,-1,04,04,06,02,1,200904 ,,, 10,13,'TC',300,5,300,75,300,5,300,1337,XXXX

在抛出'pqxx :: syntax_error'的实例后终止调用   what():ERROR:语法错误在或附近“)” 第2行:,); “           ^

中止(核心倾倒) 为什么我会把核心倾倒?另外,为什么在200904和10之间会有一个随机逗号“?”?我不知道为什么我的程序永远不会进入If语句的主体,即使测试条件为真,如输出中所示。非常感谢您查看我的帖子。

0 个答案:

没有答案