Using MySql I want my bot to take the sn of the person iming it and look it up in my database. I thought I could do it this way. But it says that it can't call the prepare method. I'm guessing because the SQL isn't right. Any Idea's?
Code:
<br />sub on_im{<br />my ($aim,$client,$msg,$away) = @_;<br /><br /><br /> #connect to db<br /> my $dbh = DBI->connect("DBI:mysql:<database>:<host>","<username>","<pass>");<br /> print( "Connected to database\n") if( defined $dbh );<br /><br /> my $sth = $dbh->prepare("SELECT name FROM users where sn = ?");<br /> print( "Query executed properly\n") if( defined $sth );<br /><br /> $sth->execute($client);<br /> <br /> while ( my $hashref = $sth->fetchrow_hashref()) { <br /> print $hashref->{"name"} . "\n";<br /> }<br /> $sth->finish();<br /> $dbh->disconnect();<br /><br />}<br />1;<br /><br /><br />
Posting the actual message would help. From what I can see you have this correct. _________________ Check out Botworld! A dev resource for things bot.
Downloads, articles, news, fourm and more.
http://botworld.marzopolis.com
Joined: 06 Jan 2004 Posts: 562 Location: Netherlands
Posted: Fri Apr 08, 2005 8:16 pm Post subject:
Yes, it seems correct, but you really do not want to connect and query your database everytime sends you a message. Instead, create a public variable for the connection (or stuff it in the AIM object, whatever you want) and re-use that. Connecting each time will dramatically slow down your bot.
Good point, you should only need to connect once for the life of the bot. _________________ Check out Botworld! A dev resource for things bot.
Downloads, articles, news, fourm and more.
http://botworld.marzopolis.com
Joined: 22 Feb 2004 Posts: 121 Location: Richmond, VA
Posted: Mon Apr 11, 2005 3:37 am Post subject:
QUOTE(xiloki @ Apr 1 2005, 11:19 PM)
Code:
<br />sub on_im{<br />my ($aim,$client,$msg,$away) = @_;<br /><br /><br /> #connect to db<br /> my $dbh = DBI->connect("DBI:mysql:<database>:<host>","<username>","<pass>") or die $dbh->errstr;<br /> print( "Connected to database\n") if( defined $dbh );<br /><br /> my $sth = $dbh->prepare("SELECT name FROM users where sn = ?") or die $dbh->errstr;<br /> print( "Query executed properly\n") if( defined $sth );<br /><br /> $sth->execute($client);<br /> <br /> while ( my $hashref = $sth->fetchrow_hashref()) { <br /> print $hashref->{"name"} . "\n";<br /> }<br /> $sth->finish();<br /> $dbh->disconnect();<br /><br />}<br />1;<br /><br /><br />
[right][snapback]47458[/snapback][/right]
Try this code. If something is wrong, it should die and tell you the error when it does. Make sure you are running your bot from a command prompt and not just double clicking it, so you can see the error when the script closes. If you still can't see the problem, post here exactly what it says when the script dies, and we can help you better.