Thread: Embed SQL in C

Embed SQL in C

From
GMX
Date:
Hi,

I am new to SQL and I am only a moderate C-programmer, so I need help to
solve the following problem:

I need a C-program that goes something like this:

1) Check how much devices are in the database and look up the IP and
the Port from them from the database.

2) Connect to the computer using the IP and the Port and send a
certain string to this computer.

[the computer echoes to this certain string with another string that
contains 3 values]

3) The received string should be split and each value should be
written in the right field of the SQL database.

I hope you can help me a little bit.


Re: Embed SQL in C

From
Michael Fuhr
Date:
On Wed, Mar 02, 2005 at 06:46:10PM +0100, GMX wrote:

> I need a C-program that goes something like this:

Why the requirement to use C?  Many tasks are easier in and can be
adequately written with higher-level languages like Perl, Python,
Ruby, Tcl, etc.

> 1) Check how much devices are in the database and look up the IP and
> the Port from them from the database.

Make an appropriate SELECT query against the table that contains
this information.  If you need help with SQL in general or with
PostgreSQL in particular, see the Tutorial and other parts of the
the PostgreSQL documentation.

http://www.postgresql.org/docs/

> 2) Connect to the computer using the IP and the Port and send a
> certain string to this computer.

This isn't a PostgreSQL issue.  Use an appropriate library/module
or write the socket code yourself.  The socket programming bible
is _UNIX Network Programming_, Volume 1, by W. Richard Stevens.

> [the computer echoes to this certain string with another string that
> contains 3 values]
>
> 3) The received string should be split and each value should be
> written in the right field of the SQL database.

Split the string using whatever facilities your programming language
provides and do an INSERT or UPDATE into the desired table.  PostgreSQL
also has some string-handling functions -- see "String Functions and
Operators" in the "Functions and Operators" chapter in the documentation.

If you're having a specific problem, then please post what you've
tried, explain what you'd like to happen, and describe what actually
does happen, including the exact text of any errors or warnings.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

Re: Embed SQL in C

From
Michael Fuhr
Date:
On Sun, Mar 06, 2005 at 09:08:07PM -0700, Michael Fuhr wrote:
> On Wed, Mar 02, 2005 at 06:46:10PM +0100, GMX wrote:
>
> > I need a C-program that goes something like this:
>
> Why the requirement to use C?  Many tasks are easier in and can be
> adequately written with higher-level languages like Perl, Python,
> Ruby, Tcl, etc.

If you must use C, see the libpq or ECPG documentation.  Here are
links to the relevant chapters for the latest version of PostgreSQL:

http://www.postgresql.org/docs/8.0/interactive/libpq.html
http://www.postgresql.org/docs/8.0/interactive/ecpg.html

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/