Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old January 24th, 2001, 10:47 AM
DrMuziK DrMuziK is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 6 DrMuziK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Red face Help with Microsoft Access Database and CGI

I need help figuring out how to write data into a Microsoft Access database through a CGI script. My database is called PMVacantDB, and there is a single table in there called PMVacant. I can get the script to read data fine, so I know the connection is working. It's just writing data to it that I can't get to work. The syntax I've been trying is:

$dbh = DBI ->connect('DBI:ODBC:PMVacantDB', {RaiseError=>1}) or die "connecting: $DBI::errstr";

$insert = "INSERT INTO PMVacant (id) VALUES ($value)";
$in = $dbh->prepare($insert);
$in->execute;

The script will hang unless the $in->execute line is commented out. Obviously, it cannot execute.

What am I doing wrong?

[Edited by DrMuziK on 01-25-2001 at 08:30 AM]

Reply With Quote
  #2  
Old January 24th, 2001, 11:19 AM
dwarf dwarf is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 300 dwarf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Question

So it doesn't send you any feedback messages?? It doesn't tell you what the error is??

Reply With Quote
  #3  
Old January 25th, 2001, 09:31 AM
DrMuziK DrMuziK is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 6 DrMuziK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
No, I get no error messages. I put a print statement after the lines of code I posted above, and it gets to that fine so I know it's getting past my database calls. I'm still stumped. I think I'm going to try a mySQL database instead.

Reply With Quote
  #4  
Old January 25th, 2001, 06:41 PM
meeh82 meeh82 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: Riverside, CA
Posts: 63 meeh82 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Here is a script that I wrote that works

#!C:PERLbinperl.exe
use DBI;
print "Content-type:text/htmlnn";
print "TEST CONNECTEDn";

#(DSN,USER NAME, PASSWORD)
$db=DBI->connect('dbi:ODBCne','','',);

print "Connect Successful.n";

#sql statement
$sql="select * from Table1";

#preapre statement
$query=$db->prepare($sql);

#execute statement
$query->execute;


print "<html><table border=1 cellpadding=0>n";

#Fetch information to print
while($lineItem=$query->fetchrow_hashref)
{
print "<tr>n";
print "<td>$lineItem->{'one'}</td><td>$lineItem->{'two'}</td><td>$lineItem->{'three'}</td>n";
print "</tr>n";
}
print "</table></html>";

DBD::ODBC is only on a Windows system, if that is any help.


Reply With Quote
  #5  
Old January 25th, 2001, 06:43 PM
meeh82 meeh82 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: Riverside, CA
Posts: 63 meeh82 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
that connection string
is
$db=DBI->connect('dbi:ODBC:one','','',);


Reply With Quote
  #6  
Old January 26th, 2001, 12:14 AM
DrMuziK DrMuziK is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 6 DrMuziK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yeah, I could get my script to read from the database just fine. It's writing that I am having the problem doing.

My script opens a comma delimited file correctly (I have print statements to show this) and I want to WRITE all that info into my database. This is what's not working.

Reply With Quote
  #7  
Old January 26th, 2001, 02:18 AM
dwarf dwarf is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 300 dwarf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Just a stupid thought, but did you set up the permissions correctly?? It happens to me now and then. Since Access is, basiaclly, jsut a file, you need to setup your permissions, so that the user under which the web server works can write to it...

Of course, the problem may be in something completely different, for example a datatype mismatch or something like that...Could you please post your entire script, so we can see whre the problem lies??

Reply With Quote
  #8  
Old January 26th, 2001, 12:32 PM
DrMuziK DrMuziK is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 6 DrMuziK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok, I found a way to get an error message for the code I originally posted. I changed the last line from:

$in->execute;

to:

$in->execute or print "<br>cannot execute: $DBI::errstr";

and got the following message:

cannot execute: [Microsoft][ODBC Microsoft Access 97 Driver]Wrong number of parameters (SQL-07001)(DBD: st_execute/SQLExecute err=-1)

What parameters am I leaving out??

Reply With Quote
  #9  
Old January 26th, 2001, 01:59 PM
dwarf dwarf is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 300 dwarf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Again, is there a way to see your whole script?? It could be possible that the error is somewhere before this block of code...

Reply With Quote
  #10  
Old January 26th, 2001, 02:47 PM
dlc dlc is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: Boston, MA, USA
Posts: 3 dlc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Don't use prepare/execute

$insert = "INSERT INTO PMVacant (id) VALUES ($value)";
$in = $dbh->prepare($insert);
$in->execute;

Try:

$dbh->do($insert) || warn $dbh->errstr;

You don't need the prepare/execute/finish trio with inserts.

Reply With Quote
  #11  
Old January 26th, 2001, 03:18 PM
DrMuziK DrMuziK is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 6 DrMuziK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I got it working! This is the single command I used:

$dbh->do("INSERT INTO PMVacant (building,floor) VALUES ('$building','$floor')") or print "<br>$DBI::errstr";

Thanks for all your help!



Reply With Quote
  #12  
Old January 26th, 2001, 06:38 PM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 9
Well the problem you had in the 1st post was that you didn't quote $value in anyway.

$insert = "INSERT INTO PMVacant (id) VALUES ($value)";

should have been

$insert = "INSERT INTO PMVacant (id) VALUES ('$value')";

but to be clean and safe, you should have done it like this:

either:

$dbh->do("INSERT INTO PMVacant (id) VALUES ('".$dbh->quote($value)."')") or die $dbh->errstr;

or if you have a bunch of values to insert (maybe more than once)...

$q = $dbh->prepare("INSERT INTO blah (asdf,fdas,ast) VALUE (?,?,?)") or die $dbh->errstr;

$q->execute($var,$foo,$stuff) or die $dbh->errstr;

this way you can avoid ANY problems with single quotes or anything else messing up the quoting system.

[Edited by JonLed on 01-26-2001 at 05:47 PM]

Reply With Quote
  #13  
Old January 29th, 2001, 03:06 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
I would have to bet that its the statement you are preparing. I have never seen an instance of variable interpolation in a SQL statement work. That is why DBI supports placeholders.

My own database inserts look like this:
Code:
$dbh = DBI->connect(DBI:dbtype:db:hostname, other args);
$sth = $sth->prepare( "INSERT INTO tablename (column) VALUES( ? )" );   # the question mark is a placeholder to be filled
$sth->execute( $value );   # passing an argument to the 'execute' method will plug the value of the argument into the placeholder


Hope this helps.
__________________
- dsb -
Perl Guy

Reply With Quote
  #14  
Old January 29th, 2001, 03:43 PM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 9
Yes, that's exactly what I told him one post ago (other than the fact that you messed yours up).

Reply With Quote
  #15  
Old January 29th, 2001, 03:53 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

so you did. Except where did i mess mine up?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Help with CGI and Access Database


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump