Gossamer Forum
Home : General : Perl Programming :

SQL In Perl

Quote Reply
SQL In Perl
Ok, I finally need touse MYSQL in Perl for a project i am doing. I'm trying to work out how to do a MySQL dump (of all the tables) into a file. So far the code I have is;

Code:
$DBH = DBI->connect("DBI:mysql:dump_database", "root", "", { RaiseError => 0, PrintError => 0, AutoCommit => 1 });
$query = "BACKUP TABLE tbl_name[lsql_CatLinks, lsql_CatRelations, lsql_Category, lsql_Changes, lsql_ClickTrack, lsql_Editors, lsql_EmailMailings, lsql_EmailTemplates, lsql_Links, lsql_MailingIndex, lsql_MailingList, lsql_MailingListIndex, lsql_Reviews, lsql_Users, lsql_Verify] TO 'file.sql'";
$sth = $DBH->prepare($query);
$sth = $DBH->execute();
mysql_close($DBH);


However, when running this I get;
Code:
Software error:

Can't locate object method "execute" via package "DBI::db" at /home/ace-host/public_html/small_dumps/getsql.cgi line 47.

For help, please send mail to the webmaster (admin), giving this error message and the time and date of the error.


Anyone see what I've done wrong? I only need to use SQL in Perl this once....so all I have really done is put bits of code together and hope it works (which it obviously doesnt Tongue).

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] SQL In Perl In reply to
$sth->execute();

...not $DBH and $sth->finish(), $dbh->disconnect() not mysql_close.

Last edited by:

RedRum: Jan 11, 2002, 4:51 AM
Quote Reply
Re: [AndyNewby] SQL In Perl In reply to
http://www.perldoc.com/perl5.6.1/lib/DBI.html
Quote Reply
Re: [RedRum] SQL In Perl In reply to
Thanks Smile

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [RedRum] SQL In Perl In reply to
Erm...odd....!

I am now using;

Code:
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
require DBI;

$DBH = DBI->connect("DBI:mysql:dump_database", "admin", "147896321", { RaiseError => 0, PrintError => 0, AutoCommit => 1 });
$query = "BACKUP TABLE tbl_name[lsql_CatLinks, lsql_CatRelations, lsql_Category, lsql_Changes, lsql_ClickTrack, lsql_Editors, lsql_EmailMailings, lsql_EmailTemplates, lsql_Links, lsql_MailingIndex, lsql_MailingList, lsql_MailingListIndex, lsql_Reviews, lsql_Users, lsql_Verify] TO '/home/ace-host.com/public-html/small_dumps/file.sql'" or die $dbh->errstr;
$sth = $DBH->do($query);
#$sth = $DBH->execute();
$DBH->disconnect;

print "Content-type: text/html \n\n";
print "done";


Now, the code doesnt cause an error, BUT the backup is not written to the file. Is my Syntax wrong? Also, why didnt errsstr pick up the problem? Unsure

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] SQL In Perl In reply to
Read that link I gave you above. It tells you everything.

Last edited by:

RedRum: Jan 11, 2002, 5:43 AM
Quote Reply
Re: [RedRum] SQL In Perl In reply to
Darn. I've read most of the stuff @ that page you suggested (at least a lot of it....its HUGE!). I've got something really weird going on here. With;



Code:
$sth = $DBH->do($query);
if (!$sth) { print "there was an error <BR>\n"; }


Every time it reports and error! I've tried to catch the error with $DBH->err; and $DBH->errstr; (also with $sth, incase i was doing it wrong)...and no error is reported. In most cases a software is produced instead. Someone pleeeeease help me Frown

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] SQL In Perl In reply to
Ah, it was just the formatting. I needed;

Code:
$sth = $DBH->do($query);
if (!$sth) { print "there was an error. It was: " . $DBH->errsrt . "<BR>"; }


Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] SQL In Perl In reply to
$DBH->errstr or $DBI::errstr :)

Last edited by:

RedRum: Jan 11, 2002, 7:23 AM
Quote Reply
Re: [RedRum] SQL In Perl In reply to
Now he tells us (me) Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] SQL In Perl In reply to
Code:
[$DBI::err] $DBI::errstr

Will print you the Error Code and the Error string.

- wil
Quote Reply
Re: [Wil] SQL In Perl In reply to
Yup, got it now. Just got another problem now Wink

Code:
BACKUP TABLE * TO '/home/ace-host.com/public_html/small_dumps/file.sql'


That is what causes an error. I am assuming that the general formatting is right, but I must have something wrong. Anyone got any ideas on how to backup a complete database to a file?

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

AndyNewby: Jan 11, 2002, 7:56 AM
Quote Reply
Re: [AndyNewby] SQL In Perl In reply to
Try this command:

Code:
mysqldump -h localhost -u username -p password
databasename > /home/ace-host.com/public-html/small_dumps/file.sql

Rgds

- wil
Quote Reply
Re: [Wil] SQL In Perl In reply to
Thanks Wil. That kinda worked Unsure It ran ok, and created file.sql; but file.sql was 0 bytes. I know for a fact the database I am trying to backup has stuff in as I just added it all! any suggestions?

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] SQL In Perl In reply to
Hm. Have you read the following?

http://www.mysql.com/doc/B/a/Backup.html

- wil
Quote Reply
Re: [AndyNewby] SQL In Perl In reply to
mysqldump is a command line program Laugh

Last edited by:

RedRum: Jan 11, 2002, 8:32 AM
Quote Reply
Re: [RedRum] SQL In Perl In reply to
Yes. I was responding directly to the line:

In Reply To:
Anyone got any ideas on how to backup a complete database to a file?

- wil
Quote Reply
Re: [Wil] SQL In Perl In reply to
>>
I finally need touse MYSQL in Perl for a project i am doing. I'm trying to work out how to do a MySQL dump
<<


...Andy:

And in answer to the question it is pretty tricky if this is your first script. You need to use SHOW Tables and DESCRIBE and all sorts.

Last edited by:

RedRum: Jan 11, 2002, 8:45 AM
Quote Reply
Re: [RedRum] SQL In Perl In reply to
No you don't?!!

http://www.mysql.com/.../A/BACKUP_TABLE.html

- wil
Quote Reply
Re: [Wil] SQL In Perl In reply to
Hmm its not even worth pointing out your mistake and getting into an argument....I'll let someone else do it Laugh
Quote Reply
Re: [RedRum] SQL In Perl In reply to
Stop it. You're being cocky again. And that's not a good thing at 17:15 on a Friday Unsure

If you think I've done a mistake, feel free to let me in on your insight.

Sorry, but I don't see where you can pick up that there's a mistake there. I simply gave Andy a reference URL to the MySQL documentation that shows how to do what he is looking for. I don't know what you're babbling on about with your Selects and your Froms bla bla.

Have you ever considered usenet?!!

- wil

Last edited by:

Wil: Jan 11, 2002, 9:17 AM
Quote Reply
Re: [Wil] SQL In Perl In reply to
Wil, it's far from being cocky....I just know how you react in these situations (as you just proved with that latest post)....so I am choosing not to get into an argument.

Remember Andy asked how to do a mysqldump

Last edited by:

RedRum: Jan 11, 2002, 9:39 AM
Quote Reply
Re: [RedRum] SQL In Perl In reply to
Yes. And I am asking you, unless you can backup your claims or comments - **don't** post them.

Posting "this won't work" or similar is usless. What is useful is "this won't work, *because* " and even.. "here's another method, or a sugested alternative"

Now, can you please explain?

- wil