Doubt Python with Firebird database query

Asked

Viewed 108 times

0

Hello I have a problem with the Firebird database query. I make the connection with the bank and step this query, but the problem is in return, it returns the name of the normal product only that the return of the price comes with the "decimal type" described in front of the data, but being that the data type that is registered in the bank is Numeric.

cursor = conector.cursor()
cursor.execute("select e.descricao, e.preco_venda from estoque e where  '7853849020424' = e.barras")
lista = cursor.fetchall()

print(lista)

cursor.close()
conector.close()

A primeira linha é o retorno da consulta a segunda e o tipo de dado cadastrado no banco

Thank you very much

2 answers

1

That one Decimal it’s not like Decimal firebird that’s guy Decimal python

The decimal module is designed to support exact unrounded decimal arithmetic (fixed-point arithmetic) and floating-point round arithmetic.

To start using the decimal type you must first adjust your Operating context.

Contexts are environments for arithmetic operations. They govern accuracy, establish rules for rounding, determine which signs are treated as exceptions and limit the range of exponents.

The method decimal.getcontext() returns the Context for the active thread.

>>> from decimal import *
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])

This Context returned is the context in the absence. Their values are prec= 28, rounding= ROUND_HALF_EVEN, and rags to Overflow, InvalidOperation, and DivisionByZero.

To adjust one of the context properties:

>>> getcontext().prec = 7

To set a new context use the method decimal.setcontext() and pass as argument one of the predefined Contexts decimal.Defaultcontext, decimal.BasicContext and decimal.ExtendedContext, or create your own context with the constructor decimal.Context().

Decimal instances can be constructed from integer numbers, strings, floating point or tuples:

>>> Decimal(3) / Decimal(4)
Decimal('0.75')
>>> data = list(map(Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split()))
>>> max(data)
Decimal('9.25')
>>> min(data)
Decimal('0.03')
>>> sorted(data)
[Decimal('0.03'), Decimal('1.00'), Decimal('1.34'), Decimal('1.87'),
 Decimal('2.35'), Decimal('3.45'), Decimal('9.25')]
>>> sum(data)
Decimal('19.29')
>>> a,b,c = data[:3]
>>> str(a)
'1.34'
>>> float(a)
1.34
>>> round(a, 1)
Decimal('1.3')
>>> int(a)
1
>>> a * 5
Decimal('6.70')
>>> a * b
Decimal('2.5058')
>>> c % a
Decimal('0.77')

Transactions between decimals are closed to each other.

If you want the textual representation of a decimal convert it to string:

>>> str(Decimal(1) / Decimal(7))
'0.1428571'
>>> f'{Decimal(Decimal(2) * Decimal("0.34")):g}'
'0.68'

Or in your case:

cursor = conector.cursor()    
cursor.execute("select e.descricao, e.preco_venda from estoque e where  '7853849020424' = e.barras")
for row in cur.fetchall():
  print(f'Item: {row[0]}, Valor: {row[1]:g}')

cursor.close()
conector.close()

0

I was able to "bypass" or display the information like this:

        cursor.execute("select e.descricao, e.preco_venda from estoque e where  '"+barras+"' = e.barras")
        retorno = cursor.fetchall()
           
        print(retorno[0][0])
        print(retorno[0][1],"RS")

Because the information was on a tuple, so I was able to display the content of the position. inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.