【问题标题】:Write to several database in Redis for python在 Redis for python 中写入多个数据库
【发布时间】:2012-10-22 03:16:48
【问题描述】:

我想将一些值写入不同的数据库,这是我的代码:

import redis

r1 = redis.Redis(host='127.0.0.1', port=6379, db = 'db1')
r2 = redis.Redis(host='127.0.0.1', port=6379, db = 'db2')

numList = ['4', '3', '2', '1']

for num in numList:
   r1.lpush('a', num)
   r2.lpush('a', 'test')

print r1.lrange('a',start=0,end=-1)
print r2.lrange('a',start=0,end=-1)

然后我得到了这个结果:

['test', '1', 'test', '2', 'test', '3', 'test', '4']
['test', '1', 'test', '2', 'test', '3', 'test', '4']

虽然我使用不同的数据库,但是对于同一个键,所有的值都被放入。 谢谢。

【问题讨论】:

    标签: python database redis key-value-store


    【解决方案1】:

    数据库应该是zero-based numeric index (显然限制是15)。尝试使用

    r1 = redis.Redis(host='127.0.0.1', port=6379, db = 0)
    r2 = redis.Redis(host='127.0.0.1', port=6379, db = 1)
    

    【讨论】:

    • 你帮了我很多。谢谢。
    • 是的,我使用几乎相同的代码 redis_server = redis.StrictRedis(host="localhost", port=6379, charset="utf-8", decode_responses=True, db=0)跨度>
    • 没有 15 个数据库的限制。您可以将redis.config文件中的数据库数量设置为您想要的数量,然后选择0到'databases'-1之间的dbid。
    • @MikeL 我差不多 6 年前写了这个答案。我不记得我为什么要写这个限制。也许当时有这样的限制,但现在已经不存在了。谁知道?我不记得了。
    猜你喜欢
    • 1970-01-01
    • 2021-02-21
    • 2015-05-01
    • 2011-05-01
    • 2021-12-02
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 2013-09-20
    相关资源
    最近更新 更多