I have a code that contains some functions unrelated to database. when I added functions for connecting to database, the code occurs Segmentation fault (core dumped)
. but when I use those functions (connecting to db) in an isolated file there is no problem.
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
void exit_db(PGconn *connection){
//this function close the connection
//between progrem and database.
PQfinish(connection);
}
void make_connection(PGconn *connection){
//this function stablish a connection
//between program and database.
connection = PQconnectdb("user=user "\
"password=123 "\
"dbname=project_db");
if(PQstatus(connection) == CONNECTION_BAD){
printf("%s\n", PQerrorMessage(connection));
exit_db(connection);
}
}
void create_table_fp_stores_data(PGresult *re, PGconn *connection){
//this function create fp_stores_data table if does not exist.
re = PQexec(connection ,"CREATE TABLE IF NOT EXISTS fp_stores_data_test (time TIME,"\
"province VARCHAR(20), city VARCHAR(20),"\
"market_id INTEGER );");
if(PQresultStatus(re)==PGRES_COMMAND_OK){
printf("table created!\n");
}else{
printf("%s\n", PQresultErrorMessage(re));
printf("%s\n", PQerrorMessage(connection));
}
PQclear(re);
}
int main(){
PGconn *con;
PGresult *res;
make_connection(con);
create_table_fp_stores_data(res, con);
return 0;
}
this code has no problem but when I add some unrelated functions to this, it occurs problem.I can put my whole code here but I'm trying to avoid congestion.