2013-04-23 50 views
1

我得到這個問題,我使用擷取結果(使用fetchall在這個例子中)的任何pyodbc API中提取方法,十進制值取整到兩個地方。在MSSQL數據庫中,更特別是在SQL管理工作室中,當我執行存儲過程時,餘額,借方和貸方的值正確顯示爲4個地方。pyodbc SQLSERVER光標小數四捨五入

我試圖格式化結果集我使用fetchall後它會顯示四個小數位,但它仍然四捨五入它雖然。例如14.5272 => 14.5300。

在模板還我嘗試使用floatformat過濾無濟於事它仍然保持四捨五入:

<td >{{ Accounts.debits|floatformat:4 }}</td> 

models.py

from django.core.cache import cache 
from django.conf import settings 

class Account(models.Model):                        
    account = models.IntegerField(primary_key=True, db_column='Account') 
    description = models.TextField(db_column='Description')      
    balance = models.DecimalField(decimal_places=4, max_digits=19, db_column='Balance') 
    debits = models.DecimalField(decimal_places=4, max_digits=19, db_column='Debits') 
    credits = models.DecimalField(decimal_places=4, max_digits=19, db_column='Credits')               

class Meta:                              
    db_table = 'Account'                        
    permissions = (("view_accounts", "Can view accounts"),             
    )                                

    @staticmethod                              
    def resultSet(startDate, endDate):                         
     try:                         
      cur = connection.cursor()                                                          
      cacheKey = 'Account_%s_%s' %(startDate, endDate)                   
      cachedSp = cache.get(cacheKey)                        
      print cacheKey                            
      if not cachedSp:                           
       print 'not cached'                          
       cur.execute('database.dbo.AccountsSelect %s, %s', (str(startDate), str(endDate)))    
       cachedSp = cur.fetchall()                        
       #for row in cachedSp:                         
        #if row[5]!= -1:                         
         #print format(row['debit'],'%4f')                    
       cachedSp = cur.fetchall()                        
       cur.close()       

       cache.set(cacheKey,cachedSp,settings.CACHE_CUSTOM_APP_SECONDS)               
     except Exception, e:                           
      print e.args                            

     return [Account(*row) for row in cachedSp]  

以下是ODBC/freetds的配置文件。我使用SQL Server 2008.Freetds v1.12,django-pyodbc v0.9和pyodbc v3.0.6。

freetds.conf

[global] 
    # TDS protocol version 
    ; tds version = 8.0 
    # Whether to write a TDSDUMP file for diagnostic purposes 
    # (setting this to /tmp is insecure on a multi-user system) 
    ; dump file = /tmp/freetds.log 
    ; debug flags = 0xffff 

    # Command and connection timeouts 
    ; timeout = 10 
    ; connect timeout = 10 

    # If you get out-of-memory errors, it may mean that your client 
    # is trying to allocate a huge buffer for a TEXT field. 
    # Try setting 'text size' to a more reasonable limit 
    text size = 64512 

[tes-db-1] 
    host = tes-db-1.doamin.test.com 
    port = 1433 
    tds version = 8.0 

ODBC.INI

[default] 
Driver = FreeTDS 
Description = ODBC connection via FreeTDS 
Trace = No 
Servername = SERVER 
Database = DATABASE 

odbcinist.ini

[FreeTDS] 
Description  = TDS Driver (Sybase/MS-SQL) 
Driver   = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so 
Setup   = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so 
CPTimeout  = 
CPReuse   = 
FileUsage  = 1 
client charset = utf-8 

另外,我有我的設置文件(INSTALLED_APPS)和工程應用同步正確,我在我的所有應用程序中使用緩存,我不認爲這是問題的根源,如果這應該很重要。

除此之外四捨五入問題,一切工作正常,任何幫助表示讚賞。

+0

什麼版本pyodbc和SQL Server驅動程序? – Bryan 2013-04-25 15:25:17

+0

我使用freetds v 1.12(通過apt-get安裝)。 pyodbc v3.0.6和django-pyodbc v0.9(均通過pip)。 SQL Server 2008. Ubuntu 12.04。感謝您的查詢,Beargle – Adel 2013-04-25 18:52:48

+0

事實上,你正在使用freetds的是信息的重要的一點,請更新你的'freetds.conf'文件的副本的問題。 – Bryan 2013-04-26 12:41:20

回答

1

我想通了。所以我打電話的存儲過程中,浮動字段的類型爲MONEY,並且正在四捨五入爲2個位置。我將它改爲十進制(7,4)現在一切正常。我沒有對odbc/freetds配置進行任何更改,也不需要使用過濾器浮動格式。再次感謝,beargle