I have a notion database that contains a list of file directories. I am writing a script that is going to update the names of all the files in that database so it will also need to update the filepaths in the database to reflect the new filenames.
我有一个包含文件目录列表的概念数据库。我正在编写一个脚本,该脚本将更新该数据库中所有文件的名称,因此它还需要更新数据库中的文件路径以反映新的文件名。
Database layout: Filepath | File_name (currently correct) | Additional Info
数据库布局:文件路径|文件名(当前正确)|其他信息
At present I have a function that reads in the data from filepath and file name and passes that through to a rename function that will rename the old file name with the one that is in the database. so /example/_test_example.mp4 would become /example/example_file.mp4 with example_file.mp4 being the value that was in the filename column.
目前,我有一个从文件路径和文件名中读取数据的函数,并将其传递给一个重命名函数,该函数将用数据库中的文件名重命名旧的文件名。因此,/Example/_TEST_Example.mp4将变为/Example/Example_File.mp4,而Example_File.mp4是FileName列中的值。
client=Client(auth=NOTION_TOKEN)
db_info=client.databases.retrieve(database_id=files_db)
rows=client.databases.query(database_id=files_db)
content_ids=[]
for row in rows['results']:
file_name=row['properties']['File_name']['rich_text'][0]['plain_text']
filepath=row['properties']['Filepath']['title'][0]['plain_text']
update_filename(file_name,filepath)
This code works as expected, getting both the filename and file path from the database. The rename function also works as expected so I wont bother posting that function here. It just replaces the current filename with the file name from the notion db using os.rename() and then passes the new filepath and the filename (passing the file name since I assume I am going to need to match the database row using it).
这段代码按预期工作,从数据库获取文件名和文件路径。重命名函数也按照预期工作,所以我不会在这里张贴该函数。它只是使用os.rename()将当前文件名替换为概念db中的文件名,然后传递新的文件路径和文件名(传递文件名,因为我假设我将需要使用它匹配数据库行)。
The code I have for updating the database is:
我拥有的用于更新数据库的代码是:
def update_db(video_file,new_file):
client.databases.update({
"database_id": files_db,
"properties":{
'filepath': {'title': [{'text': {'content': filepath}}]},
}
})
pprint('record inserted')
I know the update code wont work because I haven't been able to find out to do the "update x where y=y". Running it as it is I get the error that the database ID argument is missing. For writing to the database in the first place I used the client.pages.create(**{}).
我知道更新代码不会起作用,因为我还没有找到执行“UPDATE x where y=y”的方法。按原样运行它时,我得到的错误是缺少数据库ID参数。首先,为了写入数据库,我使用了client.pages.create(**{})。
Does anyone have an example of how the database update function should work? I did take a look at the notion api documentation, but I cant figure out how to implement that into using the notion-client package.
有谁有数据库更新功能应该如何工作的示例吗?我确实看了Concept API文档,但我想不出如何使用Concept-Client包来实现它。
更多回答
优秀答案推荐
If you're trying to update a property value for certain rows in a db, I had to use a for loop with:
如果您试图更新数据库中某些行的属性值,我必须使用带有以下内容的for循环:
client.pages.update(page_id= id, properties=update_data)
Each row when you queried the db should have an id and that is the page_id. My update_data was changing the date to a 'new_date':
查询数据库时的每一行都应该有一个id,那就是page_id。我的UPDATE_DATA正在将日期更改为‘new_date’:
update_data = {"Assigned Date": {"date": {"start": new_date, "end": None}}}
for index, row in df.iterrows():
id = row['id']
client.pages.update(page_id= pageID, properties=update_data)
print(f"Updated page with ID {id}")
更多回答
I'm not sure which row is getting updated? How does the update function know which row you are referring to?
我不确定哪一行正在更新?UPDATE函数如何知道您引用的是哪一行?
我是一名优秀的程序员,十分优秀!