python - 在内存中SQLite3共享数据库python

标签 python database amazon-web-services sqlite

我试图充分利用 aws 服务器,并想到跨多个线程使用内存数据库(在 python 中使用 SQLite 3),我在网上找到了这个命令:

    conn = sqlite3.connect('file::memory:?cache=shared')

但后来我得到了这个模糊的错误:

    sqlite3.OperationalError: unable to open database file

还有可能这样做吗?

最佳答案

仍然有可能。我刚刚在 MacOS 上针对 Python 3.6.0Python 2.7.13 进行了验证。

sqlite3.connect("file::memory:?cache=shared") 确实是连接数据库的正确方法。

import sqlite3
p = sqlite3.connect("file::memory:?cache=shared")
p.execute('CREATE TABLE foo (bar, baz)')
p.execute("INSERT INTO foo VALUES ('apple', 'orange')")
p.commit()

在另一个 python shell 中

import sqlite3
q = sqlite3.connect("file::memory:?cache=shared")
list(q.execute('SELECT * FROM foo'))

我的输出是[(u'apple', u'orange')]

对于您的问题“是否可以再这样做?”,答案是。所以问题出在你的系统上,因为你确认它可以在 aws 上运行(在下面的评论中)。

关于python - 在内存中SQLite3共享数据库python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44090121/

相关文章:

python - 我正在尝试设置一个 if 语句来删除 Sprite ,而图像消失 Sprite 仍然存在(不断激活 if 语句

python - 保存 Altair 图表,周围没有空白

python - ValueError : arrays must all be same length, json 链接

mysql - 获取属于具有两行以上父类别的行

amazon-web-services - ALB 规则限制

python - 递归斐波那契 yield

android - SQLite - 是否可以通过插入语句插入 BLOB?

php - fatal error key '1' 重复条目 'PRIMARY'

amazon-web-services - Elastic Beanstalk 的 nginx 配置不起作用

postgresql - 在 aws(ubuntu) 中安装 psycopg2 时出错,如何解决?