小编Ran*_*ian的帖子

多个 SQLite 连接到 :memory 中的数据库:

是否可以从不同线程访问内存中的 SQLite 数据库?

在以下示例代码中,我在内存中创建了一个 SQLite 数据库并创建了一个表。当我现在转到不同的执行上下文时,我认为当我转到不同的线程时必须这样做,创建的表不再存在。如果我打开一个基于文件的 SQLite 数据库,表就会在那里。

我可以为内存数据库实现相同的行为吗?

from peewee import *
db = SqliteDatabase(':memory:')

class BaseModel(Model):
    class Meta:
        database = db

class Names(BaseModel):
    name = CharField(unique=True)

print(Names.table_exists())  # this returns False 
Names.create_table()
print(Names.table_exists())  # this returns True

print id(db.get_conn())  # Our main thread's connection.

with db.execution_context():
    print(Names.table_exists())  # going to another context, this returns False if we are in :memory: and True if we work on a file *.db
    print id(db.get_conn())  # A separate connection.

print id(db.get_conn())  # …
Run Code Online (Sandbox Code Playgroud)

python sqlite peewee

4
推荐指数
1
解决办法
2749
查看次数

Peewee 按需添加列

我有一个 sqlite 数据库,用作我在 python 中开发的应用程序的数据存储文件。

现在新功能的开发需要我在数据库中定义新的字段。有没有办法使用 peewee 加载数据库文件,该文件使用旧表定义(没有新字段)而不会SQLError: no such column出错?

就像在数据库中自动插入具有默认值的新字段一样。这将使向后兼容打开以前版本的数据库文件变得更加容易。

python sqlite peewee

3
推荐指数
1
解决办法
3645
查看次数

readthedocs 上的 nbsphinx 语法突出显示

我有一个 readthedocs sphinx 文档,它使用 nbsphinx 显示 jupyter 笔记本。当我使用 autobuild 在本地构建文档时,我在 jupyter 笔记本的代码单元格中突出显示了语法。但在 readthedocs 上,代码单元没有任何语法突出显示。

我是否必须激活某些东西才能在这些单元格中突出显示语法?

python python-sphinx read-the-docs jupyter-notebook

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