Generate autoincrement serial number sequence

Asked

Viewed 176 times

2

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

Follows 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 mistake:

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

2 answers

1


Use the cursor.lastrowid to get the last ID inserted into the cursor object, or use connection.insert_id() to get the ID of the last Insert done on the connection.

But since you said it’s a no auto increment field, it would be nice for you to make a:

select max(ordem_serv_num), name from ordem_serv

so you would have the largest number registered.

  • could give me an example: and that I don’t have much experience

  • @Diuniorronaldobrumlauser I did just that use the cursor already instantiated by adding the lastrowid to validate ....

  • it worked, however it returns me 1 and not the next sequence. ex: 000570370 the next one would be 000570371

  • @Diuniorronaldobrumlauser I believe it helps you now

  • solved this question thank you, now my problem and take this data and add it to +1 to generate the sequence, ex: 000570370 to 000570371

  • @Diuniorronaldobrumlauser you take the return and put +1, it is very simple, take the opportunity to mark the question as solved

  • I’m going to create another theme to see because I’ve already picked up the return and added one more just not right

  • Perfect @Diuniorronaldobrumlauser it’s nice to separate ask for topics so it’s easier to find

Show 3 more comments

1

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

Like 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
  • I like your answer more elaborate than mine

  • returns the number 1 and not the next sequence of numbers ex: 000570370 the next one would be 000570371

  • @Diuniorronaldobrumlauser Is correct. lastrowid will return the generated value according to the auto-increment column.

  • in fact I need to consult the last sequence registered in the bank and generate the next and insert in my Entry, only that this column is not auto-increment.

Browser other questions tagged

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