在 DBF 中搜索并更新记录
Search in DBF and update record
代码:
#!/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()
DBF 图片TABLE:http://i.stack.imgur.com/1UHE1.jpg
我的问题是我无法按行更改记录,因为产品的位置 (PROCOD) 可能会有所不同。
有什么获取 PROCOD 和更改 PROEST 值的建议吗?
更新:
#!/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
但现在的问题是,我如何根据 CODIGO 字段(股票代码)编辑值。我有多个股票 ID:(1、2、5、11)。代码只更新第一个结果,我需要更新基于 CODIGO FIELD 的特定记录。
在 SQL 中将是:"UPDATE PROEST SET 32 where CODIGO=11"... 或 CODIGO=2
已解决 作者: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'
你没有说你使用的是哪个 dbf
包,但它看起来像 mine.
您要做的是在 PROCOD
字段上创建一个临时索引,然后您可以搜索它并更新您需要的任何其他字段:
# 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 = ...
如果产品编码不唯一,则上面的"should only be one product with that code comment"是错误的
将索引更改为:
procod_idx = db.create_index(lambda rec: (rec.codigo, rec.procod))
然后搜索:
match = procod_idx.search(match=(11, '000001'))
record = match[0]
...
match = procod_idx.search(match=(2, '000001'))
record = match[0]
...
代码:
#!/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()
DBF 图片TABLE:http://i.stack.imgur.com/1UHE1.jpg
我的问题是我无法按行更改记录,因为产品的位置 (PROCOD) 可能会有所不同。
有什么获取 PROCOD 和更改 PROEST 值的建议吗?
更新:
#!/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
但现在的问题是,我如何根据 CODIGO 字段(股票代码)编辑值。我有多个股票 ID:(1、2、5、11)。代码只更新第一个结果,我需要更新基于 CODIGO FIELD 的特定记录。
在 SQL 中将是:"UPDATE PROEST SET 32 where CODIGO=11"... 或 CODIGO=2
已解决 作者: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'
你没有说你使用的是哪个 dbf
包,但它看起来像 mine.
您要做的是在 PROCOD
字段上创建一个临时索引,然后您可以搜索它并更新您需要的任何其他字段:
# 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 = ...
如果产品编码不唯一,则上面的"should only be one product with that code comment"是错误的
将索引更改为:
procod_idx = db.create_index(lambda rec: (rec.codigo, rec.procod))
然后搜索:
match = procod_idx.search(match=(11, '000001'))
record = match[0]
...
match = procod_idx.search(match=(2, '000001'))
record = match[0]
...