Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Win32::ODBC insert question

by thefid (Friar)
on Mar 22, 2001 at 01:08 UTC ( [id://66140]=perlquestion: print w/replies, xml ) Need Help??

thefid has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, I am trying to insert records into an MS Access 2K database. I am having problems with a table that has a primary key with the data type of "AutoNumber" which is a Access-driven auto-sequenced key. What I want is to pass along the fields and have Access or the ODBC driver generate the next sequence number.

I have tried leaving the field out, in which I get the "Too few parameters" error. I've tried an blank variable, which gives me a "Data Type mismatch" error. Also, I've tried the number 0, which will write a record where the sequential key is 0, but of course if I try to write another record, I get a duplicate key error. The table looks like:

order <--- table name ------- seq <--- primary key, autonumber item desc qty date_time user
Here's my test code:
# Make Database connection $db = new Win32::ODBC("DSN=$DATA_SOURCE_NAME; UID=$USER_ID;PWD=$PASSWORD;") or die Win32::ODBC::Error(); # Test Variables $new_item = "Test"; $new_desc = "Test description"; $new_qty = 2099; $new_user = "TESTUSER"; # get current time $now_time = gmtime; $new_seq = 0; # Insert row into database if ($db->Sql("INSERT INTO order VALUES ($new_seq,'$new_item', '$new_desc', $new_qty,'$now +_time','$new_user')" )) { # Print out any ODBC/SQL error print $db->Error(); } # Close the database $db->Close();
Has anyone had experience in doing this? Any help will be much appreciated. thanks.

Replies are listed 'Best First'.
Re: Win32::ODBC insert question
by dws (Chancellor) on Mar 22, 2001 at 03:10 UTC
    To insert a subset of the fields in a record, you need to explicitly list the field names in the query. Try
    "INSERT INTO orders (item, desc, qty, date_time, user) VALUES('$new_item','$new_desc','$new_qty','$now_time','$new_user')"
(crazyinsomniac) Re: Win32::ODBC insert question
by crazyinsomniac (Prior) on Mar 22, 2001 at 01:35 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://66140]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found