Girard clément
Girard clément

Reputation: 49

How to parse (Decimal('str'),) with python

In my view I make a call to a stored procedure that return to me the ID of the entry it created.

I need to use this id as arguments for another stored procedure.

My problem is that the result of the first query is : (Decimal('1046'),) and of course wont fit as argument for the second procedure.

How can I parse it to only get the '1046' ?

Edit :

View.py :

def dictfetchall(cursor):
    columns = [col[0] for col in cursor.description]
    return [
        dict(zip(columns, row))
        for row in cursor.fetchall()
    ]

def mouvementCreation(request):
    idMI = 0
    especes = TbEspece.objects.order_by('id')
    #Get Mouvement informations

    #Connection to 'erp-site' DB 
    cursor = connections['erp-site'].cursor()
    try:
        #Get Produits list from Espece
        query = "{CALL SP_webGET_PRODUIT_FROM_ESPECE(%s,%s,%s,%s,%s)}"
        arguments = (2016, 'C', 0, 10, 'A',)
        cursor.execute(query, arguments)
        produits = dictfetchall(cursor)

        #Get Transporters list
        cursor.execute("{CALL SP_webGET_TRANSPORT}")
        transporters = dictfetchall(cursor)

        #Get Livreur list
        cursor.execute("{CALL SP_webGET_LIVREUR}")
        livreurs = dictfetchall(cursor)
    finally:
        cursor.close()       

    cursor = connections['site'].cursor()
    try:
        #Get Circuit list
        cursor.execute("{CALL SP_webGET_CIRCUIT_FOR_MVT}")
        circuits = dictfetchall(cursor)

        #Get Source list
        cursor.execute("{CALL SP_webGET_SOURCE_FOR_MVT}")
        mvtsources = dictfetchall(cursor)

        #Get Dest list
        cursor.execute("{CALL SP_webGET_DEST_FOR_MVT}")
        destinations = dictfetchall(cursor)

        #Get PontBascule list
        cursor.execute("{CALL SP_webGET_PBASCULE}")
        pontBascules = dictfetchall(cursor)
    finally:
        cursor.close()

    reg_normes = TbRegauxnormes.objects.all()
    ordreexecs = TbOrdreexecution.objects.all()
    if request.method == 'POST':
        typemouvement = request.POST.get('typemouvement')
        soustype = request.POST.get('soustype')
        recolte = request.POST.get('recolte') 
        groupe = request.POST.get('groupe')
        categorie = request.POST.get('categorie')
        code = request.POST.get('code')
        collecte = request.POST.get('collecte')
        vente = request.POST.get('vente')
        stock = request.POST.get('stock')
        achat = request.POST.get('achat')
        transporteur = request.POST.get('transporteur')
        blLivreur = request.POST.get('blLivreur', '')
        contratClient = request.POST.get('contratClient')

        pont1 = request.POST.get('pont1')               # BIGINT
        numTicket = request.POST.get('numTicket')       # INT
        dateheure1 = request.POST.get('dateheure1')     # DATETIME
        poid1 = request.POST.get('poid1')               # INT
        dsd1 = request.POST.get('dsd1')                 # INT
        pont2 = request.POST.get('pont2')               # BIGINT
        dateheure2 = request.POST.get('dateheure2')     # DATETIME
        poid2 = request.POST.get('poid2')               # INT
        dsd2 = request.POST.get('dsd2')                 # INT
        p1p2 = request.POST.get('p1p2')                 # INT
        livreur = request.POST.get('idlivreur')         # BIGINT
        vehicule = request.POST.get('vehicule')         # VARCHAR
        comTicket = request.POST.get('comTicket')       # VARCHAR
        comLogiciel = request.POST.get('comLogiciel')   # VARCHAR
        espece = request.POST.get('espece')             # BIGINT
        produit = request.POST.get('produit')           # BIGINT
        #Connection to 'erp-site' DB 

        cursor = connections['pontbascule'].cursor()
        try:
            query = "{CALL SP_webADD_MANUAL_PESEE(%s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s)}"
            arguments = (pont1, numTicket, dateheure1, poid1, dsd1,pont2, numTicket, dateheure2, poid2, dsd2, p1p2,livreur, vehicule,comTicket, comLogiciel,espece, produit)
            cursor.execute(query, arguments)
            #Here i recieve the (Decimal('1046'),)
            s = cursor.fetchone()
            idCreatedPesee = s[0]
        finally:
            cursor.close()
        cursor = connections['site'].cursor()
        try:
           #Here im supposed to send it as argument to this procedure
            query = "{CALL SP_webCREATE_MVT_INIT(%s)}"
            arguments = (idCreatedPesee)
            cursor.execute(query, arguments)
            idCreatedMVT = dictfetchall(cursor)
        finally:
            cursor.close()
    return render(request, 'mouvementCreation.html', {'especes' : especes, 'produits' : produits, 'transporters' :  transporters, 'livreurs' : livreurs, 'circuits' : circuits, 'mvtsources' : mvtsources, 'destinations' : destinations, 'pontBascules' : pontBascules} )

Upvotes: 0

Views: 820

Answers (3)

Girard clément
Girard clément

Reputation: 49

Since nothing worked i just used CONVERT on my query to be sure to get a string in the end.

Upvotes: 0

RemcoGerlich
RemcoGerlich

Reputation: 31270

It's an instance of the decimal.Decimal class, part of Python's standard library. It stores decimal fractions exactly, unlike normal floating point numbers. As the database does something similar, translating the database's numbers to Decimals is the most correct way of translating them to Python.

They're mostly just numbers in Python.

>>> from decimal import Decimal
>>> Decimal('12.3') + 4
Decimal('16.3')

If you know it's an integer and you need to pass an integer to somewhere, pass int(yourdecimal).

That you get (Decimal('1046'),) with the parens and the comma means you get the result as a tuple of length 1. You can access its first element by indexing it with [0], like with all tuples.

Upvotes: 0

fixmycode
fixmycode

Reputation: 8506

type casting in python is as simple as:

my_decimal = Decimal('1046')
as_a_string = str(my_decimal) # '1046', a string
as_an_int = int(my_decimal) # 1046, an integer

Upvotes: 4

Related Questions