Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <libpq-fe.h>
- void doSQL(PGconn *conn, char *command)
- {
- PGresult *result;
- printf("%s\n", command);
- result = PQexec(conn, command);
- printf("status is : %s\n", PQresSrarus(PQresultStatus(result)));
- printf("#rows aff : %s\n", PQcmdTuples(result));
- printf("result msg : %s\n", PQresultErrorMessage(result));
- switch(PQresultStatus(result)){
- case PGRES_TUPLES_OK:
- {
- int n=0, m=0;
- int nrows = PQntuples(result);
- int nfields = PQnfields(result);
- printf("number of rows returned = %d\n", nrows);
- printf("number of fields returned = %d\n", nfields);
- for(m=0; m < nrows; m++){
- for(n=0, n < nfields; n++) printf("%s = %s", PQfname(result, n),PQgetvalue(result,m,n));
- printf("\n");
- }
- }
- }
- PQclear(result);
- }
- void HTMLprint(PGconn * conn, char *tabela, char *out)
- {
- PGresult *result;
- char polec[150];
- sprintf(polec,"SELECT * FROM %s",tabela);
- result = PQexec(conn, polec);
- switch(PQresultStatus(result))
- {
- case PGRES_TUPLES_OK:
- {
- int n = 0, m = 0;
- int nrows = PQntuples(result); //wiersze
- int nfields = PQnfields(result); //kolumny
- FILE * druk = fopen(out, "w+"); //plik
- fprintf(druk,"<!DOCTYPE html> \n <html lang=\"pl\"> \n <head> \n <meta charset=\"utf-8\"> \n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> \n <title>Dane z tabel</title> \n <style> \n"); //header
- //tutaj style:
- fprintf(druk,"table,td,th%c border: 1px solid black%c \n table %cborder-collapse: collapse%c width: 100%c \n th%c text-align: left%c \n </style></head>\n",123,125,123,59,125,123,125); //znaki specjalne c w ascii
- fprintf(druk,"<body> \n <table> \n");
- fprintf(druk,"<tr>\n");
- for(n=0;n<nfields;n++)
- fprintf(druk,"<th>%s</th>\n",PQfname(result, n));
- fprintf(druk,"</tr>\n");
- for(m = 0; m < nrows; m++)
- {
- fprintf(druk,"<tr>\n");
- for(n = 0; n < nfields; n++)
- fprintf(druk,"<th>%s</th>\n",PQgetvalue(result,m,n));
- fprintf(druk,"</tr>\n");
- }
- fprintf(druk,"</table> \n </body> \n </html>");
- fclose(druk);
- }
- }
- printf("\n");
- PQclear(result);
- }
- int main(){
- PGresult *result;
- PGconn *conn = PQconnectdb("host=localhost port=5432 dbname=siusiak user=admin password=admin");
- if(PQstatus(conn) == CONNECTION_OK){
- printf("connection made\n");
- doSQL(conn, "DROP TABLE koledzy");
- doSQL(conn, "CREATE TABLE koledzy(value INTEGER PRIMARY KEY, name VARCHAR(20), surname VARCHAR(20), age VARCHAR(20), gender VARCHAR(20), is_intelligent VARCHAR(20) )");
- doSQL(conn, "INSERT INTO koledzy values(1, 'Damian', 'Seifert', '20', 'carp', 'yes')");
- doSQL(conn, "INSERT INTO koledzy values(2, 'Domniki', 'Banulski', '20', 'golem', 'no')");
- doSQL(conn, "INSERT INTO koledzy values(3 'Ddasdn', 'Sasddasrt', '20', 'sadrp', 'yes')");
- doSQL(conn, "SELECT * FROM koledzy");
- doSQL(conn, "UPDATE koledzy SET name = WHERE value = 3");
- doSQL(conn, "SELECT * FROM koledzy");
- HTMLprint(conn, 'koledzy', 'siusiak.html');
- } else printf("connection failed: %s\n", PQerrorMessage(conn));
- PQfinish(conn);
- return EXIT_SUCCES;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement