feat(connect): initial commit
This commit is contained in:
parent
de21aa2788
commit
ae480ca42a
|
@ -0,0 +1,60 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <libpq-fe.h>
|
||||||
|
|
||||||
|
int pg_connect(char* conninfo, PGconn** conn)
|
||||||
|
{
|
||||||
|
|
||||||
|
*conn = PQconnectdb(conninfo);
|
||||||
|
if (PQstatus(*conn) != CONNECTION_OK) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"ERROR: Connection to database failed: %s",
|
||||||
|
PQerrorMessage(*conn));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* set correctely your values here */
|
||||||
|
#define PG_HOST "127.0.0.1"
|
||||||
|
//#define PG_USER "postgres"
|
||||||
|
//#define PG_DB "postgres"
|
||||||
|
//#define PG_PASS "postgres"
|
||||||
|
//#define PG_PORT 5432
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
/*declarations*/
|
||||||
|
char conninfo[250];
|
||||||
|
PGconn *conn = NULL;
|
||||||
|
|
||||||
|
sprintf(conninfo,
|
||||||
|
"user=%s password=%s dbname=%s hostaddr=%s port=%d",
|
||||||
|
PG_USER, PG_PASS, PG_DB, PG_HOST, PG_PORT);
|
||||||
|
printf("main() conninfo: %s\n",conninfo);
|
||||||
|
|
||||||
|
if (!pg_connect(conninfo, &conn)) {
|
||||||
|
printf("main() go to end\n");
|
||||||
|
goto end;
|
||||||
|
}else{
|
||||||
|
printf("main() go NOT to end\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Here fit your staff
|
||||||
|
*/
|
||||||
|
int isThreadSafe=0;
|
||||||
|
printf("main() Started...\n");
|
||||||
|
isThreadSafe=PQisthreadsafe();
|
||||||
|
if(isThreadSafe==1){
|
||||||
|
printf("main() libpq is thread-safe\n");
|
||||||
|
}else{
|
||||||
|
printf("main() libpq is NOT thread-safe\n");
|
||||||
|
}
|
||||||
|
printf("main() Done.\n");
|
||||||
|
|
||||||
|
end:
|
||||||
|
PQfinish(conn);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
|
@ -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) *~
|
|
@ -0,0 +1,19 @@
|
||||||
|
* install dependencies on Debian Bullseye
|
||||||
|
```
|
||||||
|
sudo apt install libpq-dev --no-install-recommends
|
||||||
|
```
|
||||||
|
|
||||||
|
* build
|
||||||
|
```
|
||||||
|
make
|
||||||
|
```
|
||||||
|
|
||||||
|
* run
|
||||||
|
```
|
||||||
|
./main 'postgresql://<user>:<secret>@<host>:<port>/<database>'
|
||||||
|
```
|
||||||
|
|
||||||
|
* or run
|
||||||
|
```
|
||||||
|
./main "host=<host> port=<port> user=<user> password=<<secret>> dbname=<database>"
|
||||||
|
```
|
Loading…
Reference in New Issue