/* * testlibpq.c * * Test the C version of libpq, the PostgreSQL frontend * library. */ #include #include #include #include #include #include using namespace std; void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } main() { string pghost, pgport, pgoptions, pgtty; string dbName; string qResult; int nFields; string connInfo; // db connection infor. string userPass; // user/pass infor int i, j; /* FILE *debug; */ PGconn *conn; PGresult *res; /* * begin, by setting the parameters for a backend connection if the * parameters are null, then the system will try to use reasonable * defaults by looking up environment variables or, failing that, * using hardwired constants */ pghost = "host = db.eg.bucknell.edu"; /* host name of the backend server */ pgport = ""; /* port of the backend server */ pgoptions = ""; /* special options to start up the backend * server */ pgtty = ""; /* debugging tty for the backend server */ dbName = "dbname = xmeng"; connInfo = pghost + " " + dbName + " "; cin >> userPass; connInfo += "user = " + userPass + " "; cin >> userPass; connInfo += "password = " + userPass; /* make a connection to the database */ conn = PQconnectdb(connInfo.c_str()); /* conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName); */ /* * check to see that the backend connection was successfully made */ if (PQstatus(conn) == CONNECTION_BAD) { cerr << "Connection to database " << dbName << "failed.\n"; cerr << PQerrorMessage(conn); exit_nicely(conn); } /* debug = fopen("/tmp/trace.out","w"); */ /* PQtrace(conn, debug); */ /* start a transaction block */ res = PQexec(conn, "BEGIN"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { cerr << "BEGIN command failed\n"; PQclear(res); exit_nicely(conn); } /* * should PQclear PGresult whenever it is no longer needed to avoid * memory leaks */ PQclear(res); /* * fetch rows from the pg_database, the system catalog of * databases */ res = PQexec(conn, "DECLARE mycursor CURSOR FOR SELECT * FROM pg_database"); // res = PQexec(conn, "SELECT * FROM xmeng"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { cerr << "DECLARE CURSOR command failed\n"; PQclear(res); exit_nicely(conn); } PQclear(res); res = PQexec(conn, "FETCH ALL in mycursor"); if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) { cerr << "FETCH ALL command didn't return tuples properly\n"; PQclear(res); exit_nicely(conn); } /* first, print out the attribute names */ nFields = PQnfields(res); for (i = 0; i < nFields; i++) cout << PQfname(res, i) << " "; cout << endl << endl; /* next, print out the rows */ for (i = 0; i < PQntuples(res); i++) { for (j = 0; j < nFields; j++) cout << PQgetvalue(res, i, j) << " "; cout << endl; } PQclear(res); /* close the cursor */ res = PQexec(conn, "CLOSE mycursor"); PQclear(res); /* commit the transaction */ res = PQexec(conn, "COMMIT"); PQclear(res); /** * try to create a table **/ /** res = PQexec(conn, "create table xmTestTbl (index integer, name char(10), value real)"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { cerr << "create table failed\n"; PQclear(res); exit_nicely(conn); } **/ /** * try to drop (delete) a table **/ /** res = PQexec(conn, "drop table xmTestTbl"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { cerr << "drop table failed\n"; PQclear(res); exit_nicely(conn); } **/ /** * try to insert a tuple into the table **/ /* res = PQexec(conn, "insert into xmTestTbl values (2, 'first_name', 3.2)"); */ /** res = PQexec(conn, "insert into xmTestTbl values (3, 'second_nam', 4.2)"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { cerr << "insert table 3 failed\n"; PQclear(res); exit_nicely(conn); } res = PQexec(conn, "insert into xmTestTbl values (4, 'third_name', 5.2)"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { cerr << "insert table 4 failed\n"; PQclear(res); exit_nicely(conn); } res = PQexec(conn, "insert into xmTestTbl values (5, 'fourth_nam', 6.2)"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { cerr << "insert table 5 failed\n"; PQclear(res); exit_nicely(conn); } **/ /** res = PQexec(conn, "insert into xmTestTbl values (0, 'fifth_name', 1.2)"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { cerr << "insert table 0 failed\n"; PQclear(res); exit_nicely(conn); } PQclear(res); **/ res = PQexec(conn, "select index from xmTestTbl where index = 3"); qResult = PQgetvalue(res, 0, 0); cout << qResult << endl;; cerr << "command succeeds.\n"; res = PQexec(conn, "select * from xmTestTbl where index >= 3"); /* first, print out the attribute names */ nFields = PQnfields(res); for (i = 0; i < nFields; i++) cout << PQfname(res, i) << " "; cout << endl << endl; /* next, print out the rows */ for (i = 0; i < PQntuples(res); i++) { for (j = 0; j < nFields; j++) cout << PQgetvalue(res, i, j) << " "; cout << endl; } PQclear(res); /* close the connection to the database and cleanup */ PQfinish(conn); /* fclose(debug); */ return 0; }