From 266a9f8937745f51a5a80e45bfd7a25304d13572 Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Thu, 5 Jan 2023 14:49:04 +0100 Subject: [PATCH] feat: add simple_query --- simple_query/main.c | 59 +++++++++++++++++++++++++++++++++++++ simple_query/makefile | 24 +++++++++++++++ simple_query/makefile-fine | 21 +++++++++++++ simple_query/makefile-fine2 | 24 +++++++++++++++ simple_query/readme.md | 23 +++++++++++++++ 5 files changed, 151 insertions(+) create mode 100644 simple_query/main.c create mode 100644 simple_query/makefile create mode 100644 simple_query/makefile-fine create mode 100644 simple_query/makefile-fine2 create mode 100644 simple_query/readme.md diff --git a/simple_query/main.c b/simple_query/main.c new file mode 100644 index 0000000..3002479 --- /dev/null +++ b/simple_query/main.c @@ -0,0 +1,59 @@ +#include +#include +#include + +void do_exit(PGconn *conn) { + PQfinish(conn); + exit(1); +} + +int main(int argc, char **argv) { + /*declaration*/ + const char *conninfo; + + printf("main() Started...\n"); + + /*read command line*/ + if (argc > 1){ + printf("main() argv[1]: %s\n", argv[1]); + conninfo = argv[1]; + }else{ + printf("call ./main \n"); + return 1; + } + + /*connect to database*/ + PGconn *conn = PQconnectdb(conninfo); + + if (PQstatus(conn) == CONNECTION_BAD) { + fprintf(stderr, "Connection to database failed: %s\n", + PQerrorMessage(conn)); + do_exit(conn); + }else if(PQstatus(conn)==CONNECTION_OK){ + printf("main() connected to database\n"); + }else{ + printf("main() connection status NOT known\n"); + } + + char *user = PQuser(conn); + char *db_name = PQdb(conn); + char *pswd = PQpass(conn); + printf("User: %s\n", user); + printf("Database name: %s\n", db_name); + printf("Password: %s\n", pswd); + + /*retrive server of database using SQL statement*/ + /*PGRES_TUPLS_OK is returned for a query that returns data*/ + PGresult *res = PQexec(conn, "SELECT VERSION()"); + if (PQresultStatus(res) != PGRES_TUPLES_OK) { + printf("No data retrieved\n"); + PQclear(res); + do_exit(conn); + } + + /*return a single field value of one row*/ + printf("%s\n", PQgetvalue(res, 0, 0)); + PQclear(res); + PQfinish(conn); + return 0; +} diff --git a/simple_query/makefile b/simple_query/makefile new file mode 100644 index 0000000..b14baca --- /dev/null +++ b/simple_query/makefile @@ -0,0 +1,24 @@ +# Docu +# +#$@ is the name of the file to be made +#$? is the names of the changed dependents +#$< the name of the related file that caused the action +#$* the prefix shared by target and dependent files +# +# Others +RM = /bin/rm -f +# +# Source, Executable, Includes, Library Defines +EXE = main +# +# Compiler, Linker Defines +CC = /usr/bin/gcc +# +all: main.o + $(CC) main.c -I/usr/include/postgresql -L/usr/lib/x86_64-linux-gnu -lpq -std=c99 -Wall -o $(EXE) +# +main.o: + $(CC) -c main.c -I/usr/include/postgresql +# Clean Up Objects, Exectuables, Dumps out of source directory +clean: + $(RM) *.o $(EXE) *~ diff --git a/simple_query/makefile-fine b/simple_query/makefile-fine new file mode 100644 index 0000000..616581c --- /dev/null +++ b/simple_query/makefile-fine @@ -0,0 +1,21 @@ +# Docu +# +#$@ is the name of the file to be made +#$? is the names of the changed dependents +#$< the name of the related file that caused the action +#$* the prefix shared by target and dependent files +# +# Others +RM = /bin/rm -f +# +# Source, Executable, Includes, Library Defines +EXE = main +# +# Compiler, Linker Defines +CC = /usr/bin/gcc +# +all: + $(CC) main.c -I/usr/include/postgresql -L/usr/lib/x86_64-linux-gnu -lpq -std=c99 -Wall -o $(EXE) +# Clean Up Objects, Exectuables, Dumps out of source directory +clean: + $(RM) *.o $(EXE) *~ diff --git a/simple_query/makefile-fine2 b/simple_query/makefile-fine2 new file mode 100644 index 0000000..b14baca --- /dev/null +++ b/simple_query/makefile-fine2 @@ -0,0 +1,24 @@ +# Docu +# +#$@ is the name of the file to be made +#$? is the names of the changed dependents +#$< the name of the related file that caused the action +#$* the prefix shared by target and dependent files +# +# Others +RM = /bin/rm -f +# +# Source, Executable, Includes, Library Defines +EXE = main +# +# Compiler, Linker Defines +CC = /usr/bin/gcc +# +all: main.o + $(CC) main.c -I/usr/include/postgresql -L/usr/lib/x86_64-linux-gnu -lpq -std=c99 -Wall -o $(EXE) +# +main.o: + $(CC) -c main.c -I/usr/include/postgresql +# Clean Up Objects, Exectuables, Dumps out of source directory +clean: + $(RM) *.o $(EXE) *~ diff --git a/simple_query/readme.md b/simple_query/readme.md new file mode 100644 index 0000000..a0dc22d --- /dev/null +++ b/simple_query/readme.md @@ -0,0 +1,23 @@ +* build + +``` +make +``` + +* run + +``` +./main 'postgresql://begerad:secret@localhost:5432/sib00_vbn_gtfs' +``` + +* or run + +``` +./main 'postgresql://begerad:secret@localhost:5432/vbn_data' +``` + +* or run + +``` +./main "host=localhost port=5432 user=begerad password= dbname=vbn_data" +``` \ No newline at end of file