1

Code:

#!/usr/bin/python
db = dbf.Dbf("MEST2.DBF")
#LINE TO UPDATE:
rec = db[0]
#PROEST IS A field of my dbf. I'm assigning 1 to this field line 0
rec["PROEST"] = 1
rec.store()
del rec
db.close()

IMAGE OF DBF TABLE: https://i.stack.imgur.com/1UHE1.jpg

My problem is I can not change the records by rows, cause the position of the products (PROCOD) may vary.

Any suggestions to get the PROCOD and change the value of PROEST?

UPDATED:

#!/usr/bin/python

import dbf
db = dbf.Table('MEST2.DBF')
with db:
    procod_idx = db.create_index(lambda rec: (rec.codigo, rec.procod))
    match = procod_idx.search(match='000001')

    # should only be one product with that code
    record = match[0]   
    with record:
        record.proest = 23

But the question now is, how i edit the value based on the CODIGO field (Stock code). I have multiples stocks ID: (1, 2, 5, 11). The code update just the first result, i need update a specific record based in the CODIGO FIELD.

In SQL would be: "UPDATE PROEST SET 32 where CODIGO=11"... or CODIGO=2

SOLVED by Ethan Furman

#!/usr/bin/python

import dbf
db = dbf.Table('MEST2.DBF')
with db:
    procod_idx = db.create_index(lambda rec: (rec.codigo, rec.procod))
    match = procod_idx.search(match=(11, '000001'))
    record = match[0]
    with record:
        record.proest = 25
        record.dt_atualiz = '14/07/15 16:52'
user2925795
  • 400
  • 10
  • 29

1 Answers1

2

You don't say which dbf package you are using, but it looks like mine.

What you want to do is create an temporary index on the PROCOD field and then you can search on it and update whichever other fields you need to:

# untested
import dbf
db = dbf.Table('MEST2.DBF')
with db:
    procod_idx = db.create_index(lambda rec: rec.procod)
    # get a list of all matching records
    match = procod_idx.search(match='000001')
    # should only be one product with that code
    record = match[0]
    with record:
        record.proest = ...
        record.dt_atualiz = ...

If the product code is not unique, then the above "should only be one product with that code comment" is wrong.

Change the index to:

procod_idx = db.create_index(lambda rec: (rec.codigo, rec.procod))

and then search with:

    match = procod_idx.search(match=(11, '000001'))
    record = match[0]
    ...
    match = procod_idx.search(match=(2, '000001'))
    record = match[0]
    ...
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
  • Result: Traceback (most recent call last): File "python3.py", line 8, in record = match[0] File "C:\Python27\lib\site-packages\dbf\ver_2.py", line 6370, in __getitem__ raise NotFoundError("Record %d is not in list." % key) dbf.ver_2.NotFoundError: 'Record 0 is not in list.' – user2925795 Jul 14 '15 at 16:33
  • Yeah, this package is yours! procod_idx ..its correct? – user2925795 Jul 14 '15 at 16:43
  • The error is saying that it couldn't find any records with `000001` as a product code -- is that a correct product code? – Ethan Furman Jul 14 '15 at 17:03
  • The product exist... Attach: http://i.imgur.com/4re5V6U.jpg Another detail is that I have several records with this code, due to different stocks. (eg: 000001 in Stock 1 and Stock 2, where the table is represented by the CODE field .. – user2925795 Jul 14 '15 at 17:58
  • Thanks! Your code worked, it updates the stock! However only the first record with that code. Product 000001 has 12 stocks, I need to update in a specific stock (which is not the first) Only the date is giving error: "IndentationError: unexpected indent" I put as follows: record.dt_atualiz = "14/07/15 15:04" and tried so record.dt_atualiz = '14 / 07/15 15:04 ' – user2925795 Jul 14 '15 at 18:10
  • How do you know which stock? You should probably add that to the `create_index` call. – Ethan Furman Jul 14 '15 at 18:17
  • thank you for helping me... The MEST2.DBF has the CODE column, this is the identifier for the stock. the attach you can see who has the record 000001 It is on multiple lines with different codes of stock. http://i.imgur.com/FlrEQm6.jpg – user2925795 Jul 14 '15 at 18:32
  • Can you help me to create this index? (updating based on the CODIGO field? In SQL would be: "UPDATE PROEST SET 32 where CODIGO=11"... or CODIGO=2 – user2925795 Jul 14 '15 at 19:35
  • @user2925795: Updated my answer. – Ethan Furman Jul 14 '15 at 19:42
  • Thanx, Ethan. ((: Solved my problem. – user2925795 Jul 14 '15 at 19:54
  • I'm sorry for so many questions, but I'm starting with python. Hard to find some specific answers. How i put a variable in this line: match = procod_idx.search(match=(3, '000001')) like this: procod = '000001' match = procod_idx.search(match=(3, procod)) – user2925795 Jul 14 '15 at 20:36
  • Checkout the [Python dBase group](https://groups.google.com/forum/#!forum/python-dbase), or ask on the [Python mailing list](https://mail.python.org/mailman/listinfo/python-list%C2%A0). Lots of helpful people (including me ;) . – Ethan Furman Jul 14 '15 at 21:31