python - Search in DBF and update record -


code:

#!/usr/bin/python db = dbf.dbf("mest2.dbf") #line update: rec = db[0] #proest field of dbf. i'm assigning 1 field line 0 rec["proest"] = 1 rec.store() del rec db.close() 

image of dbf table: http://i.stack.imgur.com/1uhe1.jpg

my problem can not change records rows, cause position of products (procod) may vary.

any suggestions procod , change value of proest?

updated:

#!/usr/bin/python  import dbf db = dbf.table('mest2.dbf') db:     procod_idx = db.create_index(lambda rec: (rec.codigo, rec.procod))     match = procod_idx.search(match='000001')      # should 1 product code     record = match[0]        record:         record.proest = 23 

but question is, how edit value based on codigo field (stock code). have multiples stocks id: (1, 2, 5, 11). code update first result, need update specific record based in codigo field.

in sql be: "update proest set 32 codigo=11"... or codigo=2

solved ethan furman

#!/usr/bin/python  import dbf db = dbf.table('mest2.dbf') db:     procod_idx = db.create_index(lambda rec: (rec.codigo, rec.procod))     match = procod_idx.search(match=(11, '000001'))     record = match[0]     record:         record.proest = 25         record.dt_atualiz = '14/07/15 16:52' 

you don't dbf package using, looks mine.

what want create temporary index on procod field , can search on , update whichever other fields need to:

# untested import dbf db = dbf.table('mest2.dbf') db:     procod_idx = db.create_index(lambda rec: rec.procod)     # list of matching records     match = procod_idx.search(match='000001')     # should 1 product code     record = match[0]     record:         record.proest = ...         record.dt_atualiz = ... 

if product code not unique, above "should 1 product code comment" wrong.

change index to:

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

and search with:

    match = procod_idx.search(match=(11, '000001'))     record = match[0]     ...     match = procod_idx.search(match=(2, '000001'))     record = match[0]     ... 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -