Обсуждение: Postgres' DBI(Pg) problems.

Поиск
Список
Период
Сортировка

Postgres' DBI(Pg) problems.

От
Kevin Lo
Дата:
Hi,

I'm trying to use Postgres DBI, which is Pg, to write a simple cgi script.
But I encounter a problem, hope anyone can tell me how to do, thanks.

For example:
I have two input forms in ex.html, which names are 'id' and 'student'.
I want to insert them into 'course' table in student database by using Pg.
The script I wrote as following:

#!/usr/local/bin/perl
require 'forms-lib.pl';/* this script gets form values in HTML that I input */
use DBI;
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>Data inserted</TITLE>\n";
print "</HEAD>\n";
%input = &GetFormInput();
$v1 = $input{'id'};
$v2 = $input{'student'};
$dbh = DBI->connect("dbi:Pg:dbname=student") or die $DBI::errstr;
$sth = $dbh->prepare("insert into course values('$v1', '$v2')")
     or die $DBI::errstr;
$sth->execute or  die $DBI::errstr;
print "<BODY>\n";
print "<H1>Data inserted!</H1>\n";
print "</BODY>\n";
print "</HTML>\n";

After executing this script, I just see brower's title "Data inserted",
I can't see "Data inserted!" in HTML and can't insert values into 'course'
table. I don't know how to do, would anyone tell me, thanks in advance.

Best regards,
Kevin.


[GENERAL] Postgres' DBI(Pg) problems.

От
"R\imi Lehn"
Дата:
    Your script worked for me (I skipped everything related to
forms-lib), but :

