我正在尝试为查询值的输出分配一个字符串。我正在使用PostgreSQL执行命令,我的代码如下:
int finding_string(int id_subject){
char STRING[255];
PGconn *conn;
PGresult *res;
conn = PQconnectdb("dbname=username host=host_address user=username password=password");
char o[255];
sprintf(o, "SELECT credits FROM subject WHERE id_subject = %d;", id_subject);
printf("%s\n", o);
res = PQexec(conn, o);
sprintf(STRING, "%s", PQgetvalue(res, 0, 0));
printf("%s \n", STRING);
因此,重点放在第sprintf(p, "SELECT average_grade FROM student WHERE student_code = %d;", student_code);
行,在该行我得到查询的输出。该命令效果很好,但是当我尝试使用sprintf(STRING, "%s", PQgetvalue(res, 0, 0));
时,程序在该行中崩溃,并且不打印字符串。
谁能告诉我可能是什么问题,并提供解决方案,以便我可以将查询的输出分配给字符串?
当我开始失去希望时,我们会提供任何帮助。