开发者

PyODBC - cursor record value rounding

I am working on a Python script (Python version 2.5.1 on Windows XP) that involves connecting to a Microsoft Access (.mdb) database to read values from a table. I'm getting some unexpected results with one record whereby the field of interest precision is getting rounded.

I know the Access table field of interest is a Double data type. But, the value that caused me to discover this in the table is 1107901035.43948. When I read the value in the Python code and print it out, it's showing 1107901035.44.

Is there a pyODBC connection parameter or other that must be set? I couldn't find anything in the documentation

Here's what my code looks like (the intention is to resolve unneeded records by identifying the record that has the greatest value for my field of interest):

conn = pyodbc.connect('DRIVER={Microsoft Ac开发者_运维技巧cess Driver (*.mdb, *.accdb)};DBQ=' + pGDB)
conn.autocommit = True
cursor = conn.cursor()

tableList = []
for x in cursor.tables():
    val = str(x[2])
    if val[0:3] <> "MSy":
        if val[0:3] <> "GDB":
            if val[-5:] <> "Index":
                tableList.append(val)

for x in tableList:
    try:
        SQL = "SELECT * FROM %s" % (x)
        cursor.execute(SQL)
        rows = cursor.fetchall()
        counter = 0
        for row in rows:
            counter +=1

        if counter > 1:
            print "Site %s is a multipart basin" % (x)
            SQL = "SELECT MAX(Shape_Area) AS AREA FROM %s" % (x)
            cursor.execute(SQL)
            row = cursor.fetchone()
            val = row.AREA
            print str(val)
            SQL = "DELETE * FROM %s WHERE Shape_Area < %s" % (x, val)
            cursor.execute(SQL)

thanks, Tom


Django uses the Jinja template, so you can use its round filer. It works as follows:

template.html

<p>{{ VALUE| round(2, 'floor') }}</p>

Check out Jinja's documentation on topic

The SQL round function can also do this job:

SQL = "SELECT ROUND(MAX(Shape_Area), 2) AS AREA FROM %s" % (x)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