You are viewing a plain text version of this content. The canonical link for it is here.
Posted to embperl@perl.apache.org by Mathias Hennersdorf <ma...@web.de> on 2003/05/15 14:33:34 UTC
Perl + C + DBI
Hi!
I`ve a big problem:
I `ve written 2 scripts, one in perl (db_connect.pl) and one in c
(connect.c).
The C-Script shoul connect to a DB by the Perlskript.
But i doesn`t get any result.
Greetings
Mathias
Here the scripts, every idea is welcome:
connect.c
#include <EXTERN.h>
#include </usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE/perl.h>
#include </usr/include/stdio.h>
#ifdef __cplusplus
# define EXTERN_C extern "C"
#else
# define EXTERN_C extern
#endif
static PerlInterpreter *my_perl;
static void xs_init (pTHX);
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
EXTERN_C void
xs_init(pTHX)
{
char *file = __FILE__;
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
}
PerlOpenDB(char *db_name)
{
int a;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(db_name,a)));
PUTBACK;
call_pv("db_connect", G_SCALAR);
SPAGAIN;
printf("db open: %d wurde %d geöffnet!\n",db_name,POPi);
PUTBACK;
FREETMPS;
LEAVE;
}
int main (int argc, char **argv, char **env)
{
char *loader[] = { "", "db_connect.pl" };
my_perl = perl_alloc();
perl_construct( my_perl );
perl_parse(my_perl, xs_init, argc, loader, NULL);
//perl_parse(my_perl, NULL, 2, my_argv, (char **)NULL);
perl_run(my_perl);
PerlOpenDB("testdb");
perl_destruct(my_perl);
perl_free(my_perl);
}
the perl-script:
#!/usr/bin/perl -w
use DBI;
sub db_open_pg
{
$data_source_pgsql="dbi:Pg:dbname=$db_name";
$dbuser="postgres";
$dbpass="";
$dbh_pgsql = DBI -> connect ($data_source_pgsql, $dbuser, $dbpass );
print "Errors: $DBI::err, $DBI::errstr\n" if ($DBI::err || $DBI::errstr);
if (! defined $dbh_pgsql)
{
print "No connection to:$db_name: $DBI::errstr\n";
$opendb=0;
}
else
{
print "Connect to $db_name !\n";
$opendb=1;
}
return $opendb;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: Perl + C + DBI
Posted by Richard Schilling <rs...@nationalinformatics.net>.
I am not familiar enough with the syntax for invoking PERL through a
C/C++ program to comment on that.
But, in your PERL script you have the following:
if (! defined $dbh_pgsql)
{
print "No connection to:$db_name: $DBI::errstr\n";
$opendb=0;
}
else
{
print "Connect to $db_name !\n";
$opendb=1;
}
You might, just for fun, try to change the if statement:
if ( !defined($dbh_pgsql))
{
. . .
Notice I put "!" right next to define and also used paranthesis to call
the define function call. Perhaps the other form you had works too but
I always get a good result when I use the form I'm suggesting.
Good luck.
--Richard Schilling
On 2003.05.15 05:33 Mathias Hennersdorf wrote:
> Hi!
>
> I`ve a big problem:
> I `ve written 2 scripts, one in perl (db_connect.pl) and one in c
> (connect.c).
> The C-Script shoul connect to a DB by the Perlskript.
> But i doesn`t get any result.
>
> Greetings
>
> Mathias
>
> Here the scripts, every idea is welcome:
>
> connect.c
>
> #include <EXTERN.h>
> #include </usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE/perl.h>
> #include </usr/include/stdio.h>
>
> #ifdef __cplusplus
> # define EXTERN_C extern "C"
> #else
> # define EXTERN_C extern
> #endif
>
> static PerlInterpreter *my_perl;
>
> static void xs_init (pTHX);
>
> EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
>
> EXTERN_C void
> xs_init(pTHX)
> {
> char *file = __FILE__;
> newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
> }
>
> PerlOpenDB(char *db_name)
> {
> int a;
> dSP;
> ENTER;
> SAVETMPS;
> PUSHMARK(SP);
> XPUSHs(sv_2mortal(newSVpv(db_name,a)));
>
> PUTBACK;
> call_pv("db_connect", G_SCALAR);
> SPAGAIN;
> printf("db open: %d wurde %d geöffnet!\n",db_name,POPi);
> PUTBACK;
> FREETMPS;
> LEAVE;
> }
>
> int main (int argc, char **argv, char **env)
> {
> char *loader[] = { "", "db_connect.pl" };
>
> my_perl = perl_alloc();
> perl_construct( my_perl );
>
> perl_parse(my_perl, xs_init, argc, loader, NULL);
> //perl_parse(my_perl, NULL, 2, my_argv, (char **)NULL);
> perl_run(my_perl);
>
> PerlOpenDB("testdb");
>
> perl_destruct(my_perl);
> perl_free(my_perl);
> }
>
>
> the perl-script:
>
>
> #!/usr/bin/perl -w
>
> use DBI;
>
> sub db_open_pg
> {
> $data_source_pgsql="dbi:Pg:dbname=$db_name";
>
> $dbuser="postgres";
> $dbpass="";
>
>
> $dbh_pgsql = DBI -> connect ($data_source_pgsql, $dbuser, $dbpass );
>
> print "Errors: $DBI::err, $DBI::errstr\n" if ($DBI::err ||
> $DBI::errstr);
> if (! defined $dbh_pgsql)
> {
> print "No connection to:$db_name: $DBI::errstr\n";
> $opendb=0;
>
> }
> else
> {
> print "Connect to $db_name !\n";
> $opendb=1;
> }
>
> return $opendb;
> }
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
> For additional commands, e-mail: embperl-help@perl.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org