Issue querying from Access database: “could not convert string to float: E+6”

后端 未结 2 1048
臣服心动
臣服心动 2020-12-07 03:43

I have a database in MS Access. I am trying to query one table to Python using pypyodbc. I get the following error message:

ValueError: could not conv

相关标签:
2条回答
  • 2020-12-07 04:27

    This appears to be a compatibility issue between pypyodbc and the Access ODBC driver when retrieving "large" or "small" numbers from a Single or Double field (column), where "large" means

    • Single values with more than 6 significant digits to the left of the decimal point, or
    • Double values with more than 14 significant digits to the left of the decimal point

    and "small" means

    • Single values with more than 6 zeros immediately to the right of the decimal point, or
    • Double values with more than 14 zeros immediately to the right of the decimal point

    when the numbers are represented as "normal" decimals (i.e., not in scientific notation).

    Code to recreate:

    import pypyodbc
    cnxn = pypyodbc.connect(
        r"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};"
        r"DBQ=C:\Users\Public\Database1.accdb")
    crsr = cnxn.cursor()
    try:
        crsr.execute("DROP TABLE tblJunk")
    except pypyodbc.ProgrammingError as pe:
        # ignore "table does not exist"
        if pe.value[0] != '42S02':
            raise
    crsr.execute("CREATE TABLE tblJunk (ID INT PRIMARY KEY, DoubleField DOUBLE, SingleField SINGLE)")
    crsr.execute("INSERT INTO tblJunk (ID, DoubleField) VALUES (1, 12345678.9)")
    crsr.execute("SELECT DoubleField, SingleField FROM tblJunk WHERE ID=1")
    row = crsr.fetchone()
    print(row)
    # prints: (12345678.9, None)
    crsr.execute("UPDATE tblJunk SET SingleField = DoubleField WHERE ID=1")
    crsr.execute("SELECT DoubleField, SingleField FROM tblJunk WHERE ID=1")
    row = crsr.fetchone()
    # ValueError: could not convert string to float: E+7
    

    Workaround 1: For Single values, using the CDbl() function can avoid the error:

    crsr.execute("SELECT DoubleField, CDbl(SingleField) AS foo FROM tblJunk WHERE ID=1")
    row = crsr.fetchone()
    print(row)
    # prints: (12345678.9, 12345679.0)
    

    Workaround 2: Use the CStr() function to return the value as a string and then convert it to a float afterwards (works for both Single and Double):

    crsr.execute("SELECT DoubleField, CStr(SingleField) AS foo FROM tblJunk WHERE ID=1")
    row = crsr.fetchone()
    print(row)
    # prints: (12345678.9, u'1.234568E+07')
    print(float(row[1]))
    # prints: 12345680.0
    

    Workaround 3: Use pyodbc instead of pypyodbc.

    0 讨论(0)
  • 2020-12-07 04:27

    As I was putting together test files for you to try to reproduce, I noticed that two of the fields in the table were set to Single type rather than Double. Changed them to Double and that solved the problem. Sorry for the bother and thanks for the help.

    0 讨论(0)
提交回复
热议问题