如何从命令行访问django sqlite3数据库?

vawmfj5a  于 2023-05-29  发布在  SQLite
关注(0)|答案(1)|浏览(215)

我正在为一个django应用程序使用sqlite3数据库,它工作正常。我可以打印我在视图中编写的代码所使用的版本:

# views.py
import sqlite3
def my_view(..):
    connection = sqlite3.connect('C:/path/to/db.sqlite3')
    cursor = connection.cursor()
    cursor.execute('SELECT sqlite_version();')
    result = cursor.fetchone()
    sqlite_version = result[0]
    print("SQLite version:", sqlite_version) 
    ...

但是如果我从命令行运行python manage.py dbshell,我得到CommandError: You appear not to have the 'sqlite3' program installed or on your path.
有类似的问题,但答案例如建议下载sqlite3,我不认为这是有意义的,因为我可以看到我已经安装了一个版本
使用:Django 4.2,Python 3.10.4,sqlite3 3.37.2.在虚拟环境中。

qjp7pelc

qjp7pelc1#

要运行**python manage.py dbshell,您必须在系统中安装数据库客户端。例如,如果您正在使用PostgreSQL,则必须下载并安装PostgreSQL。SQLite也是如此。默认情况下,它随Python3一起提供,但它只是一个程序,而不是命令行工具。为了获得命令行工具,您必须下载SQLite并将其安装在系统上。有关更多详细信息,请查看dbshell**的Django文档。

相关问题