%Ultra Newbie Question% -- Perl & MYSQL working together

J

Julia Briggs

I am very new at this, and looking for a very simple standalone script
to insert data into a mysql database. I found this posted somewhere
else, but can't seem to get it to execute. It probably has the wrong
values - or something else. Can someone look at this and fix, or post
a simple example of how this should work? Thank you so much! :))

Sincerely,

Julia Briggs



#!/usr/local/bin/perl
# Use the DBI module
use DBI qw:)sql_types);

# Declare local variables

my ($databaseName, $databaseUser, $databasePw, $dbh);
my ($stmt, sth, @newRow);
my ($test_record);

# Set the parameter values for the connection
$databaseName = "dbname";
$databaseUser = "user";
$databasePw = "password";

$dbh = DBI->connect($databaseName, $databaseUser,
$databasePw) || die "Connect failed: $DBI::errstr\n";

# Create the statement.
$stmt = "INSERT INTO wslookup (test_record)
VALUES (‘testing')";

# Prepare and execute the SQL query
$sth = $$dbh->prepare($$stmt)
|| die "prepare: $$stmt: $DBI::errstr";
$sth->execute || die "execute: $$stmt: $DBI::errstr";

# Clean up the record set and the database connection
$sth->finish();
$dbh->disconnect();
 
G

gnari

Julia Briggs said:
I am very new at this, and looking for a very simple standalone script
to insert data into a mysql database. I found this posted somewhere
else, but can't seem to get it to execute.

this does not tell us much does it?
what id you do ?
what happened?
what did you expect to happen?
what did you try to do to fix it?

there are many things obviously wrong here, and some that might be.
#!/usr/local/bin/perl

add here these lines:
use strict;
use warnings;
# Use the DBI module
use DBI qw:)sql_types);

do you get an error message saying something DBI something?
my ($databaseName, $databaseUser, $databasePw, $dbh);
my ($stmt, sth, @newRow);

the sth should be $sth. did you get an error message mentioning
this line?
my ($test_record);

# Set the parameter values for the connection
$databaseName = "dbname";
$databaseUser = "user";
$databasePw = "password";

did you replace these with actual real-world values?
does the database and user exist?
$dbh = DBI->connect($databaseName, $databaseUser,
$databasePw) || die "Connect failed: $DBI::errstr\n";

something is missing here. $dbh is not defined.
did you get an error message about that, or did you just
skip a part in your posting?
# Create the statement.
$stmt = "INSERT INTO wslookup (test_record)
VALUES ('testing')";

does a table 'wslookup' exist with column 'test_record'?
# Prepare and execute the SQL query
$sth = $$dbh->prepare($$stmt)
|| die "prepare: $$stmt: $DBI::errstr";
$sth->execute || die "execute: $$stmt: $DBI::errstr";

whats the deal with the double $ chars?
# Clean up the record set and the database connection
$sth->finish();
$dbh->disconnect();


hope this helps.
gnari
 
T

Tad McClellan

Julia Briggs said:
Can someone look at this and fix, or post
a simple example of how this should work?


We could if we weren't conditioned crickets jumping all over the place.
 
J

Julia Briggs

We could if we weren't conditioned crickets jumping all over the place.

Add to "some" of the "we" as you globally indicate, who are hostile --
and also quite predictable... You might do better next time if you
just shut up or you'll just continue imagining using your words that
you somehow speak for 100% of the people in this newsgroup.
 
P

Peter Hickman

Julia said:
# Set the parameter values for the connection
$databaseName = "dbname";

Is this an example or are you actually using "dbname". It is usually of the
type "mysql:database=wizdom", that is the type of database (MySQL, PostgreSQL
etc) followed by the name of the actual database.
$databaseUser = "user";
$databasePw = "password";

$dbh = DBI->connect($databaseName, $databaseUser,
$databasePw) || die "Connect failed: $DBI::errstr\n";

Did you get an error at this point?
# Create the statement.
$stmt = "INSERT INTO wslookup (test_record)
VALUES (‘testing')";

# Prepare and execute the SQL query
$sth = $$dbh->prepare($$stmt)
|| die "prepare: $$stmt: $DBI::errstr";

Or did you get an error here?
$sth->execute || die "execute: $$stmt: $DBI::errstr";

Or perhaps here?
# Clean up the record set and the database connection
$sth->finish();
$dbh->disconnect();

Or did it run through to the end?

Has the wslookup table been created, what is it's structure?

The code is usefull but unless we have the exact same setup as yourself (and
you didn't tell us what that was) then the problems we have getting it to run
will not neccessarily be the same as yours.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top