diff --git a/insert/main.c b/insert/main.c new file mode 100644 index 0000000..4e92df7 --- /dev/null +++ b/insert/main.c @@ -0,0 +1,71 @@ +#include +#include +#include + +void do_exit(PGconn *conn, PGresult *res) { + fprintf(stderr, "%s\n", PQerrorMessage(conn)); + /*every statement result should be freed*/ + PQclear(res); + /*close connection and free memory*/ + PQfinish(conn); + exit(EXIT_FAILURE); +} + +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{ + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + /*connect to database*/ + PGconn *conn = PQconnectdb(conninfo); + if (PQstatus(conn) == CONNECTION_BAD) { + fprintf(stderr, "Connection to database failed: %s\n", + PQerrorMessage(conn)); + PQfinish(conn); + exit(1); + }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); + + /*submit SQL statement to the server and wait for the result*/ + PGresult *res = PQexec(conn, "INSERT INTO entities (name) VALUES('test entity name');"); + if (PQresultStatus(res) != PGRES_COMMAND_OK){ + do_exit(conn, res); + }else{ + printf("main() record inserted into database table\n"); + } + /*every statement result should be freed*/ + PQclear(res); + + res = PQexec(conn, "INSERT INTO entities (name) VALUES('test entity name next');"); + if (PQresultStatus(res) != PGRES_COMMAND_OK) { + do_exit(conn, res); + }else{ + printf("main() record inserted into database table\n"); + } + /*every statement result should be freed*/ + PQclear(res); + + PQfinish(conn); + return 0; +} + diff --git a/insert/makefile b/insert/makefile new file mode 100644 index 0000000..b14baca --- /dev/null +++ b/insert/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/insert/makefile-fine b/insert/makefile-fine new file mode 100644 index 0000000..616581c --- /dev/null +++ b/insert/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/insert/makefile-fine2 b/insert/makefile-fine2 new file mode 100644 index 0000000..b14baca --- /dev/null +++ b/insert/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/insert/readme.md b/insert/readme.md new file mode 100644 index 0000000..3289c59 --- /dev/null +++ b/insert/readme.md @@ -0,0 +1,23 @@ +* build + +``` +make +``` + +* run + +``` +./main 'postgresql://:@:/' +``` + +* or run + +``` +./main 'postgresql://:@:/' +``` + +* or run + +``` +./main "host= port= user= password=<> dbname=" +```