N
Nicolao
Sorry to use an non-C word like 'paste' but it's the quickest way to
describe my problem. My knowledge of C is clearly incomplete, to say
the least; I'm struggling to find a way to do what I need, and am way
out of my depth.
I'm trying to write a program which retrieves data from a postgreSQL
database (e.g. I enter a word, and the program checks with the database
what part-of-speech it is). I've adapted a script I found and I can
retrieve the data fine, but I don't want to just display it, I want to
store it. That's where I hit the problem, and I don't know enough of C
to solve it, and experimenting with the 2 books I have hasn't given me
a solution.
Obviously the retrieved data (e.g. 'noun') is in a buffer somewhere,
and if this were Filemaker on my Mac I could simply issue a 'paste'
command into an appropriate field. But I can't find a successful way to
assign what's in the buffer to the variable 'part'.
I'll show you what I have, and mark where I reckon the missing move
should be. I'd really appreciate some advice here, and please forgive
the gaping holes in my knowledge - I sure must have missed something!
Incidentally, I'm doing this on a Debian box (woody on a powerpc)
Nic
/* WORDFETCH.C, a program to consult database 'wordpart' */
/* compile like this: gcc -o wordfetch -I/usr/include/postgresql
wordfetch.c -lpq */
#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"
int main()
{
char word[9]; /* holds word entered by user */
char part[12]; /* to hold copied part-of-speech from database */
char query_string[256]; /* holds constructed SQL query */
PGconn *conn; /* holds database connection */
PGresult *res; /* holds query result */
int i;
conn = PQconnectdb("dbname=wordpart"); /* connect to database */
if (PQstatus(conn) == CONNECTION_BAD) /* did connection fail? */
{
fprintf(stderr, "Connection to database failed.\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
exit(1);
}
printf("Type a word: "); /* prompt user for word to be analysed */
scanf("%s", word);
sprintf(query_string, "SELECT part FROM list WHERE word = '%s'",
word); /* create SQL query string */
res = PQexec(conn, query_string); /* sends the query */
if (PQresultStatus(res) != PGRES_TUPLES_OK) /* did the query fail? */
{
fprintf(stderr, "SELECT query failed.\n");
PQclear(res);
PQfinish(conn);
exit(1);
}
for (i = 0; i < PQntuples(res); i++) /* loop through all
rows */
printf("result = %s\n", PQgetvalue(res, i, 0)); /* display the
value returned */
/* HERE IS WHERE I THINK THE ASSIGNMENT SHOULD BE; I want to 'paste'
into 'part' */
PQclear(res); /* free result */
PQfinish(conn); /* disconnect from database */
printf("%s = %s\n", word, part); /* a tester to check */
return 0;
}
describe my problem. My knowledge of C is clearly incomplete, to say
the least; I'm struggling to find a way to do what I need, and am way
out of my depth.
I'm trying to write a program which retrieves data from a postgreSQL
database (e.g. I enter a word, and the program checks with the database
what part-of-speech it is). I've adapted a script I found and I can
retrieve the data fine, but I don't want to just display it, I want to
store it. That's where I hit the problem, and I don't know enough of C
to solve it, and experimenting with the 2 books I have hasn't given me
a solution.
Obviously the retrieved data (e.g. 'noun') is in a buffer somewhere,
and if this were Filemaker on my Mac I could simply issue a 'paste'
command into an appropriate field. But I can't find a successful way to
assign what's in the buffer to the variable 'part'.
I'll show you what I have, and mark where I reckon the missing move
should be. I'd really appreciate some advice here, and please forgive
the gaping holes in my knowledge - I sure must have missed something!
Incidentally, I'm doing this on a Debian box (woody on a powerpc)
Nic
/* WORDFETCH.C, a program to consult database 'wordpart' */
/* compile like this: gcc -o wordfetch -I/usr/include/postgresql
wordfetch.c -lpq */
#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"
int main()
{
char word[9]; /* holds word entered by user */
char part[12]; /* to hold copied part-of-speech from database */
char query_string[256]; /* holds constructed SQL query */
PGconn *conn; /* holds database connection */
PGresult *res; /* holds query result */
int i;
conn = PQconnectdb("dbname=wordpart"); /* connect to database */
if (PQstatus(conn) == CONNECTION_BAD) /* did connection fail? */
{
fprintf(stderr, "Connection to database failed.\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
exit(1);
}
printf("Type a word: "); /* prompt user for word to be analysed */
scanf("%s", word);
sprintf(query_string, "SELECT part FROM list WHERE word = '%s'",
word); /* create SQL query string */
res = PQexec(conn, query_string); /* sends the query */
if (PQresultStatus(res) != PGRES_TUPLES_OK) /* did the query fail? */
{
fprintf(stderr, "SELECT query failed.\n");
PQclear(res);
PQfinish(conn);
exit(1);
}
for (i = 0; i < PQntuples(res); i++) /* loop through all
rows */
printf("result = %s\n", PQgetvalue(res, i, 0)); /* display the
value returned */
/* HERE IS WHERE I THINK THE ASSIGNMENT SHOULD BE; I want to 'paste'
into 'part' */
PQclear(res); /* free result */
PQfinish(conn); /* disconnect from database */
printf("%s = %s\n", word, part); /* a tester to check */
return 0;
}