当cassandra python驱动程序从用户GUI接受参数时出现错误消息

时间:2017-06-22 10:15:41

标签: cassandra-2.0

我正在使用cassandra 2.1和CQL 3.2.1,我想让用户从UI指定键空间名称,复制策略,复制因子,然后将这些值传递给查询以执行Insert CQL,但是给我一个语法错误,我尝试了很多,但没有去写>> 我正在创建密钥空间 - >连 和列族 - > keyspaces 但插入导致错误 这是我的代码:

from cassandra.cluster import Cluster

class Connection():

    def __init__ (self , ips , keyspace ,replication_strategy ,replication_factor):

        self.keyspace=keyspace
        self.ips =ips
        self.replication_strategy=replication_strategy
        self.replication_factor=replication_factor
        cluster = Cluster([ips])
        session = cluster.connect() 
        session.execute("CREATE keyspace IF NOT EXISTS connect with replication={ 'class' : 'SimpleStrategy', 'replication_factor' :1}")
        session.execute("CREATE TABLE IF NOT EXISTS connect.keyspaces (id int primary key , keyspaces_name text, replication_strategy text, replication_factor int)")
        session.execute("INSERT INTO connect.keyspaces(id , keyspaces_name , replication_strategy ,replication_factor ) VALUES (1 " +','  + self.keyspace + ',' + self.replication_strategy +',' + self.replication_factor + ")")

and the ERROR MESSAGE IS :
File "cassandra/cluster.py", line 3822, in cassandra.cluster.ResponseFuture.result (cassandra/cluster.c:74332)
    raise self._final_exception
SyntaxException: <Error from server: code=2000 [Syntax error in CQL query] message="line 1:125 no viable alternative at input ',' (...) VALUES (1 ,noon,[SimpleStrategy],...)">

1 个答案:

答案 0 :(得分:0)

这意味着您正在构建的INSERT语句中存在语法错误。如果您打印已构建的字符串查询,则可能更容易进行故障排除。

或者我建议参数化您的查询以让驱动程序进行格式化: http://datastax.github.io/python-driver/getting_started.html#passing-parameters-to-cql-queries