Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在cassandra python中插入列表_Python_Database_String_Cassandra_Formatting - Fatal编程技术网

在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"])
    )