- /* xxxx */ C-style comments are not allowed by perl (line #2)
- you should $sth->finish() and $dbh->disconnect() at the end of your
  script to avoid warnings ("Database handle destroyed without
  explicit disconnect.")

    I assumed that your table course has two fields, both are
type text. Check that your fields are not numbers (inserted values
are quoted (line #13)).

    You can replace $sth->prepare() and $sth->execute() by a
$sth->do( "your query" ); as your query has no output row.

    Check your web server's logs. At least for apache, stderr
of cgi scripts is dumped in an error log. Try running your script
offline. A common mistake is not having access to your database
with the uid endorsed by your web server (apache usually runs
cgi scripts as nobody which may not have INSERT permissions on
the table).

Hope it will help.
Rémi.

Kevin Lo writes:
 > Hi,
 >
 > I'm trying to use Postgres DBI, which is Pg, to write a simple cgi script.
 > But I encounter a problem, hope anyone can tell me how to do, thanks.
 >
 > For example:
 > I have two input forms in ex.html, which names are 'id' and 'student'.
 > I want to insert them into 'course' table in student database by using Pg.
 > The script I wrote as following:
 >
 > #!/usr/local/bin/perl
 > require 'forms-lib.pl';/* this script gets form values in HTML that I input */
 > use DBI;
 > print "Content-type: text/html\n\n";
 > print "<HTML>\n";
 > print "<HEAD>\n";
 > print "<TITLE>Data inserted</TITLE>\n";
 > print "</HEAD>\n";
 > %input = &GetFormInput();
 > $v1 = $input{'id'};
 > $v2 = $input{'student'};
 > $dbh = DBI->connect("dbi:Pg:dbname=student") or die $DBI::errstr;
 > $sth = $dbh->prepare("insert into course values('$v1', '$v2')")
 >      or die $DBI::errstr;
 > $sth->execute or  die $DBI::errstr;
 > print "<BODY>\n";
 > print "<H1>Data inserted!</H1>\n";
 > print "</BODY>\n";
 > print "</HTML>\n";
 >
 > After executing this script, I just see brower's title "Data inserted",
 > I can't see "Data inserted!" in HTML and can't insert values into 'course'
 > table. I don't know how to do, would anyone tell me, thanks in advance.
 >
 > Best regards,
 > Kevin.
 >
 >

Re: [GENERAL] Postgres' DBI(Pg) problems.

От
Kevin Lo
Дата:
Rimi Lehn wrote:

>         Your script worked for me (I skipped everything related to
> forms-lib), but :
>
> - /* xxxx */ C-style comments are not allowed by perl (line #2)
> - you should $sth->finish() and $dbh->disconnect() at the end of your
>   script to avoid warnings ("Database handle destroyed without
>   explicit disconnect.")

OK, added.

>         I assumed that your table course has two fields, both are
> type text. Check that your fields are not numbers (inserted values
> are quoted (line #13)).
>
>         You can replace $sth->prepare() and $sth->execute() by a
> $sth->do( "your query" ); as your query has no output row.
>
>         Check your web server's logs. At least for apache, stderr
> of cgi scripts is dumped in an error log. Try running your script
> offline. A common mistake is not having access to your database
> with the uid endorsed by your web server (apache usually runs
> cgi scripts as nobody which may not have INSERT permissions on
> the table).

I checked the log file of Apache server, I got an error message:

[Mon Jan 18 18:12:01 1999] [notice] httpd: caught SIGTERM, shutting down
[Mon Jan 18 21:48:28 1999] [notice] Apache/1.3.1 (Unix) configured -- resuming
normal operations
install_driver(Pg) failed: Can't load
'/usr/local/lib/perl5/site_perl/i386-freebsd/auto/DBD/Pg/Pg.so' for module DBD::Pg:
Can't find shared library "libpq.so.2.0" at
/usr/local/lib/perl5/i386-freebsd/5.00404/DynaLoader.pm line 166.

 at (eval 1) line 2

 at /home/kevin/public_html/example.pl line 14

I've already set enviroment variables PGLIB and LD_LIBRARY_PAHT in .login.
libpq.so.2.0 is in /usr/local/pgsql/lib directory. I don't install mod_perl with
Apache.
Should I install mod_perl?  Would you tell me how to solve this problem, thanks.

Best regards,
Kevin.


Re: [GENERAL] Postgres' DBI(Pg) problems.

От
Remi Lehn
Дата:
As your perl script is run by apache, LD_LIBRARY_PATH and PGLIB may
be defined *before* apache starting. If apache is run by a rc script,
define LD_LIBRARY_PATH and PGLIB in the rc script before apache is
started.

You do not need mod_perl for this script. mod_perl gives you the
benefits of having a persistant perl vm staying alive between the
execution of your cgi scripts. It will speedup things because you
can maintain a persistant state (your perl code compiled, your
database connections, ...) between executions of your scripts.

Rémi.

Kevin Lo writes:
 > Rimi Lehn wrote:
 >
 > >         Your script worked for me (I skipped everything related to
 > > forms-lib), but :
 > >
 > > - /* xxxx */ C-style comments are not allowed by perl (line #2)
 > > - you should $sth->finish() and $dbh->disconnect() at the end of your
 > >   script to avoid warnings ("Database handle destroyed without
 > >   explicit disconnect.")
 >
 > OK, added.
 >
 > >         I assumed that your table course has two fields, both are
 > > type text. Check that your fields are not numbers (inserted values
 > > are quoted (line #13)).
 > >
 > >         You can replace $sth->prepare() and $sth->execute() by a
 > > $sth->do( "your query" ); as your query has no output row.
 > >
 > >         Check your web server's logs. At least for apache, stderr
 > > of cgi scripts is dumped in an error log. Try running your script
 > > offline. A common mistake is not having access to your database
 > > with the uid endorsed by your web server (apache usually runs
 > > cgi scripts as nobody which may not have INSERT permissions on
 > > the table).
 >
 > I checked the log file of Apache server, I got an error message:
 >
 > [Mon Jan 18 18:12:01 1999] [notice] httpd: caught SIGTERM, shutting down
 > [Mon Jan 18 21:48:28 1999] [notice] Apache/1.3.1 (Unix) configured -- resuming
 > normal operations
 > install_driver(Pg) failed: Can't load
 > '/usr/local/lib/perl5/site_perl/i386-freebsd/auto/DBD/Pg/Pg.so' for module DBD::Pg:
 > Can't find shared library "libpq.so.2.0" at
 > /usr/local/lib/perl5/i386-freebsd/5.00404/DynaLoader.pm line 166.
 >
 >  at (eval 1) line 2
 >
 >  at /home/kevin/public_html/example.pl line 14
 >
 > I've already set enviroment variables PGLIB and LD_LIBRARY_PAHT in .login.
 > libpq.so.2.0 is in /usr/local/pgsql/lib directory. I don't install mod_perl with
 > Apache.
 > Should I install mod_perl?  Would you tell me how to solve this problem, thanks.
 >
 > Best regards,
 > Kevin.
 >