在cassandra python中插入列表
我在卡桑德拉有一张桌子,像:在cassandra python中插入列表,python,database,string,cassandra,formatting,Python,Database,String,Cassandra,Formatting,我在卡桑德拉有一张桌子,像: CREATE TABLE IF NOT EXISTS article( url text PRIMARY KEY, title text, author text, sources list<text>) 问题是我应该使用什么来代替?来正确格式化字符串您应该对所有类型的参数使用%s,而不仅仅是字符串 因此,请使用%s,以下是完整的代码 def save_to_cassandr
CREATE TABLE IF NOT EXISTS article(
url text PRIMARY KEY,
title text,
author text,
sources list<text>)
问题是我应该使用什么来代替
?
来正确格式化字符串您应该对所有类型的参数使用%s,而不仅仅是字符串因此,请使用%s,以下是完整的代码
def save_to_cassandra(self, session):
session.execute(
"""
INSERT INTO article (url, title, author, sources)
VALUES (%s, %s, %s, %s)
""",
(self.url, self.title, self.author, ["http://source1.com", "http://source2.com"])
)
资料来源:
另一件事,你应该总是使用事先准备好的语句
准备好的语句是由Cassandra解析并保存以供以后使用的查询。当驱动程序使用准备好的语句时,它只需要发送要绑定的参数值。这降低了Cassandra内的网络流量和CPU利用率,因为Cassandra不必每次都重新解析查询
def save_to_cassandra(self, session):
session.execute(
"""
INSERT INTO article (url, title, author, sources)
VALUES (%s, %s, %s, %s)
""",
(self.url, self.title, self.author, ["http://source1.com", "http://source2.com"])
)