小编use*_*834的帖子

如何使用 psycopg2 从具有键值对的 postgres 表中检索某个键的值

我是新手python,正在尝试使用psycopg2阅读Postgres

我正在从名为部署的数据库表中读取数据,并尝试处理具有三个字段 id、Key 和 value 的表中的值。

import psycopg2
conn = psycopg2.connect(host="localhost",database=database, user=user, password=password)
cur = conn.cursor()
cur.execute("SELECT \"Value\" FROM deployment WHERE (\"Key\" = 'DUMPLOCATION')")
records = cur.fetchall()
print(json.dumps(records))
[["newdrive"]]
Run Code Online (Sandbox Code Playgroud)

我希望这只是“newdrive”,这样我就可以在下一行中进行字符串比较以检查它是否是“newdrive”

我在 json.dumps 输出上尝试了 json.loads ,但没有成功。

>>> a=json.loads(json.dumps(records))
>>> print(a)
[['newdrive']]

I also tried to print just the records without json.dump
>>> print(records)
[('newdrive',)]
Run Code Online (Sandbox Code Playgroud)

python postgresql psycopg2

2
推荐指数
1
解决办法
4372
查看次数

标签 统计

postgresql ×1

psycopg2 ×1

python ×1