Solved

problem using perl to read small numbers from access database using microsoft access driver (*.mdb, *.accdb)

Posted on 2010-09-01
4
923 Views
Last Modified: 2012-08-13
Minimal version of problem attached. Numbers such as 1.23 are read ok using perl .mdb driver or .mdb, .accdb driver. Small numbers such as 1.23E-15 are read ok using .mdb driver, but not .accdb driver. Latter returns E-15 instead.

Have tried database in 2000,2003,2007 format and .accdb driver always has same behaviour. May be able to get away using older driver, but would like to understand how to make the newer one work.

I'm using active state perl 5.10

Output of code is shown below

Testing with driver microsoft access driver (*.mdb)
1 1.23
2 0.0000123
3 0.000000000123
4 1.23E-15
5 1.23E-20

Testing with driver microsoft access driver (*.mdb, *.accdb)
1 1.23
2 0.0000123
3 0.000000000123
4 E-15
5 E-20

#!/bin/perl

#

# Driver test on access table containing small numbers

#



use DBI;



$database = 'F:\temp\numericTest.mdb';





runTest('microsoft access driver (*.mdb)');

runTest('microsoft access driver (*.mdb, *.accdb)');





sub runTest {



      my $driver = shift(@_);

      print STDOUT "\nTesting with driver $driver\n";



     # Open database connection

     $dbh =  DBI->connect(

	'dbi:ODBC:driver='.$driver.';dbq=' . $database )

  || die "Can't connect to Kromer Database: $DBI::errstr";

      

     # run query

 	$sqlstatement =

	    "SELECT numbers from numbersTable";

	$sth = runQuery($sqlstatement);

	$cnt = 0;

	while ( @row = $sth->fetchrow_array() ) {

            $cnt++;

		$val = $row[0];

		print STDOUT "$cnt $val\n";

	}

     



     # print most recent errr if exists and close connection

     print $dbh->errstr;

     $dbh->disconnect;

}



sub runQuery {

	my $query = shift(@_);

	$sth = $dbh->prepare($sqlstatement)

	  || die "Can't prepare statement: $DBI::errstr";



	$sth->execute

	  || die "Could not execute SQL statement, check query";

	return $sth;

}

Open in new window

numericTest.mdb
0
Comment
Question by:ian__hawkins
  • 2
4 Comments
 
LVL 6

Expert Comment

by:apresence
ID: 33575448
Shot in the dark, but can you try:
SELECT CDBl(numbers) AS numbers from numbersTable

This will convert the number to a double-precision type before returning, which should give you the accuracy you need without truncating data like what you're seeing.
0
 

Author Comment

by:ian__hawkins
ID: 33575594
Thanks for the suggestion. Unfortunately it did not change the result. (I have also tried printing out $sth->TYPE[0] after the select query, and both drivers return  8, corresponding to "double" as expected)
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 33605449
Don't know the cause of the error, but this work-around seems to work.
$sqlstatement = "SELECT FORMAT(numbers, 'Scientific') from numbersTable";

Open in new window

0
 

Author Closing Comment

by:ian__hawkins
ID: 33609566
Many thanks for the reply - a good solution to the problem.
Ian
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SPROC to look for existing record in passed table name 7 47
View vs Edit mode enhancements 12 40
Syntax Error in Query 7 31
tabctrl with page click event 9 7
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now