Why can't I insert HTML into my database? - perl

I have errors when inserting into my database. I have tried everything to sus this. Someone mentioned usind DBI's trace() to track what is going wrong but i cannot make heads or tails of what is going on. Could someone please take a look and see what they think might be the problem.
D
BI 1.607-ithread default trace level set to 0x0/1 (pid 13524) at cgitest.pl line 10
-> DBI->connect(*****************************************************)
-> DBI->install_driver(mysql) for MSWin32 perl=5.010001 pid=13524 ruid=0 euid=0
install_driver: DBD::mysql version 4.011 loaded from C:/Perl/site/lib/DBD/mysql.pm
<- install_driver= DBI::dr=HASH(0x37be1fc)
!! warn: 0 CLEARED by call to connect method
<- connect('database=web110-accounts;host=79.170.44.110;', 'web110-accounts', ...)= DBI::db=HASH(0x37bdc8c) at DBI.pm line 638
<- STORE('PrintError', 1)= 1 at DBI.pm line 690
<- STORE('AutoCommit', 1)= 1 at DBI.pm line 690
<- STORE('Username', 'web110-accounts')= 1 at DBI.pm line 693
<> FETCH('Username')= 'web110-accounts' ('Username' from cache) at DBI.pm line 693
<- connected(**********************************)= undef at DBI.pm line 699
<- connect= DBI::db=HASH(0x37bdc8c)
<- STORE('dbi_connect_closure', CODE(0x37c3634))= 1 at DBI.pm line 708
<- prepare('SELECT * FROM accounts WHERE KI = '9096699576bcc810df5bc311650c4ebd' ')= DBI::st=HASH(0x37bda24) at cgitest.pl line 61
<- execute= 1 at cgitest.pl line 62
<- rows= '1' at cgitest.pl line 63
<- fetchrow_hashref= HASH(0x37bd514)24keys row1 at cgitest.pl line 64
<- finish= 1 at cgitest.pl line 71
<- disconnect= 1 at cgitest.pl line 72
****************************************************************************
****************************************************************************
<- STORE('PrintError', 1)= 1 at DBI.pm line 690
<- STORE('AutoCommit', 1)= 1 at DBI.pm line 690
<- STORE('Username', 'web110-db-2')= 1 at DBI.pm line 693
<> FETCH('Username')= 'web110-db-2' ('Username' from cache) at DBI.pm line 693
<- connected('DBI:mysql:database=web110-db-2;host=79.170.44.110;', 'web110-db-2', ...)= undef at DBI.pm line 699
<- connect= DBI::db=HASH(0x3d615ac)
<- STORE('dbi_connect_closure', CODE(0x3d60f6c))= 1 at DBI.pm line 708
<- prepare('INSERT INTO ? ( headData, headDataOutput ) VALUES ( ?, ? )')= DBI::st=HASH(0x351659c) at cgitest.pl line 263
<- prepare('INSERT INTO ? ( bodyData, bodyDataOutput ) VALUES ( ?, ? )')= DBI::st=HASH(0x37bd914) at cgitest.pl line 264
<- prepare('INSERT INTO page_names (linkFromRoot, linkTrue, page_name, table_name, navigation, location) VALUES ( ?, ?, ?, ?, ?, ? )')= DBI::st=HASH(0x3d575fc) at cgitest.pl line 265
<- quote("http://www.themobilemakeover.co.uk/index.php")= "'http://www.themobilemakeover.co.uk/index.php'" at cgitest.pl line 271
<- prepare("SELECT * FROM page_names WHERE linkTrue = 'http://www.themobilemakeover.co.uk/index.php' ")= DBI::st=HASH(0x3d571bc) at cgitest.pl line 272
<- execute= '0E0' at cgitest.pl line 273
<- rows= '0' at cgitest.pl line 274
<- quote("The Mobile Makeover - Mobile Beautician")= "'The Mobile Makeover - Mobile Beautician'" at cgitest.pl line 289
<- prepare("SELECT * FROM page_names WHERE page_name = 'The Mobile Makeover - Mobile Beautician' ")= DBI::st=HASH(0x3d56fdc) at cgitest.pl line 290
<- execute= '0E0' at cgitest.pl line 291
<- rows= '0' at cgitest.pl line 292
<- DESTROY(DBI::st=HASH(3d54254))= undef at cgitest.pl line 287
<- do('CREATE TABLE IF NOT EXISTS `6959bbd13fdb4df586a5b9d08aae1153_body` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`bodyData` TEXT NOT NULL,
`bodyDataOutput` TEXT NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;')= '0E0' at cgitest.pl line 362
<- do('CREATE TABLE IF NOT EXISTS `6959bbd13fdb4df586a5b9d08aae1153_header` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`headData` TEXT NOT NULL,
`headDataOutput` TEXT NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;')= '0E0' at cgitest.pl line 363
!! ERROR: 1064 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''6959bbd13fdb4df586a5b9d08aae1153_header' ( headData, headDataOutput ) VALUES ( ' at line 1' (err#0)
<- execute('6959bbd13fdb4df586a5b9d08aae1153_header', '<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="The Mobile Makeover offers a wide range of services and treatments all from the luxuary of your own home!" />
<meta name="keywords" content="Mobile Makeover, Beauty Therapist, Beautician, Therapist, Mobile Therapist, Mansfield, Nottinghamshire" />
<meta name="language" content="en" />
<meta name="author" content="ACT Web Designs" />
<meta name="copyright" content="The Mobile Makeover" />
<meta name="publisher" content="ACT Web Designs" />
<meta name="country" content="United Kingdom" />
<meta name="city" content="Mansfield, Nottinghamshire" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<meta name="verify-v1" content="4lZIaMmjLMq+UA8nkPYB9RjF5RreNwF3Mzurm9JYvQM=" />
<title>The Mobile M...', ...)= undef at cgitest.pl line 365
!! ERROR: 1064 CLEARED by call to execute method
!! ERROR: 1064 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''6959bbd13fdb4df586a5b9d08aae1153_body' ( bodyData, bodyDataOutput ) VALUES ( '<' at line 1' (err#0)
<- execute('6959bbd13fdb4df586a5b9d08aae1153_body', '<body>
<div id="wrapper">
<div id="header">
<div id="innerHeader">
<h1><span>The Mobile Makeover - Mobile Beauty Therapist - Mansfield Nottinghamshire</span></h1>
<div class="clear"></div>
<?php include("php/navigation.php"); ?>
<div class="clear"></div>
</div>
</div>
<div id="main">
<div id="content1" class="content vis">
<h2>Welcome to The Mobile Makeover</h2>
<div class="image1">
</div>
<p>Home visit appointments are designed to save you time and stress, by providing a service in the comfort of your own home, no need to worry about travel and traffic or facing the cold night air after a relaxing treatment.</p>
<p>If you have a baby or a toddler The Mobile Makeover can help you put together a package of treatments that is ada...', ...)= undef at cgitest.pl line 366
!! ERROR: 1064 CLEARED by call to execute method
<- execute("public_html/index.php", "http://www.themobilemakeover.co.uk/index.php", ...)= 1 at cgitest.pl line 367
<- DESTROY(DBI::st=HASH(3d56f5c))= undef at cgitest.pl line 270
<- quote("http://www.themobilemakeover.co.uk/about-us-the-mobile-makeover.php")= "'http://www.themobilemakeover.co.uk/about-us-the-mobile-makeover.php'" at cgitest.pl line 271
<- prepare("SELECT * FROM page_names WHERE linkTrue = 'http://www.themobilemakeover.co.uk/about-us-the-mobile-makeover.php' ")= DBI::st=HASH(0x3d56d9c) at cgitest.pl line 272
<- execute= '0E0' at cgitest.pl line 273
<- rows= '0' at cgitest.pl line 274
<- quote("The Mobile Makeover - About Us")= "'The Mobile Makeover - About Us'" at cgitest.pl line 289
<- prepare("SELECT * FROM page_names WHERE page_name = 'The Mobile Makeover - About Us' ")= DBI::st=HASH(0x3b07814) at cgitest.pl line 290
<- execute= '0E0' at cgitest.pl line 291
<- rows= '0' at cgitest.pl line 292
<- DESTROY(DBI::st=HASH(3d54254))= undef at cgitest.pl line 287
<- do('CREATE TABLE IF NOT EXISTS `30df18a64311aa9aaaa9576b030f0f83_body` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`bodyData` TEXT NOT NULL,
`bodyDataOutput` TEXT NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;')= '0E0' at cgitest.pl line 362
<- do('CREATE TABLE IF NOT EXISTS `30df18a64311aa9aaaa9576b030f0f83_header` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`headData` TEXT NOT NULL,
`headDataOutput` TEXT NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;')= '0E0' at cgitest.pl line 363
!! ERROR: 1064 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''30df18a64311aa9aaaa9576b030f0f83_header' ( headData, headDataOutput ) VALUES ( ' at line 1' (err#0)
<- execute('30df18a64311aa9aaaa9576b030f0f83_header', '<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content=">My name is Kelly Knight, I am a highly Qualifed Mobile Beauty Therapist. I have trained in NVQ Level 2 and NVQ level 3 in Beauty Therapy with many additional courses and training..." />
<meta name="keywords" content="Mobile Makeover, About Us, Beauty Therapist, Mobile Therapist, Mansfield, Nottinghamshire" />
<meta name="language" content="en" />
<meta name="author" content="ACT Web Designs" />
<meta name="copyright" content="The Mobile Makeover" />
<meta name="publisher" content="ACT Web Designs" />
<meta name="country" content="United Kingdom" />
<meta name="city" content="Mansfield, Nottinghamshire" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<title>The Mobile Makeover - About U...', ...)= undef at cgitest.pl line 365
!! ERROR: 1064 CLEARED by call to execute method
!! ERROR: 1064 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''30df18a64311aa9aaaa9576b030f0f83_body' ( bodyData, bodyDataOutput ) VALUES ( '<' at line 1' (err#0)
<- execute('30df18a64311aa9aaaa9576b030f0f83_body', '<body id="aboutUsPage">
<div id="wrapper">
<div id="header">
<div id="innerHeader">
<h1><span>The Mobile Makeover - About Us</span></h1>
<div class="clear"></div>
<?php include("php/navigation.php"); ?>
<div class="clear"></div>
</div>
</div>
<div id="main">
<div id="content1" class="content vis">
<h2>About Us - Kelly Night</h2>
<div class="image1">
</div>
<p>My name is Kelly Knight, I am a highly Qualified Mobile Beauty Therapist. I have trained in NVQ Level 2 and NVQ level 3 in Beauty Therapy with many additional courses and training gained within my time in the beauty industry. I have gained my years of experience from working at one of the UK's biggest Hotels & Spa chains where i worked as a salon supervisor. After being approached by a number of people to go mobile to their homes i recognised that the...', ...)= undef at cgitest.pl line 366
!! ERROR: 1064 CLEARED by call to execute method
<- execute("public_html/about-us-the-mobile-makeover.php", "http://www.themobilemakeover.co.uk/about-us-the-mobile-makeover.php", ...)= 1 at cgitest.pl line 367
<- DESTROY(DBI::st=HASH(3d56f1c))= undef at cgitest.pl line 270
<- quote("http://www.themobilemakeover.co.uk/beauty-products-used.php")= "'http://www.themobilemakeover.co.uk/beauty-products-used.php'" at cgitest.pl line 271
<- prepare("SELECT * FROM page_names WHERE linkTrue = 'http://www.themobilemakeover.co.uk/beauty-products-used.php' ")= DBI::st=HASH(0x3d56f3c) at cgitest.pl line 272
<- execute= '0E0' at cgitest.pl line 273
<- rows= '0' at cgitest.pl line 274
<- quote("The Mobile Makeover - Beauty Products Used")= "'The Mobile Makeover - Beauty Products Used'" at cgitest.pl line 289
<- prepare("SELECT * FROM page_names WHERE page_name = 'The Mobile Makeover - Beauty Products Used' ")= DBI::st=HASH(0x3d545a4) at cgitest.pl line 290
<- execute= '0E0' at cgitest.pl line 291
<- rows= '0' at cgitest.pl line 292
<- DESTROY(DBI::st=HASH(3d54684))= undef at cgitest.pl line 287
<- do('CREATE TABLE IF NOT EXISTS `02b5f135f611c1d7b0ec090182bc6cf5_body` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`bodyData` TEXT NOT NULL,
`bodyDataOutput` TEXT NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;')= '0E0' at cgitest.pl line 362
<- do('CREATE TABLE IF NOT EXISTS `02b5f135f611c1d7b0ec090182bc6cf5_header` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`headData` TEXT NOT NULL,
`headDataOutput` TEXT NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;')= '0E0' at cgitest.pl line 363
!! ERROR: 1064 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''02b5f135f611c1d7b0ec090182bc6cf5_header' ( headData, headDataOutput ) VALUES ( ' at line 1' (err#0)
<- execute('02b5f135f611c1d7b0ec090182bc6cf5_header', '<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="We use a wide range of the finest products on the market today. Below is a short brief of just a few of the names which we believe give you the best results - we do not compromise when it comes to quality." />
<meta name="keywords" content="Mobile Makeover, Beauty Therapist, Beauty Products, Jessica, Calgel, Eve Taylor, Tantrick, Therapist, Mansfield, Nottinghamshire" />
<meta name="language" content="en" />
<meta name="author" content="ACT Web Designs" />
<meta name="copyright" content="The Mobile Makeover" />
<meta name="publisher" content="ACT Web Designs" />
<meta name="country" content="United Kingdom" />
<meta name="city" content="Mansfield, Nottinghamshire" />
<meta http-equiv="X-UA-Compa...', ...)= undef at cgitest.pl line 365
!! ERROR: 1064 CLEARED by call to execute method
!! ERROR: 1064 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''02b5f135f611c1d7b0ec090182bc6cf5_body' ( bodyData, bodyDataOutput ) VALUES ( '<' at line 1' (err#0)
<- execute('02b5f135f611c1d7b0ec090182bc6cf5_body', '<body id="aboutUsPage">
<div id="wrapper">
<div id="header">
<div id="innerHeader">
<h1><span>The Mobile Makeover - Beauty Products</span></h1>
<div class="clear"></div>
<?php include("php/navigation.php"); ?>
<div class="clear"></div>
</div>
</div>
<div id="main">
<div id="content1" class="content vis">
<h2>Beauty products that we use</h2>
<div class="image1">
</div>
<p>We use a wide range of the finest products on the market today. Below is a short brief of just a few of the names which we believe give you the best results - we do not compromise when it comes to quality.<br /> - <em>Kelly Knight</em></p>
</div>
<div class="content vis norm">
<h2>Jessica</h2>
<div class="image3">
</div>
<p>The Jessica © nail products are de...', ...)= undef at cgitest.pl line 366
!! ERROR: 1064 CLEARED by call to execute method
<- execute("public_html/beauty-products-used.php", "http://www.themobilemakeover.co.uk/beauty-products-used.php", ...)= 1 at cgitest.pl line 367
<- DESTROY(DBI::st=HASH(3d6982c))= undef at cgitest.pl line 270
<- quote("http://www.themobilemakeover.co.uk/beauty-treatments.php")= "'http://www.themobilemakeover.co.uk/beauty-treatments.php'" at cgitest.pl line 271
<- prepare("SELECT * FROM page_names WHERE linkTrue = 'http://www.themobilemakeover.co.uk/beauty-treatments.php' ")= DBI::st=HASH(0x3d56edc) at cgitest.pl line 272
<- execute= '0E0' at cgitest.pl line 273
<- rows= '0' at cgitest.pl line 274
<- quote("The Mobile Makeover - Beauty Treatments")= "'The Mobile Makeover - Beauty Treatments'" at cgitest.pl line 289
<- prepare("SELECT * FROM page_names WHERE page_name = 'The Mobile Makeover - Beauty Treatments' ")= DBI::st=HASH(0x3d6b48c) at cgitest.pl line 290
<- execute= '0E0' at cgitest.pl line 291
<- rows= '0' at cgitest.pl line 292
<- DESTROY(DBI::st=HASH(3d6c674))= undef at cgitest.pl line 287
<- do('CREATE TABLE IF NOT EXISTS `22e64cef7d70fa952ce7444f158e2c4e_body` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`bodyData` TEXT NOT NULL,
>>> EDIT <<<<<<
I tried to post full code but wouldn't allow:
.
..
...
my $headDataUpload = $dbh->prepare("INSERT INTO ? ( headData, headDataOutput ) VALUES ( ?, ? )") or die " error: Couldn't prepare : " . DBI->errstr;
my $bodyDataUpload = $dbh->prepare("INSERT INTO ? ( bodyData, bodyDataOutput ) VALUES ( ?, ? )") or die " error: Couldn't prepare : " . DBI->errstr;
my $insertData = $dbh->prepare("INSERT INTO page_names (linkFromRoot, linkTrue, page_name, table_name, navigation, location) VALUES ( ?, ?, ?, ?, ?, ? )") or die " error: Couldn't connect to database: " . DBI->errstr;
foreach( #mainPagesArray ) {
my $webpage = &extention(trim($_));
if( trim($webpage) ne trim($domain) ){
my $webpageQuote = $dbh->quote("http://www." . $webpage);
my $sth = $dbh->prepare("SELECT * FROM page_names WHERE linkTrue = $webpageQuote ") or die "Could not select from table" . $DBI::errstr;
$sth->execute();
if( $sth->rows == 0 ) {
#output display pages found
print "<span class=\"green\">http://www." . $webpage . "</span><br />\n";
my $md5Con = "http://www." . $webpage;
my $linkTrue = "http://www." . $webpage;
$webpage =~ s/^$domain//g;
my $linkFromRoot = $root . $webpage;
my $getTitleContents = get($md5Con);
my $stringPageName;
if( $getTitleContents =~ m/<title>([^<]*)<\/title>/g ) {
my #titles = ($1);
my $counter = 2;
while(#titles){
my $currentTitle = shift #titles;
my $titleQuoted = $dbh->quote($currentTitle);
my $sth = $dbh->prepare("SELECT * FROM page_names WHERE page_name = $titleQuoted ") or die "Could not select from table" . $DBI::errstr;
$sth->execute();
if( $sth->rows == 0 ) {
$stringPageName = $currentTitle;
last;
}else{
my $newTitle = $currentTitle . "(" . $counter . ")";
push(#titles, $newTitle);
$counter ++;
}
}
}else{
my #titles = ('Untitled');
my $counter = 2;
while(#titles){
my $currentTitle = shift #titles;
my $titleQuoted = $dbh->quote($currentTitle);
my $sth = $dbh->prepare("SELECT * FROM page_names WHERE page_name = $titleQuoted ") or die "Could not select from table" . $DBI::errstr;
$sth->execute();
if( $sth->rows == 0 ) {
$stringPageName = $currentTitle;
last;
}else{
my $newTitle = $currentTitle . "(" . $counter . ")";
push(#titles, $newTitle);
$counter ++;
}
}
}
my $page_name = $stringPageName;
my $table_name = md5_hex($md5Con);
my $navigation = "1";
my $location = "1";
my $bodyTable = md5_hex($md5Con) . "_body";
my $headerTable = md5_hex($md5Con) . "_header";
my $createBodyTable = "CREATE TABLE IF NOT EXISTS `$bodyTable` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`bodyData` TEXT NOT NULL,
`bodyDataOutput` TEXT NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;";
my $createHeadTable = "CREATE TABLE IF NOT EXISTS `$headerTable` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`headData` TEXT NOT NULL,
`headDataOutput` TEXT NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;";
my $fileContents;
if( $md5Con =~ m/\.php$/g ) {
my $ftp = Net::FTP->new($DB_ftpserver, Debug => 0) or die "Cannot connect to some.host.name: $#";
$ftp->login($DB_ftpuser, $DB_ftppass) or die "Cannot login ", $ftp->message;
$ftp->get("/" . $root . $webpage, "c:/perlscripts/" . md5_hex($md5Con) . "-code.php") or die $ftp->message;
my $file = "c:/perlscripts/" . md5_hex($md5Con) . "-code.php";
{
local( $/ ); # undefine the record seperator
open FILE, "<", $file or die "Cannot open:$!\n";
my $fileContents = <FILE>;
my $bodyContents;
my $headContents;
my #contentsArray = split( /<\/head>/is, $fileContents, 2);
#print $contentsArray[1];
if( scalar #contentsArray == 2 ){
$bodyContents = trim($contentsArray[1]);
$headContents = trim($contentsArray[0]) . "</head>";
#print $headContents . "\n";
$dbh->do($createBodyTable) or die " error: Couldn't create body table: " . DBI->errstr;
$dbh->do($createHeadTable) or die " error: Couldn't create header table: " . DBI->errstr;
$headDataUpload->execute($headerTable, $headContents, $headContents);
$bodyDataUpload->execute($bodyTable, $bodyContents, $bodyContents);
$insertData->execute($linkFromRoot, $linkTrue, $page_name, $table_name, $navigation, $location);
#unlink("c:/perlscripts/" . md5_hex($md5Con) . "-code.php");
}else{
print "<span class=\"red\">" . $md5Con . " cannot be used by our CMS, invalid data.</span><br />\n";
}
}
$ftp->quit;
}elsif( $md5Con =~ m/(?:(?:\.asp)|(?:\.aspx))$/g ){
}
}
}
}
}else{
print "<span class=\"red\"> error: No pages where found. This CMS is designed for pre-existing sites. Please contact support for more information.</span><br />\n";
}
...
..
.

It would have been helpful to see just your query and the error that resulted, separate from all the trace information.
It looks to me like you are trying to use a placeholder for the table name, something that mysql doesn't support.

Related

Not able to perform multiple sed operations at a time

Hi i need to perform multiple sed operations at a time and then flush the output to a file.
I have a .dat file which has data as follows
indicator.dat
Air_Ind - A.Air_Ind Air_Ind - 0000 - 00- 00
Rpting_Ind - Case When Dstbr_Id Is Null Then 'N' Else 'Y' End Rpting_Ind - 0000 - 00 - 00
Latitude,Longitude - A.Store_Latitude Latitude,A.Store_Longitude Longitude - 0000- 00- 00
coalesce(Pm_Cig_Direct_Ind,'') - Coalesce(Direct_Acct_Ind ,'') - 0004 - 01- 01
coalesce(Pm_Mst_Direct_Ind,'') - Coalesce(Direct_Acct_Ind ,'') - 0004 - 02 - 02
coalesce(Pm_Snus_Direct_Ind,'') - Coalesce(Direct_Acct_Ind ,'') - 0004 - 01 - 02
coalesce(Pm_Snuf_Direct_Ind,'') - Coalesce(Direct_Acct_Ind ,'') - 0004 - 04- 02
coalesce(Jmc_Cgr_Direct_Ind,'') - Coalesce(Direct_Acct_Ind ,'') - 2000 - 02 - 01
coalesce(Usst_Mst_Direct_Ind,'') - Coalesce(Direct_Acct_Ind ,'') - 1070- 02- 02
coalesce(Usst_Snus_Direct_Ind,'') - Coalesce(Direct_Acct_Ind ,'') - 1070 - 01 - 02
coalesce(Usst_Snuf_Direct_Ind,'') - Coalesce(Direct_Acct_Ind ,'') - 1070 - 04 - 02
Now I am trying to replace the parameters defined in a file called indicator.sql and flush the output to a file
indicator_s.sql:
Select A.Location_Id,
param1
From Edw.Location A
--1Left Outer Join
--1(
--1 Select
--1 Location_Id,
--1 Direct_Acct_Ind
--1 From Edw.Location_Bcbc D
--1 Where Company_Cd = 'param2'
--1 And Prod_Type_Cd = 'param3'
--1 And Prod_Catg_Cd = 'param4'
--1 ) A
--1 On L.Location_Id = A.Location_Id
Inner Join
Mim.Mdm_Xref_Distributor D
On D.Src_Dstbr_Id=A.Location_Id
Where Sdw_Exclude_Ind='N' And Dstrb_Cd='Us'
the else block is not entered at any point of time
#!/bin/sh
rm ./Source_tmp.sql
touch ./Source_tmp.sql
while read line
do
MIM=`echo $line | cut -d " " -f1 `
EDW=`echo $line | cut -d "-" -f2 `
Company_Cd=`echo $line | cut -d "-" -f3 `
Prod_Type_Cd=`echo $line | cut -d "-" -f4 `
Prod_Catg_Cd=`echo $line | cut -d "-" -f5 `
echo "Select top 10 * from (" >> ./Source_tmp.sql ;
sed "s/Param1/$MIM/g" indicator.sql >> Source_tmp.sql;
echo "minus">> Source_tmp.sql;
if [ "$MIM"="Air_Ind " ] || [ "$MIM"="Rpting_Ind " ] || [ "$MIM"="Latitude,Longitude " ]
then
sed "s/param1/$EDW/g" indicator_s.sql >> Source_tmp.sql
else
sed -e "s/--1/' '/g" -e "s/param1/$EDW/g" -e "s/param2/$Company_Cd/g" -e "s/param3/$Prod_Type_Cd/g" -e "s/param4/$Prod_Catg_Cd/g" ./indicator_s.sql >> ./Source_tmp.sql
fi
done <indicator.dat
the output should be like the param1 and param2 etc values which i defined should be replaced from the indicator .dat file and the commented lines need to be un-commented in the else block
kindly help me
As far as I can tell, the sed command "is working". But it does not produce the expected output. I guess. To give some output example:
[...]
' ' Where Company_Cd = ' 0000 '
' ' And Prod_Type_Cd = ' 00 '
' ' And Prod_Catg_Cd = ' 00'
[...]
Obviously you have extra quotes on start of line, and extra spaced around your values.
Will this fix your issue:
#!/bin/bash
[...]
MIM=`echo -n $line | cut -d " " -f1 `
EDW=`echo -n $line | cut -d "-" -f2 `
Company_Cd=`echo -n $line | cut -d "-" -f3 `
Prod_Type_Cd=`echo -n $line | cut -d "-" -f4 `
Prod_Catg_Cd=`echo -n $line | cut -d "-" -f5 `
# Trim spaces as you "cut" on "-", keeping extra spaces arround
shopt -s extglob
EDW=${EDW%%+([[:space:]])}; EDW=${EDW##+([[:space:]])};
Company_Cd=${Company_Cd%%+([[:space:]])}; Company_Cd=${Company_Cd##+([[:space:]])};
Prod_Type_Cd=${Prod_Type_Cd%%+([[:space:]])}; Prod_Type_Cd=${Prod_Type_Cd##+([[:space:]])};
Prod_Catg_Cd=${Prod_Catg_Cd%%+([[:space:]])}; Prod_Catg_Cd=${Prod_Catg_Cd##+([[:space:]])};
[...]
# Fix "sed" in your "else" clause by removing extra single quotes
sed -e "s/--1/ /g" -e "s/param1/$EDW/g" -e "s/param2/$Company_Cd/g" -e "s/param3/$Prod_Type_Cd/g" -e "s/param4/$Prod_Catg_Cd/g" ./indicator_s.sql >> ./Source_tmp.sql
Producing now the much more valid SQL:
[...]
Where Company_Cd = '0000'
And Prod_Type_Cd = '00'
And Prod_Catg_Cd = '00'
[...]
That being said, this is mostly some hacks to fix (some of ?) the various issues you might have in your script. But the whole things seems a little bit contrived. And fragile. As for example, it will break if any replace string contains a &. Here Be Dragons.
Its impossible to tell what you want the script to do given so far you've only posted a script that DOESN'T produce whatever output you want and you haven't posted the output you DO want, but let's start with this and you can update your question to show expected output and clarify your requirements:
$ cat tst.awk
BEGIN{ FS="-" }
NR==FNR { template = (template ? template ORS : "") $0; next }
{
split($0,arr,/ /)
MIM = arr[1]
EDW = $1
Company_Cd = $3
Prod_Type_Cd = $4
Prod_Catg_Cd = $5
$0 = "Select top 10 * from (\n" template
gsub(/Param1/,MIM "\nminus")
gsub(/param1/,EDW)
if ( MIM !~ /^Air_Ind|Rpting_Ind|Latitude,Longitude/ ) {
gsub(/--1/," ")
gsub(/param2/,Company_Cd)
gsub(/param3/,Prod_Type_Cd)
gsub(/param4/,Prod_Catg_Cd)
}
print
}
.
$ awk -f tst.awk indicator.sql indicator.dat
Select top 10 * from (
Select A.Id,
Air_Ind
minus
From Location A
--1Left Outer Join
--1(
--1 Select
--1 Location_Id,
--1 Direct_Acct_Ind
--1 From Location_Bcbc D
--1 Where Company_Cd = 'param2'
--1 And Prod_Type_Cd = 'param3'
--1 And Prod_Catg_Cd = 'param4'
--1 ) A
Select top 10 * from (
Select A.Id,
Rpting_Ind
minus
From Location A
--1Left Outer Join
--1(
--1 Select
--1 Location_Id,
--1 Direct_Acct_Ind
--1 From Location_Bcbc D
--1 Where Company_Cd = 'param2'
--1 And Prod_Type_Cd = 'param3'
--1 And Prod_Catg_Cd = 'param4'
--1 ) A

Unicode names of tables/columns in mssql db and sqlalchemy

I'm using latest sqlalchemy and latest pymssql from pip to connect mssql server 8.00.2039 (2005?) The difficulty is table and column names are in russian. Is it possible to handle this database with sqlalchemy? At least i have to make 'select ... where' queries.
engine = create_engine("mssql+pymssql://%s:%s#RTBD/rt?charset=utf8" % (settings.RT_USER, settings.RT_PWD), echo = True, encoding = 'utf8')
metadata = MetaData()
metadata.reflect(engine, only = [u"Заказы",])
orders = metadata.tables[u'Заказы']
res = engine.execute(orders.select(orders.c[u'Номер заказа'] == u'14-01-0001'))
Exception is
ValueError Traceback (most recent call last)
<ipython-input-8-50ce93243d1c> in <module>()
----> 1 engine.execute(orders.select(orders.c[orders.columns.keys()[0]] == u'14-01-0001'))
python2.7/site-packages/sqlalchemy/engine/base.pyc in execute(self, statement, *multiparams, **params)
1680
1681 connection = self.contextual_connect(close_with_result=True)
-> 1682 return connection.execute(statement, *multiparams, **params)
1683
1684 def scalar(self, statement, *multiparams, **params):
python2.7/site-packages/sqlalchemy/engine/base.pyc in execute(self, object, *multiparams, **params)
718 type(object))
719 else:
--> 720 return meth(self, multiparams, params)
721
722 def _execute_function(self, func, multiparams, params):
python2.7/site-packages/sqlalchemy/sql/elements.pyc in _execute_on_connection(self, connection, multiparams, params)
315
316 def _execute_on_connection(self, connection, multiparams, params):
--> 317 return connection._execute_clauseelement(self, multiparams, params)
318
319 def unique_params(self, *optionaldict, **kwargs):
python2.7/site-packages/sqlalchemy/engine/base.pyc in _execute_clauseelement(self, elem, multiparams, params)
815 compiled_sql,
816 distilled_params,
--> 817 compiled_sql, distilled_params
818 )
819 if self._has_events or self.engine._has_events:
python2.7/site-packages/sqlalchemy/engine/base.pyc in _execute_context(self, dialect, constructor, statement, parameters, *args)
945 parameters,
946 cursor,
--> 947 context)
948
949 if self._has_events or self.engine._has_events:
python2.7/site-packages/sqlalchemy/engine/base.pyc in _handle_dbapi_exception(self, e, statement, parameters, cursor, context)
1109 )
1110
-> 1111 util.reraise(*exc_info)
1112
1113 finally:
python2.7/site-packages/sqlalchemy/engine/base.pyc in _execute_context(self, dialect, constructor, statement, parameters, *args)
938 statement,
939 parameters,
--> 940 context)
941 except Exception as e:
942 self._handle_dbapi_exception(
python2.7/site-packages/sqlalchemy/engine/default.pyc in do_execute(self, cursor, statement, parameters, context)
433
434 def do_execute(self, cursor, statement, parameters, context=None):
--> 435 cursor.execute(statement, parameters)
436
437 def do_execute_no_params(self, cursor, statement, context=None):
python2.7/site-packages/pymssql.so in pymssql.Cursor.execute (pymssql.c:6057)()
python2.7/site-packages/_mssql.so in _mssql.MSSQLConnection.execute_query (_mssql.c:9858)()
python2.7/site-packages/_mssql.so in _mssql.MSSQLConnection.execute_query (_mssql.c:9734)()
python2.7/site-packages/_mssql.so in _mssql.MSSQLConnection.format_and_run_query (_mssql.c:10814)()
python2.7/site-packages/_mssql.so in _mssql.MSSQLConnection.format_sql_command (_mssql.c:11042)()
python2.7/site-packages/_mssql.so in _mssql._substitute_params (_mssql.c:18359)()
<type 'str'>: (<type 'exceptions.UnicodeEncodeError'>, UnicodeEncodeError('ascii', u'params dictionary did not contain value for placeholder: \u041d\u043e\u043c\u0435\u0440 \u0437\u0430\u043a\u0430\u0437\u0430_1', 57, 62, 'ordinal not in range(128)'))
The query is right and ends with WHERE [Заказы].[Номер заказа] = %(Номер заказа_1)s
But info message from sqla is INFO sqlalchemy.engine.base.Engine {'\xd0\x9d\xd0\xbe\xd0\xbc\xd0\xb5\xd1\x80 \xd0\xb7\xd0\xb0\xd0\xba\xd0\xb0\xd0\xb7\xd0\xb0_1': '14-01-0001'}
The strings \xd0\x9d\xd0\xbe\xd0\xbc\xd0\xb5\xd1\x80 \xd0\xb7\xd0\xb0\xd0\xba\xd0\xb0\xd0\xb7\xd0\xb0_1 and \u041d\u043e\u043c\u0435\u0440 \u0437\u0430\u043a\u0430\u0437\u0430_1 are equal to Номер заказа_1
as stated on the mailing list, FreeTDS and such are very picky about this. The following test works for me but for the poster above it did not work:
UnixODBC 2.3.0
FreeTDS 0.91
Pyodbc 3.0.7
Linux, not OSX, OSX has tons of problems with tds / pyodbc, I’m running on a Fedora 14 machine here
Freetds setting:
[sqlserver_2008_vmware]
host = 172.16.248.142
port = 1213
tds version = 7.2
client charset = UTF8
text size = 50000000
Test script:
# coding: utf-8
from sqlalchemy import create_engine, MetaData, Table, Column, String
e = create_engine("mssql+pyodbc://scott:tiger#ms_2008", echo=True)
#e = create_engine("mssql+pymssql://scott:tiger#172.16.248.142:1213", echo=True)
m = MetaData()
t = Table(u'Заказы', m, Column(u'Номер заказа', String(50)))
m.drop_all(e)
m.create_all(e)
orders = m.tables[u'Заказы']
e.execute(orders.select(orders.c[u'Номер заказа'] == u'14-01-0001'))
part of the output:
CREATE TABLE [Заказы] (
[Номер заказа] VARCHAR(50) NULL
)
2014-03-31 20:57:16,266 INFO sqlalchemy.engine.base.Engine ()
2014-03-31 20:57:16,268 INFO sqlalchemy.engine.base.Engine COMMIT
2014-03-31 20:57:16,270 INFO sqlalchemy.engine.base.Engine SELECT [Заказы].[Номер заказа]
FROM [Заказы]
WHERE [Заказы].[Номер заказа] = ?
2014-03-31 20:57:16,270 INFO sqlalchemy.engine.base.Engine (u'14-01-0001',)

Calculating timing offsets in a text file

I'm using sed to process a very long PeopleSoft trace file. The trace file is full of information, but I am interested in how long each step took to process, and the number of rows affected in the database by the step.
I have a fairly ugly bash script which is basically a one-line series of sed pipes. It works pretty quickly, so it is OK. Of course I would welcome any suggestions to clarify the code.
I must say that while it is ugly, it is clear (to me) what sequential steps it is going through. Often sed/awk one liners are completely unintelligible to a non-expert.
#!/bin/bash
sed '/./=' "$1" | sed '/./N; s/\n/ /' | sed '/--/!d' | sed '/-- B/d' | sed '/-- /d' | sed '/Instance/d' | sed '1,/Application Engine ended normally/!d' | awk '/Row/ {n = $0; getline; print $0 n; next } 1' > "$1".txt
Timings
Most lines contain timing information, in the format for HH:MM:SS. Many statements are running subsecond so the timing field often doesn't change. I'd like to produce a new field which is the offset of the previous line. Only the seconds need to be considered as most operations are subsecond anyway.
246 -- 14.54.43 .(TL_TIMEADMIN.MAIN.Step040) (PeopleCode)238 -- Row(s) affected: 1
247 -- 14.54.43 Program Skipping Step due to non-zero return code from PeopleCode at TL_TIMEADMIN.MAIN.Step040
249 -- 14.54.43 .(TL_TIMEADMIN.MAIN.Step050) (Call Section TL_TIMEADMIN.DISPATCH)
251 -- 14.54.45 ..(TL_TIMEADMIN.DISPATCH.Step02a) (PeopleCode)
253 -- 14.54.45 ..(TL_TIMEADMIN.DISPATCH.Step02a) (SQL)
266 -- 14.54.45 ..(TL_TIMEADMIN.DISPATCH.Step02b) (Call Section TL_TA000200.TA000200)258 -- Row(s) affected: 1
268 -- 14.54.46 ...(TL_TA000200.TA000200.Step001) (PeopleCode)
270 -- 14.54.46 ...(TL_TA000200.TA000200.Step001) (Call Section FUNCLIB_TLTA.STEPMSG)
I would like to see something like this:
246 -- 14.54.43 0 .(TL_TIMEADMIN.MAIN.Step040) (PeopleCode)238 -- Row(s) affected: 1
247 -- 14.54.43 0 Program Skipping Step due to non-zero return code from PeopleCode at TL_TIMEADMIN.MAIN.Step040
249 -- 14.54.43 0 .(TL_TIMEADMIN.MAIN.Step050) (Call Section TL_TIMEADMIN.DISPATCH)
251 -- 14.54.45 2 ..(TL_TIMEADMIN.DISPATCH.Step02a) (PeopleCode)
253 -- 14.54.45 0 ..(TL_TIMEADMIN.DISPATCH.Step02a) (SQL)
266 -- 14.54.45 0 ..(TL_TIMEADMIN.DISPATCH.Step02b) (Call Section TL_TA000200.TA000200)258 -- Row(s) affected: 1
268 -- 14.54.46 1 ...(TL_TA000200.TA000200.Step001) (PeopleCode)
270 -- 14.54.46 0 ...(TL_TA000200.TA000200.Step001) (Call Section FUNCLIB_TLTA.STEPMSG)
OK with an awk solution?
{
split($3, time, ".")
if (NR == 1) prev = time[3]
$3 = $3 " " time[3] - prev
prev = time[3]
print
}
output:
$ awk -f time.awk input
246 -- 14.54.43 0 .(TL_TIMEADMIN.MAIN.Step040) (PeopleCode)238 -- Row(s) affected: 1
247 -- 14.54.43 0 Program Skipping Step due to non-zero return code from PeopleCode at TL_TIMEADMIN.MAIN.Step040
249 -- 14.54.43 0 .(TL_TIMEADMIN.MAIN.Step050) (Call Section TL_TIMEADMIN.DISPATCH)
251 -- 14.54.45 2 ..(TL_TIMEADMIN.DISPATCH.Step02a) (PeopleCode)
253 -- 14.54.45 0 ..(TL_TIMEADMIN.DISPATCH.Step02a) (SQL)
266 -- 14.54.45 0 ..(TL_TIMEADMIN.DISPATCH.Step02b) (Call Section TL_TA000200.TA000200)258 -- Row(s) affected: 1
268 -- 14.54.46 1 ...(TL_TA000200.TA000200.Step001) (PeopleCode)
270 -- 14.54.46 0 ...(TL_TA000200.TA000200.Step001) (Call Section FUNCLIB_TLTA.STEPMSG)

Group by in JPA

My Question is why the GROUP BY didn't show in the glassfish logs.
i added the following data below because someone wants to see it
public List<Message> listAllMessageBySender(String currentUserID) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistenceUnit");
EntityManager entityManager = emf.createEntityManager();
TypedQuery<Message> eq = entityManager.createQuery("SELECT m FROM Message m WHERE m.sendBy.userId = :userid GROUP BY m.sendTo.userId", Message.class);
eq.setParameter("userid", Integer.parseInt(currentUserID));
List<Message> message = eq.getResultList();
return message;
}
Glassfish 3.1.2.Logs
FINE: SELECT id, message, send_date, send_by, send_to FROM message WHERE (send_by = ?)
bind => [50]
Data:
User A : sendBy.userId = 50
User B : sendTo.userId = 44
User C : sendTo.userId = 43
id send_date send_by send_to message
1 2013-03-14 14:58:46 44 50 50 send message to 44
2 2013-03-14 14:58:46 50 44 reply from 44 to 50
3 2013-03-14 17:38:52 44 50 2nd reply to 50
6 2013-03-14 18:22:10 50 44 44 reply to 50
7 2013-03-14 18:22:10 50 43 new
in the query above is i want to list all the user sent from User A. it causes duplication because there is two send_to 44. the xhtml below shows :
<ui:repeat value="#{messageController.items}" var="messageItem">
<br />
<div onclick="loadMessage()">
<h:panelGroup layout="block" >
<p:graphicImage height="50" width="50" value="#{messageItem.sendTo.profilePicture}"/>
</h:panelGroup>
<h:panelGroup layout="block" style="width: 270px;">
<h:outputText value="#{messageItem.sendTo.registerName}"/>
<h:panelGroup layout="block" style="float:right;">
<h:outputText value="#{messageItem.sendDate}" >
<f:convertDateTime pattern="HH:mm"/>
</h:outputText>
</h:panelGroup>
</h:panelGroup>
</div>
<p:remoteCommand name="loadMessageBean" action="#{messageController.loadMessage(messageItem.id)}" /> </ui:repeat>
ERD
User Table Message Table
user_id message_id
register_name send_date
send_to ----> referenced by user_id User Table
send_by ----> referenced by user_id User Table
This request doesn't make sense. group by is used when the select clause contains at least one aggregation function (sum, avg, min, max, etc.). And all the other columns must appear in the group by clause.
So for example, the following query would make sense:
SELECT max(m.length), sender.userId from Message m
inner join m.sendBy sender
GROUP BY sender.userId
It would return, for each message sender, the length of its longest message.
Tell us, with example data, what you would like your query to return.
If you want to get all the distinct users whom user A sent a message to, the query should simply be
select distinct m.sendTo from Message m
where m.sendBy.userId = :userid

Generate XML from Table

I have a table like this:
ID Name Column1 Column2 Column3
1 ABC 202.2 1500 34000
2 IJK 104 10000 27000
I want to generate XML like this:
<doc>
<record ID="1" Name="ABC" Column1="202.2" Column2="15000" Column3="34000" />
<record ID="2" Name="IJK" Column1="104" Column2="10000" Column3="27000" />
</doc>
I have got some clue from this forum post and used this code:
CREATE TABLE #tmp (column1 VARCHAR(20), column2 VARCHAR(20), column3 VARCHAR(20))
INSERT INTO #tmp VALUES ( 'data1', 'data2', 'data3' )
INSERT INTO #tmp VALUES ( 'data11', 'data21', 'data31' )
-- FOR XML PATH with ELEMENTS will automatically unpivot the data for you
-- Then reshape your XML using nested FLWOR loops
SELECT
(
SELECT *
FROM #tmp t
FOR XML PATH, ELEMENTS, TYPE
).query('
for $e in row
return
<row>{
for $f in $e/*
return <field name="{local-name($f)}">{data($f)}</field>
}
</row>
')
I tried the following modified version:
SELECT (SELECT * FROM cte_temp t FOR XML PATH, ELEMENTS, TYPE)
.query('for $e in row return
<doc>
{
for $f in $e return <record {local-name($f)}="{data($f)}" />
}
</doc>')
But I'm getting error:
XQuery [query()]: Invalid source character 0x7b found in an identifier
near 'return'.
Why you trying to get fancy.
Select * from #tmp as record
FOR XML AUTO, root('doc')