/* * testlibpq.c * * Test the C version of libpq, the PostgreSQL frontend * library. */ #include #include void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } main() { char *pghost, *pgport, *pgoptions, *pgtty; char *dbName; char *qResult; int nFields; char connInfo[128]; // db connection infor. char userPass[128]; // 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 = NULL; /* port of the backend server */ pgoptions = NULL; /* special options to start up the backend * server */ pgtty = NULL; /* debugging tty for the backend server */ dbName = "dbname = xmeng"; strcpy(connInfo, pghost); strcat(connInfo, " "); strcat(connInfo, dbName); strcat(connInfo, " "); scanf("%s", userPass); // user name first strcat(connInfo, "user = "); strcat(connInfo, userPass); strcat(connInfo, " "); scanf("%s", userPass); // now password strcat(connInfo, "password = "); strcat(connInfo, userPass); /* make a connection to the database */ conn = PQconnectdb(connInfo); /* conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName); */ /* * check to see that the backend connection was successfully made */ if (PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr, "Connection to database '%s' failed.\n", dbName); fprintf(stderr, "%s", 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) { fprintf(stderr, "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) { fprintf(stderr, "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) { fprintf(stderr, "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++) printf("%-15s", PQfname(res, i)); printf("\n\n"); /* next, print out the rows */ for (i = 0; i < PQntuples(res); i++) { for (j = 0; j < nFields; j++) printf("%-15s", PQgetvalue(res, i, j)); printf("\n"); } 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) { fprintf(stderr, "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) { fprintf(stderr, "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) { fprintf(stderr, "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) { fprintf(stderr, "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) { fprintf(stderr, "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) { fprintf(stderr, "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); printf("%s\n", qResult); fprintf(stderr, "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++) printf("%-15s", PQfname(res, i)); printf("\n\n"); /* next, print out the rows */ for (i = 0; i < PQntuples(res); i++) { for (j = 0; j < nFields; j++) printf("%-15s", PQgetvalue(res, i, j)); printf("\n"); } PQclear(res); /* close the connection to the database and cleanup */ PQfinish(conn); /* fclose(debug); */ return 0; }