Generate autoincrement serial number sequence

1

I want to generate a service order number, I have the code, but I do want it to query the last number of the service order that was generated in the database.

Follow the code:

 def rangeOrdemServ(self):
        db = self.conexaoBanco()
        cursor = db.cursor()
        cursor.execute("select ordem_serv_num from ordem_serv where id = LAST_INSERT_ID()")
        dadosOrdemServ = cursor.fetchall()
        db.commit()
        global rec
        pStart = dadosOrdemServ
        pInterval = +1
        if (rec == 0):
            dado = pStart + pInterval
            self.entrOrdServ.insert(END,dado)
        else:
            rec += pInterval
        return rec

The error:

Traceback (most recent call last):
  File "E:/CharIp ServerFTP_1.1/CharIP_1.1.7/Ordem_Servicos.py", line 152, in <module>
    sis(root)
  File "E:/CharIp ServerFTP_1.1/CharIP_1.1.7/Ordem_Servicos.py", line 21, in __init__
    self.telaOrdServ()
  File "E:/CharIp ServerFTP_1.1/CharIP_1.1.7/Ordem_Servicos.py", line 108, in telaOrdServ
    self.rangeOrdemServ()
  File "E:/CharIp ServerFTP_1.1/CharIP_1.1.7/Ordem_Servicos.py", line 119, in rangeOrdemServ
    dado = pStart + pInterval
TypeError: can only concatenate tuple (not "int") to tuple
    
asked by anonymous 04.10.2016 / 16:18

2 answers

1

Use cursor.lastrowid to get the last ID inserted in the cursor object, or use connection.insert_id() to get the ID of the last insert made on the connection.

But since you said it's a non-auto increment field, it would be nice to do one:

select max (servername), name from servername

so you would have the highest number.

    
04.10.2016 / 16:26
1

Depending on the error message, cursor.fetchall returns a list of tuples , so the concatenation you want to do is not possible.

As mentioned by Otto , you can use cursor.lastrowid to return the desired value.

# ....
pStart = cursor.lastrowid
pInterval += 1

if (rec == 0):
    dado = pStart + pInterval
    self.entrOrdServ.insert(END,dado)
else:
    rec += pInterval
    
04.10.2016 / 16:35