#include "targetver.h" #include <stdio.h> #include <tchar.h> #include <iostream> #include <string.h> #include "libpq-fe.h" #include "stdafx.h" using namespace std; int main() { const char *conninfo; PGconn *conn; std::string EmailToDB = "gfdgfdshj@mail.ru"; std::string PswdToDb = "1234"; char d = char(64); //Тестовый Char для передачи чистый ANCII const char *paramValues[2] = { (char *)&EmailToDB, (char *)&PswdToDb }; conninfo = "hostaddr = '192.168.0.130' port = '5433' user = 'postgres' password = 'pass' connect_timeout = '2'"; conn = PQconnectdb(conninfo); PQsetClientEncoding(conn, "UTF-8"); if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn)); //exit(0); } else { PQsetClientEncoding(conn, "UTF8"); printf("Good Done \n"); std::string insert = "CREATE ROLE "+ EmailToDB+" WITH PASSWORD "+PswdToDb; PGresult *res = PQexec(conn, insert.c_str()); fprintf(stderr, "UserCreeate: %s", PQerrorMessage(conn)); PQfinish(conn); } system("pause"); return 0; } 

The code does not work, spits out the error indicated in the header. He doesn’t accept any characters in the request except letters, numbers in the username and password are not accepted either, only aZ. Help me figure out what the problem is.

  • Handle your request and get the same. Because the password is without quotes. - PinkTux
  • “Syntax error at or near @” PQexec notice a character that is not being processed. Also write how to create a password correctly, just do not need to say that the password should not contain No.;%: @ # $% and other characters ... nonsense and biliberda, also explain how to store string variables in C ++ without using "", if you think they spoil the SQL query - lostandleft
  • I do not understand what it is. Enclose the password in quotes in the request (do not forget to escape quotes in the password, if they are there). - PinkTux
  • See the PQescapeIdentifier () function - PinkTux

2 answers 2

The error occurs because the password is not enclosed in quotes. In addition, if it can contain non-ASCII characters, it must be further processed. The role must also be pre-processed. The functions PQescapeLiteral () and PQescapeIdentifier () are intended for this. Please note that the values ​​returned by these functions cannot be used directly, they must be saved in order to free the allocated memory:

 const char role[] = "gfdgfdshj@mail.ru"; const char password[] = "%$#$'"; /* * */ char *escaped_role = PQescapeIdentifier( conn, role, sizeof(role) ); char *escaped_password = PQescapeLiteral( conn, password, sizeof(password) ); std::string insert = "CREATE ROLE "; insert += escaped_role; insert += " WITH PASSWORD "; insert += escaped_password; PGresult *rc = PQexec( conn, insert.c_str() ); /* * */ PQfreemem( escaped_role ); PQfreemem( escaped_password ); 
  • Thanks for a more detailed comment! Do you have in the arsenal of the translation option String to Char, which is guaranteed to work. C_str () does not always work. - lostandleft
  • @lostandleft, what does "not always" mean? .c_str() always works in the same way, stably and predictably. There is another method .data() , but it is not clear why it is here. By the way, both of them return not char * , but const char * , these are different things ... - PinkTux
  • Thanks for the example, that option that I wrote below does not work with a number of characters, I looked at yours, figured out, there are no problems, everything is fine. - lostandleft

All hedgehogs are degenerate ... you need to issue a request like this:

 std::string insert = "CREATE USER \"" + EmailToDB + "\" WITH PASSWORD "+ "'\"" + PswdToDb + "\"'"; // РАБОТАЕТ 

I don’t see any problems with the construction, I’ll try the rake