无法连接到c中的数据库
Can't connect to database in c
我有一段代码包含一些与数据库无关的函数。当我添加用于连接到数据库的函数时,代码出现 Segmentation fault (core dumped)
。但是当我在一个独立的文件中使用这些函数(连接到数据库)时没有问题。
#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;
}
这段代码没有问题,但是当我向它添加一些不相关的函数时,它发生了problem.I可以把我的整个代码放在这里,但我试图避免拥塞。
问题出在
的定义上
void make_connection(PGconn *connection)
PGconn *
是按值传递的,因此函数体内的 connection
与 main
函数中的 con
是不同的变量。
您在 make_connection
中分配给 connection
,但这不会更改 con
的值。
有两种解决方法:
传递con
的地址:
void make_connection(PGconn **connection) {
*connection = PQconnectdb(...);
}
make_connection(&con);
(更好)有函数return指针:
PGconn *make_connection() {
PGconn *result;
result = PQconnectdb(...);
return result;
}
con = make_connection();
我有一段代码包含一些与数据库无关的函数。当我添加用于连接到数据库的函数时,代码出现 Segmentation fault (core dumped)
。但是当我在一个独立的文件中使用这些函数(连接到数据库)时没有问题。
#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;
}
这段代码没有问题,但是当我向它添加一些不相关的函数时,它发生了problem.I可以把我的整个代码放在这里,但我试图避免拥塞。
问题出在
的定义上void make_connection(PGconn *connection)
PGconn *
是按值传递的,因此函数体内的 connection
与 main
函数中的 con
是不同的变量。
您在 make_connection
中分配给 connection
,但这不会更改 con
的值。
有两种解决方法:
传递
con
的地址:void make_connection(PGconn **connection) { *connection = PQconnectdb(...); } make_connection(&con);
(更好)有函数return指针:
PGconn *make_connection() { PGconn *result; result = PQconnectdb(...); return result; } con = make_connection();