I am using libpq-fe to connect to the sql server as explained here.
I made a small file to check login using this api.
Here is the file psql.cpp:
#include <iostream>
#include <libpq-fe.h>
#include <string>
using namespace std;
void CloseConn(PGconn *conn)
{
PQfinish(conn);
getchar();
}
PGconn *ConnectDB(string user="postgres",string password="123321",string dbname="bridge",string hostaddr="127.0.0.1",string port="5432")
{
PGconn *conn = NULL;
string s = "user=" + user + " password=" + password + " dbname=" +dbname + " hostaddr=" + hostaddr + " port=" + port;
// Make a connection to the database
conn = PQconnectdb(s.c_str());
// Check to see that the backend connection was successfully made
if (PQstatus(conn) != CONNECTION_OK)
{
cout << "Connection to database failed.\n";
CloseConn(conn);
}
cout << "Connection to database - OK\n";
return conn;
}
void login_check(PGconn *conn, string username, string password)
{
string query = "SELECT * FROM login where plid='" + username + "'";
PGresult *res = PQexec(conn,query.c_str());
if(PQresultStatus(res) == PGRES_TUPLES_OK)//successful completion of a command returning data
{
cout << "query executed successfully\n";
int row = PQntuples(res); // number of rows in the output of the query
cout<<row<<endl;
if (row != 1)
{
//wrong username
}
else
{
cout<<PQgetvalue(res,0,1)<<endl;
if( !(string(PQgetvalue(res,0,1)).compare(password)) )//return 0 on equality
{
cout<<"valid user";
}
}
}
// Clear result
PQclear(res);
}
I put hits main function in this file and everything works fine:
int main()
{
PGconn *conn = NULL;
//conn = ConnectDB("postgres","123321","bridge","127.0.0.1","5432");
conn = ConnectDB("postgres","123321","bridge","127.0.0.1","5432");
if (conn != NULL) {
login(conn, "11111000", "abcd");
CloseConn(conn);
}
return 0;
}
Now I want to include this file in another .cpp file so I made a psql.h file:
#include <libpq-fe.h>
#include <string>
using namespace std;
PGconn *ConnectDB(string ,string, string, string, string);
void login_check(PGconn, string, string);
void CloseConn(PGconn);
For using the psql.h file the changes I have done in psql.cpp header are:
#include "psql.h"
#include <iostream>
void CloseConn(PGconn *conn)
{
...
...
and I removed the main function from this.
Now in my new file – dispatcher.cpp I want to include this file, So change in its header:
#include "psql.h"
And I put the contents of the main function that I have mentioned above in the main function of this file. When I compile this file I got the error:
dispatcher.cpp: In function ‘void login(int)’:
dispatcher.cpp:154:45: error: parameter 1 of ‘void login_check(PGconn, std::string, std::string)’ has incomplete type ‘PGconn {aka pg_conn}’
dispatcher.cpp:155:23: error: parameter 1 of ‘void CloseConn(PGconn)’ has incomplete type ‘PGconn {aka pg_conn}’
My makefile:
dispatcher:dispatcher.o access.o psql.o
g++ dispatcher.o access.o psql.o -pthread -I /usr/include/postgresql -lpq -o ./bin/dispatcher
dispatcher.o:dispatcher.cpp
g++ -I /usr/include/postgresql -lpq -c dispatcher.cpp
access.o:access.cpp access.h
g++ -c access.cpp
psql.o:psql.cpp psql.h
g++ -c psql.cpp -I /usr/include/postgresql -lpq
You can ignore access.o it only contain accessories. I know I am c programming using c++. Why I am getting this error. Is it anything specific to CPP. The login() is a simple function that I am calling from the dispatcher's main and its defination is same as of the main I have mentioned above.
This doesn’t match your functions. Use:
You can have pointers to incomplete types, but not incomplete types, in function arguments. The compiler needs to know the size of the objects to set up the call/stack properly. It can’t do that with incomplete types, but it can with pointers to such.