如何使用PostgreSQL通过C ++将变量插入数据库表?

时间:2016-11-30 21:33:43

标签: c++ postgresql

我有一个将值插入数据库表的C ++程序。我不能直接硬编码值,因为数据不断更新,但我真的很困惑语法。 当我尝试这样做时:

l.exec("INSERT INTO course VALUES(cid, term, 'subj',crse, sec, 'units', 'instructors');");
          l.exec("INSERT INTO meeting VALUES(cid, term, 'type', 'days', 'time', 'build', room);");
          l.exec("INSERT INTO enrolledin VALUES(cid, term, sid, 'major', 'classlevel', 'level', 'status', seat, numunits, 'grade');");
          l.exec("INSERT INTO student VALUES(sid, 'surname', 'prefname', 'email');");

I get this error: 
terminate called after throwing an instance of 'pqxx::undefined_column'
  what():  ERROR:  column "cid" does not exist
LINE 1: INSERT INTO course VALUES(cid, term, 'subj',crse, se...

                              ^
HINT:  There is a column named "cid" in table "course", but it cannot be referenced from this part of the query.

- 我被告知这是因为我插入了文字字符串名而不是字符串中的值,而我对如何在仍使用变量名时通过C ++在字符串中插入值感到困惑。

1 个答案:

答案 0 :(得分:0)

使用的SQL elements can be accessed not only through iterators, but also using offsets on regular pointers to elements查询的语法不正确。它应该是:

INSERT

您应该指定表名以及要插入的列和之后的值。为简单起见,我减少了列数。有关INSERT INTO course (cid, subj) VALUES(1, 'subj'); 查询的完整语法,请查看PostgreSQL documentation

要从变量中插入值,您可以执行以下操作:

INSERT

esc()函数有助于防止SQL注入攻击。