Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
postgres + perl help...
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Off the Wall
View previous topic :: View next topic  
Author Message
atrus123
Guru
Guru


Joined: 06 Jul 2005
Posts: 339
Location: Annapolis, MD

PostPosted: Thu Mar 15, 2007 1:21 am    Post subject: postgres + perl help... Reply with quote

Forgive my obvious n00bness, but I've been researching this for a while, and I just can't figure it out.

Is there a simple way to take a statement like this:
Code:

my $dbh = DBI->connect("dbi:Pg:dbname=$dbase","$uid","$pw");


and have Perl create the database if it doesn't already exist?

Thanks in advance!
_________________
"I cannot support a movement that exploded spending and borrowing and blames its successor for the debt."
-Andrew Sullivan
Back to top
View user's profile Send private message
tdennist
Tux's lil' helper
Tux's lil' helper


Joined: 11 May 2004
Posts: 145

PostPosted: Thu Mar 15, 2007 1:20 pm    Post subject: Reply with quote

I don't know Postgresql, and I haven't done Perl DB programming in a while, but here's the trick:

Code:

my $dbh = DBI->connect("dbi:Pg:dbname=$dbase","$uid","$pw");
if (undef($dbh)) {
    #create the new database
}


I'm sorry if that's too vague to be useful, but just trying to help out :-).
_________________
Registered Linux user #356840

Fleens??! YOU'RE not FLEENS! Well, whatever you are....MAKE ME A PIZZA!!?
Back to top
View user's profile Send private message
atrus123
Guru
Guru


Joined: 06 Jul 2005
Posts: 339
Location: Annapolis, MD

PostPosted: Thu Mar 15, 2007 1:26 pm    Post subject: Reply with quote

tdennist wrote:
I don't know Postgresql, and I haven't done Perl DB programming in a while, but here's the trick:

Code:

my $dbh = DBI->connect("dbi:Pg:dbname=$dbase","$uid","$pw");
if (undef($dbh)) {
    #create the new database
}


I'm sorry if that's too vague to be useful, but just trying to help out :-).


That makes a lot of sense. Thanks for your input.

I'll give that a shot, and I'll be back if I still have questions. :)
_________________
"I cannot support a movement that exploded spending and borrowing and blames its successor for the debt."
-Andrew Sullivan
Back to top
View user's profile Send private message
atrus123
Guru
Guru


Joined: 06 Jul 2005
Posts: 339
Location: Annapolis, MD

PostPosted: Mon Mar 19, 2007 9:55 pm    Post subject: Reply with quote

Back to this again. Here's the syntax I'm using in my cgi file, but I can't seem to get it working correctly. I'd appreciate any corrections.

Code:
my $dbh = DBI->connect("dbi:Pg:dbname=$dbase","$uid","$pw") or $c = 1;
if (undef($dbh))
{
  $dbh = DBI->func("createdb", $dbase, $uid, $pw);
}


Here's the apache error I'm getting:

Code:
DBI connect('dbname=test01','postgres',...) failed: FATAL:  database "test01" does not exist


I don't know why I'm having such a tough time finding information on how to do this online, but I really can't find anything.
_________________
"I cannot support a movement that exploded spending and borrowing and blames its successor for the debt."
-Andrew Sullivan
Back to top
View user's profile Send private message
tdennist
Tux's lil' helper
Tux's lil' helper


Joined: 11 May 2004
Posts: 145

PostPosted: Tue Mar 20, 2007 12:08 am    Post subject: Reply with quote

Weeell. We'll probably need a bit more information, such as does the database 'test01' actually exist/do you have permission to connect to it :p.
_________________
Registered Linux user #356840

Fleens??! YOU'RE not FLEENS! Well, whatever you are....MAKE ME A PIZZA!!?
Back to top
View user's profile Send private message
runningwithscissors
Guru
Guru


Joined: 21 Apr 2006
Posts: 454
Location: the third world

PostPosted: Tue Mar 20, 2007 3:43 am    Post subject: Reply with quote

atrus123 wrote:
Back to this again. Here's the syntax I'm using in my cgi file, but I can't seem to get it working correctly. I'd appreciate any corrections.

Code:
my $dbh = DBI->connect("dbi:Pg:dbname=$dbase","$uid","$pw") or $c = 1;
if (undef($dbh))
{
  $dbh = DBI->func("createdb", $dbase, $uid, $pw);
}


Here's the apache error I'm getting:

Code:
DBI connect('dbname=test01','postgres',...) failed: FATAL:  database "test01" does not exist


I don't know why I'm having such a tough time finding information on how to do this online, but I really can't find anything.

You won't find much information because, databases are usually not generated programatically. You use the proper client to do that. Or, if they are, they use proper library functions for that (like when you're writing a client for the database).

Now I don't know Perl very well, but if it supports exceptions, have DBI->connect() throw an error and then connect to the database using the db root password and create the database.
But I doubt you'd be able to do that. I don't think DBI provies a method to connect as root to the database.
_________________
At some stage, the Hindus locked on to the nation destroying concepts like ahimsa (non-violence), shanti (peace), satya (truth) — the ‘ass’ syndrome.
Back to top
View user's profile Send private message
tdennist
Tux's lil' helper
Tux's lil' helper


Joined: 11 May 2004
Posts: 145

PostPosted: Tue Mar 20, 2007 11:24 am    Post subject: Reply with quote

Code:
my $dbh = DBI->connect("dbi:Pg:dbname=$dbase","$uid","$pw") or $c = 1 || push(@errors,"$DBI::errstr");
if (!@errors) {
  #we're good
} else {
  foreach $error (@errors) {
    print "Error: $error\n";
  }
}




If I remember anything from my DBI and Perl days, that's another method. However, runningwithscissors raised a good point that I had forgotten about. If you're developing this for somebody else who's going to be putting it on their website, for instance, have the error message direct the user to contact their webmaster or whoever to manually create the database. If you're developing it for yourself, have the error message say hey, you need to create this database first.
_________________
Registered Linux user #356840

Fleens??! YOU'RE not FLEENS! Well, whatever you are....MAKE ME A PIZZA!!?
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Off the Wall All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum