如何从 TinyDB 数据库中检索单个值? [英] How to retrieve a single value from a TinyDB database?

查看:23
本文介绍了如何从 TinyDB 数据库中检索单个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在 Python 上使用 TinyDB,并且我已经掌握了基础知识(添加、删除、更新等).但现在我正在尝试从数据库中检索特定值.我正在使用的代码是在这个方法中:

I'm learning how to use TinyDB on Python and I have the basics working (add, remove, update etc.). But now I'm trying to retrieve specific values from the database. The code I'm using is in this method:

def showpassword():
    show = userdb.get(where("username") == txtname.get())
    txtshow.insert(0, show)

当我运行该方法时,它显示如下输出:

When I run the method, it displays the output as follows:

{'username': 'John', 'surname': 'Doe'}

如何才能只显示用户名或姓氏,不带任何括号?

How can I get it so that I can display only the user's name or surname and without any brackets?

推荐答案

TinyDb 将其数据存储为 JSON,在 Python 中表示为字典.

TinyDb stores its data as JSON which is represented in Python as a dictionary.

换行

txtshow.insert(0, show)

txtshow.insert(0, "{username} {surname}".format(**show)

查看此处,深入了解字符串格式.

Look here for a deeper dive in to string formatting.

这篇关于如何从 TinyDB 数据库中检索单个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