Sponsored Content
Top Forums Programming Problems using Perl DBI to edit database entries - basic stuff Post 302379991 by SteveSwitzer on Sunday 13th of December 2009 10:04:56 PM
Old 12-13-2009
Dave,
What you have is mostly a mis-ordering of the lines. try this instead:

Code:
#SQL CODE

my $dbh = DBI->connect("DBI:mysql:daveDB","dave","pass412");
my $sql = "INSERT INTO user_accounts (username, email, password)
 VALUES ($NewUserName, $NewEmail, $CryptPassword)";
my $sth = $dbh->prepare($sql);
my $res = $sth->execute();
$sth->finish();
$dbh->disconnect();

print end_html;

Once $dbh is defined, a "prepare" call is needed to get the SQL statement into the object.

As a side note, for best coding practice, you should have a " -w" after the perl line at the top (like this: "#!/usr/local/bin/perl -w") OR "use warnings;" to be sure you're told about things that could be mistakes. It's also a very good idea to "use strict;", which forces you to declare variables with "my" before you can use them. This also helps to make in obvious what variable scope you're expecting and can really reduce the amount of hair you pull out during a project. Smilie

HTH!

Steve

Quote:
Originally Posted by Dave247
Here is my Perl code with the DBI code in there to access the database:
When I run this, I get this error message: "Can't locate object method "disconnect" via package "INSERT INTO user_accounts (username, email, password)"Dave247
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problems Downloading and Installing Stuff

HI to everyone, I have been for a very long time in my life a GUI user, and now that i have to use a Solaris 5 terminal, i am not sure how to do some things: Downloading stuff from the internet: How do i do that? in a GUI you just click on the link and start downloading automatic, but i have... (4 Replies)
Discussion started by: sx3v1l_1n51de
4 Replies

2. Shell Programming and Scripting

PERL DBI module install

We ran into an issue trying to install DBI and DB2 modules for perl for AIX from the link http://www-306.ibm.com/software/data/db2/perl/ We tried to install the DBI module using bash# perl -MCPAN -e 'install DBI' command. However we ended up with the following error. Stop. ... (3 Replies)
Discussion started by: jerardfjay
3 Replies

3. UNIX for Dummies Questions & Answers

SSH Connection - Problems viewing stuff

Hey, I connected with Cygwin and this command: "ssh -Y USER@web.com" Then trying to open a program, but this error appeared: "Cannot open X display: DISPLAY variable unset" what do I have to do? Thanks. Alex (3 Replies)
Discussion started by: alf123
3 Replies

4. Programming

perl dbi to oracle getting disconnect_all for oracle dbi help

hi i am trying to connect to an oracle database using dbi and i get this :: Driver has not implemented the disconnect_all method. at /opt/perl/lib/site_perl/5.8.0/sun4-solaris/DBI.pm line 575 END failed--call queue aborted. for all i know, the script was working earlier, but has... (1 Reply)
Discussion started by: poggendroff
1 Replies

5. Shell Programming and Scripting

Installing Perl DBI and DBD

Hi, i have some queries on installing the Perl DBI and the DBD Oracle. I know that i have to install the DBI first. I have the source files in a folder in my home directory.The commands to install arecd /home/DBI Perl Makefile.PL make make installI would like to know, after executing these... (4 Replies)
Discussion started by: new2ss
4 Replies

6. Shell Programming and Scripting

DBI - Change database

Hello ! I am working on a small Perl script that should connect to the MySQL server, will select all the databases one by one and do some queryes on each. I started working on it but I just saw that on the DBI manual page there's no method for changing the working database. Am I missing... (2 Replies)
Discussion started by: Sergiu-IT
2 Replies

7. Shell Programming and Scripting

connect to MySQL from Perl using DBI

Hi, I want to connect perl with the mysql to select and so on but the connection don't work code #!/usr/bin/perl BEGIN { # PERL MODULES WE WILL BE USING use DBI; $dbh = DBI->connect('DBI:mysql:C:\Program Files\MySQL\MySQL Server 5.0\data\db1','','pass') or die $DBI::errstr;} #... (1 Reply)
Discussion started by: eng_shimaa
1 Replies

8. Shell Programming and Scripting

