首页 > 解决方案 > 相同的 python 解释器,但 json_set 仅不适用于 Visual Studio Code,适用于其他任何地方

问题描述

我无法绕过我的头。

完全相同的代码,完全相同的解释器,在其他任何地方都可以工作,但不能在Visual Studio Code.

让我们考虑以下示例:

import sqlite3


_connection = sqlite3.connect(":memory:")
_connection.row_factory = sqlite3.Row

create_table = """
    create table food (
        id int not null,
        data json not null,
        primary key(id)
    )
"""

insert = """
    insert into food (id, data)
    values (1, '{"name": "Steak"}')
"""

update = """
    update food
    set data = json_set(data, '$.name', 'Bacon and Eggs')
    where id = 1
"""

fetch_query = "select json_extract(data, '$.name') as name from food where id = 1"

with _connection:
        _connection.execute(create_table)
        _connection.execute(insert)
        _connection.execute(update)
        food = _connection.execute(fetch_query)

new_food = dict(food.fetchone()).get('name')
print(new_food)

它应该打印出来Bacon and EggsVisual Studio Code抱怨json_set不是一个功能。

如何检查内部sqlite3使用的版本是什么Visual Studio Code?不知道为什么从 vs 代码运行它与从外部终端运行它的行为不同。

更新 1

Linux 终端

在系统终端上工作

视觉工作室代码 没有这样的功能:json_set

更新 2

我不依赖 VS 代码来确定要使用哪个版本的 python。我正在明确输入它。估计和pip我没关系。pip反正我用的是同一个版本。

与代码 未找到 json_set 函数

Linux终端 工作正常

标签: pythonsqljsonsqlitevisual-studio-code

解决方案


谢谢吉尔和肖恩。

原来Visual Studio Code是使用与sqlite3系统不同的引擎盖下版本。

我只好pip install pysqlite3-binary

import pysqlite3 as sqlite3
...

其他一切都没有改变。

我很确定还有其他方法可以解决这个问题,但我就是这样做的。


推荐阅读