Gossamer Forum
Home : General : Perl Programming :

Anohter SQL Problem

Quote Reply
Anohter SQL Problem
Ok, i am still on the same thing of transfering data, but now it transfers all except ones with a ' in them . I have tried s/'//g; to get rid of the ' first but that didn't work.
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central
Quote Reply
Re: [nolimit] Anohter SQL Problem In reply to
You need to use $dbh->quote
Quote Reply
Re: [PaulW] Anohter SQL Problem In reply to
Just tried $dbh->quote and it wouldn't add any thing.

Code:
$dbh->do("INSERT INTO links VALUES (('$bc','$cd','$de','$ef'," . $dbh->quote("$fg") . ",'$gh','$hi','$ij','$jk','$kl','$lm','$mn')");
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central
Quote Reply
Re: [nolimit] Anohter SQL Problem In reply to
You should do it before $dbh->do

$fg = $dbh->quote($fg);

$dbh->do( ... );

Ideally you should quote all values.

You _need_ some error checking too.

Last edited by:

PaulW: Nov 24, 2001, 11:21 AM
Quote Reply
Re: [PaulW] Anohter SQL Problem In reply to
I will add some error checking in a minute, but right now the quoting is making it worse now more is not getting added then without the quotes.
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central
Quote Reply
Re: [nolimit] Anohter SQL Problem In reply to
>>now more is not getting added then without the quotes.<<

If you add error checking then you'll probably find out why!!
Quote Reply
Re: [PaulW] Anohter SQL Problem In reply to
The error says:

DBD::mysql::db do failed: You have an error in your SQL syntax near 'Internet Business Consultant helping entrepreneur in all their n' at line 1 at /home/crashint/public_html/cgi-bin/mysql/install.cgi line 19, line 2654.



then it lists every other line that wasnt added
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central
Quote Reply
Re: [nolimit] Anohter SQL Problem In reply to
You may be better turning $ab, $bc etc into a hash (or array) and using something like:

Code:
my $dbh = DBI->connect ( bla );

my $query = 'INSERT INTO Table VALUES (';

foreach (keys %hash) {
$query .= $dbh->quote($hash{$_}) . ',';
}
chop $query;

$query .= ')';

my $sth = $dbh->prepare($query) or &error($DBI::errstr);

$sth->execute or &error($DBI::errstr);

$sth->finish;

sub error {

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

}

Last edited by:

PaulW: Nov 24, 2001, 12:51 PM
Quote Reply
Re: [PaulW] Anohter SQL Problem In reply to
Created a hash and got :

Content-type: text/html You have an error in your SQL syntax near 'Table VALUES ('0','No\n','j_smallwood@hotmail.com','All About Art','No','All Abo' at line 1
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central
Quote Reply
Re: [nolimit] Anohter SQL Problem In reply to
Did you used the code I suggested?


It may be the \n causing it

Can you add $query into the error sub and see what it prints

Last edited by:

PaulW: Nov 24, 2001, 2:05 PM
Quote Reply
Re: [PaulW] Anohter SQL Problem In reply to
Yes, I used your code. I printed out $query and their is \n printing out after each No in there.
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central
Quote Reply
Re: [nolimit] Anohter SQL Problem In reply to
Please can you show me what the whole of $query prints

Where are the \n's coming from?
Quote Reply
Re: [PaulW] Anohter SQL Problem In reply to
Well it's way too long to print here..

Basically the data.txt has 14 |'s and in the middle of each one is where the \n is at Right before the email address...
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central
Quote Reply
Re: [nolimit] Anohter SQL Problem In reply to
>>Well it's way too long to print here..<<

??

It should only be one line.

INSERT INTO bla....
Quote Reply
Re: [PaulW] Anohter SQL Problem In reply to
oh i thought you meant the data.txt that I wanted transfered to sql..

Give me a minute..

-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central

Last edited by:

nolimit: Nov 24, 2001, 3:54 PM
Quote Reply
Re: [nolimit] Anohter SQL Problem In reply to
INSERT INTO Table VALUES ($dbh->quote(all about art),$dbh->quote(http://www.coralcoast.com/allaboutart.html|),$dbh->quote(7/20/01),$dbh->quote(art),$dbh->quote(all about art),$dbh->quote(Jeremy),$dbh->quote(j_smallwood@hotmail.com),$dbh->quote(12),$dbh->quote(No),$dbh->quote(No),$dbh->quote(0),$dbh->quote(0))


The error comes on the second no...
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central
Quote Reply
Re: [nolimit] Anohter SQL Problem In reply to
Ewww what have you done?

That isn't the code I provided.

Could you attach all the code?

Last edited by:

PaulW: Nov 24, 2001, 4:01 PM
Quote Reply
Re: [PaulW] Anohter SQL Problem In reply to
Code:

use DBI();


my $dbh = DBI->connect("DBI:mysql:crashint_link:localhost","crashint", "pass") or &error($DBI::errstr);

my $query = 'INSERT INTO Table VALUES (';

open(LINKS, "</home/crashint/public_html/data.txt");
while(<LINKS>) {
%data = split /\|/;

foreach (keys %data) { $query .= $dbh->quote($data{$_}) . ',';}

chop $query;


}
close(LINKS);

$query .= ')';
my $sth = $dbh->prepare($query) or &error($DBI::errstr);

print "$query";

$sth->execute or &error($DBI::errstr);
$sth->finish;


# Disconnect from the database.
$dbh->disconnect();

sub error {
print shift;
exit;
}

-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central

Last edited by:

nolimit: Nov 24, 2001, 4:05 PM
Quote Reply
Re: [nolimit] Anohter SQL Problem In reply to
There are a few vital differences between that code and mine. Anyway no probs...try:

Code:
use DBI;

my $query;
my (@data,@line);
my $dbh = DBI->connect("DBI:mysql:crashint_link:localhost","crashint", "pass")
or &error($DBI::errstr);

open(LINKS, "</home/crashint/public_html/data.txt");
@data = <LINKS>;
close LINKS;


foreach (@data) {
undef $query;
undef @line;
chomp;
@line = split /\|/;
$query = 'INSERT INTO Table VALUES (';
$query .= $dbh->quote($line[$_]) . ',' foreach @line;
chop $query;
$query .= ')';
$sth = $dbh->prepare($query) or &error($DBI::errstr);
$sth->execute or &error($DBI::errstr);
$sth->finish;
}
$dbh->disconnect();

sub error { print shift; exit; }

Not the fastest way but it works.

Last edited by:

PaulW: Nov 24, 2001, 4:23 PM
Quote Reply
Re: [PaulW] Anohter SQL Problem In reply to
Don't know what happened but stuff did get into the database just most of the data is numbers... Weird..
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central