Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem inserting Perl into mysql 1

Status
Not open for further replies.

BookemS

Technical User
Jul 21, 2006
2
US
I'm pretty new to Perl (and mysql for that matter) what I want to do is create a Perl script that does web data mining and inserts it into a mysql database.

I have Perl code that goes to the website using Simple::LWP and parses the data into a single variable that contains a comma-seperated row of data from the website.

I want a Perl statement that inserts the variable called $content into a mysql database.

The code that I have now is as follows:

my $dbh=DBI->connect("DBI:mysql:dbname:localhost:root");
$dbh->do(INSERT INTO table VALUES (?),$content);
$dbh->disconnect();

Obviously, this isn't working, but I'm not sure if I'm going about it all wrong. PLease help. Thanks in advance

 
You mean like this?

$dbh->do("INSERT INTO table VALUES (?),$content");

Also, I was thinking that it wasn't working because $content actually returns 19 values that are comma separated: ie, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19

I want to just have it dump this into the table. Is that a problem or should it work.

I received the error a couple times while change it that the number of columns do not match the number of values.

Your help is apprecaited.
 
If you are trying to load those values into records which contain only one field, then you want to end up with a SQL statement like:[tt]
INSERT tblname VALUES (1),(2),(3)[/tt]
which must be assembled using code,
or individual insert statements (less efficient):[tt]
INSERT tblname VALUES (1)
INSERT tblname VALUES (2)
INSERT tblname VALUES (3)
which can be produced with:[tt]
$dbh->do('INSERT tblname VALUES (?)',$n)[/tt]
where $n is 1 or 2 or 3, etc.

Alternatively, if your field is declared as AUTO_INCREMENT, you can simply execute the following several times:[tt]
INSERT tblname VALUES ()[/tt]
which will insert the values 1,2,3, etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top