perl: help with DBI

Hi there, I have a bit of code similar to below (which ive actually got from perldoc, but mine is similar enough) $sth = $dbh->prepare(q{ SELECT region, sales FROM sales_by_region }); $sth->execute; my ($region, $sales); # Bind Perl variables to columns: $rv =... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

9. Shell Programming and Scripting

GNU-Screen Stuff problems

OPTIONS="java -Xms1024M -Xmx1024M -jar craftbukkit.jar" PROCESS=server01 screen -dmS $PROCESS $OPTIONS nogui # Starting the application screen -x $PROCESS -X stuff `printf "stop\r"` # Closing the application screen -x $PROCESS # Attaching to the... (3 Replies)
Discussion started by: Zanax
3 Replies

10. Shell Programming and Scripting

Perl DBI error

Hi All, I installed DBI module in a non INC location and using it in my script via "use lib". But it throw the below error at the "use DBI" step. Please help Usage: DBI::_install_method(dbi_class, meth_name, file, attribs=Nullsv) at /xx/xxx/xxxxx/xxxxx/oracle/lib/DBI.pm/oracle/lib/DBI.pm line... (2 Replies)
Discussion started by: prasperl
2 Replies
NetSDS::DBI(3pm)					User Contributed Perl Documentation					  NetSDS::DBI(3pm)

NAME
NetSDS::DBI - DBI wrapper for NetSDS SYNOPSIS
use NetSDS::DBI; $dbh = NetSDS::DBI->new( dsn => 'dbi:Pg:dbname=test;host=127.0.0.1;port=5432', login => 'user', passwd => 'topsecret', ); print $db->call("select md5(?)", 'zuka')->fetchrow_hashref->{md5}; DESCRIPTION
"NetSDS::DBI" module provides wrapper around DBI module. CLASS API
new(%params) - class constructor $dbh = NetSDS::DBI->new( dsn => 'dbi:Pg:dbname=test;host=127.0.0.1;port=5432', login => 'user', passwd => 'topsecret', ); dbh() - DBI connection handler accessor Returns: DBI object This method provides accessor to DBI object and for low level access to database specific methods. Example (access to specific method): my $quoted = $db->dbh->quote_identifier(undef, 'auth', 'services'); # $quoted contains "auth"."services" now call($sql, @bind_params) - prepare and execute SQL query Method "call()" implements the following functionality: * check connection to DBMS and restore it * prepare chached SQL statement * execute statement with bind parameters Parameters: * SQL query with placeholders * bind parameters Return: * statement handler from DBI Example: $sth = $dbh->call("select * from users"); while (my $row = $sth->fetchrow_hashref()) { print $row->{username}; } fetch_call($sql, @params) - call and fetch result Paramters: SQL query, parameters Returns: arrayref of records as hashrefs Example: # SQL DDL script: # create table users ( # id serial, # login varchar(32), # passwd varchar(32) # ); # Now we fetch all data to perl structure my $table_data = $db->fetch_call("select * from users"); # Process this data foreach my $user (@{$table_data}) { print "User ID: " . $user->{id}; print "Login: " . $user->{login}; } begin() - start transaction commit() - commit transaction rollback() - rollback transaction quote() - quote SQL string Example: # Encode $str to use in queries my $str = "some crazy' string; with (dangerous characters"; $str = $db->quote($str); INTERNAL METHODS
_add_sets() - add initial SQL query Example: $obj->_add_sets("set search_path to myscheme"); $obj->_add_sets("set client_encoding to 'UTF-8'"); _add_attrs() - add DBI handler attributes $self->_add_attrs(AutoCommit => 1); _check_connection() - ping and reconnect Internal method checking connection and implement reconnect _connect() - connect to DBMS Internal method starting connection to DBMS EXAMPLES
samples/testdb.pl SEE ALSO
DBI, DBD::Pg TODO
1. Make module less PostgreSQL specific. AUTHOR
Michael Bochkaryov <misha@rattler.kiev.ua> LICENSE
Copyright (C) 2008-2009 Net Style Ltd. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA perl v5.10.1 2010-04-28 NetSDS::DBI(3pm)
All times are GMT -4. The time now is 05:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy