从一个 csv 更新多个表并在 update_or_insert 之后获取行 ID
update multiple tables from one csv and get row id after update_or_insert
我正在尝试构建一个 .csv 数据上传工具,它将按顺序更新两个 table。因此,我的 'person' table 与 'thing' table 具有一对多关系,即一个人可以拥有许多不同的东西。
我需要做的是从 'update_or_insert ' 之后的 'person' 行获取主键,然后这个 ID 将进入 'owner_id' 中对应行的 'thing' table.
关于我如何做到这一点有什么想法吗?请原谅我的编程,我是初学者!
型号
db.define_table('person',
Field('name'),
Field('age'),
Field('country'),
format='%(name)s')
db.define_table('thing',
Field('thing_name'),
Field('value'),
Field('location'),
Field('owner_id'))
控制器
导入 csv
def importer():
form = FORM(INPUT(_type='submit',_class="btn btn-primary"))
if form.process().accepted:
with open('C:/Users/matt/PycharmProjects/all_projects/web2py/applications/uploadTest/private/test.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
next(readCSV, None)
for row in readCSV:
name = row[0]
age = row[1]
country = row[2]
thing_name = row[3]
value = row[4]
location = row[5]
db.person.update_or_insert(name=name,age=age, country=country)
db.thing.insert(thing_name=thing_name,value=value,location=location)
return dict(form=form)
及以下数据
person.name,person.age,person.country,thing.name,thing.value,thing.location
matt,38,uk,teddy,12,house
matt,38,uk,kite,13,outside
matt,38,uk,screen,14,shed
matt,38,uk,teddy,15,garden
will,24,us,table,16,house
will,24,us,teddy,17,outside
will,24,us,kite,18,shed
will,24,us,screen,19,garden
pete,56,bel,teddy,20,house
pete,56,bel,table,21,outside
pete,56,bel,teddy,22,shed
pete,56,bel,kite,23,garden
pete,56,bel,screen,24,house
pete,56,bel,teddy,25,outside
matt,38,aus,teddy,26,shed
matt,38,can,kite,27,garden
matt,38,fr,screen,23,shed
pete,25,bel,teddy,22,shed
pete,25,bel,screen,74,shed
ps - 我正在本地获取文件,因为我还没有制定 .retrieve: 方法
.update_or_insert
不会 return 更新时的记录 ID,因此您不能使用该方法。相反,只需检查并获取记录以获取其 ID。例如:
for row in readCSV:
person = dict(zip(('name', 'age', 'country'), row[:3]))
record = db.person(**person) # Check for a match.
person_id = record.id if record else db.person.insert(**person)
thing = dict(zip('thing_name', 'value', 'location'), row[3:6])
db.thing.insert(owner_id=person_id, **thing)
请注意,db.person(**person)
语法 return 是与 person
字典中所有值匹配的第一条记录(假设您期望给定的数据最多匹配一次)或None
如果没有匹配项(详见 documentation)。
另外请注意,如果有匹配的 db.person
记录,则无需更新它,因为 CSV 文件中可用的所有字段都已用于查找匹配项(即,没有其他字段的值需要更新)。
我正在尝试构建一个 .csv 数据上传工具,它将按顺序更新两个 table。因此,我的 'person' table 与 'thing' table 具有一对多关系,即一个人可以拥有许多不同的东西。
我需要做的是从 'update_or_insert ' 之后的 'person' 行获取主键,然后这个 ID 将进入 'owner_id' 中对应行的 'thing' table.
关于我如何做到这一点有什么想法吗?请原谅我的编程,我是初学者!
型号
db.define_table('person',
Field('name'),
Field('age'),
Field('country'),
format='%(name)s')
db.define_table('thing',
Field('thing_name'),
Field('value'),
Field('location'),
Field('owner_id'))
控制器
导入 csv
def importer():
form = FORM(INPUT(_type='submit',_class="btn btn-primary"))
if form.process().accepted:
with open('C:/Users/matt/PycharmProjects/all_projects/web2py/applications/uploadTest/private/test.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
next(readCSV, None)
for row in readCSV:
name = row[0]
age = row[1]
country = row[2]
thing_name = row[3]
value = row[4]
location = row[5]
db.person.update_or_insert(name=name,age=age, country=country)
db.thing.insert(thing_name=thing_name,value=value,location=location)
return dict(form=form)
及以下数据
person.name,person.age,person.country,thing.name,thing.value,thing.location
matt,38,uk,teddy,12,house
matt,38,uk,kite,13,outside
matt,38,uk,screen,14,shed
matt,38,uk,teddy,15,garden
will,24,us,table,16,house
will,24,us,teddy,17,outside
will,24,us,kite,18,shed
will,24,us,screen,19,garden
pete,56,bel,teddy,20,house
pete,56,bel,table,21,outside
pete,56,bel,teddy,22,shed
pete,56,bel,kite,23,garden
pete,56,bel,screen,24,house
pete,56,bel,teddy,25,outside
matt,38,aus,teddy,26,shed
matt,38,can,kite,27,garden
matt,38,fr,screen,23,shed
pete,25,bel,teddy,22,shed
pete,25,bel,screen,74,shed
ps - 我正在本地获取文件,因为我还没有制定 .retrieve: 方法
.update_or_insert
不会 return 更新时的记录 ID,因此您不能使用该方法。相反,只需检查并获取记录以获取其 ID。例如:
for row in readCSV:
person = dict(zip(('name', 'age', 'country'), row[:3]))
record = db.person(**person) # Check for a match.
person_id = record.id if record else db.person.insert(**person)
thing = dict(zip('thing_name', 'value', 'location'), row[3:6])
db.thing.insert(owner_id=person_id, **thing)
请注意,db.person(**person)
语法 return 是与 person
字典中所有值匹配的第一条记录(假设您期望给定的数据最多匹配一次)或None
如果没有匹配项(详见 documentation)。
另外请注意,如果有匹配的 db.person
记录,则无需更新它,因为 CSV 文件中可用的所有字段都已用于查找匹配项(即,没有其他字段的值需要更新)。