首页 文章

使用DataStax客户端将参数传递给Cassandra CQL查询

提问于
浏览
12

我使用datastax作为连接cassandra的客户端 . 我已经通过Java成功连接到cassandra集群/密钥空间/列系列 . 我正在尝试,对cassandra专栏家族thriugh java发出一些疑问 . 对我来说,它适用于简单的查询

ResultSet results = session.execute("select * from demodb.customer where id = 1");

现在我想从用户获取id参数并将其传递给 session.execute(); 语句 . 我该怎么办呢?

2 回答

  • 18

    您需要创建一个准备好的语句 . 然后,您需要将该语句与您从用户获得的ID值绑定 . 然后您可以执行bound语句 .

  • 0

    下面是使用预准备语句插入有关图像的数据的代码示例 .

    PreparedStatement statement = getSession().prepare(
                                   "INSERT INTO pixelstore.image " +
                                   "(image_name, " +
                                   " upload_time, " + 
                                   " upload_by, " + 
                                   " file_type, " + 
                                   " file_size" +
                                   ") VALUES (?, ?, ?, ?, ?);"); 
    
    // create the bound statement and initialise it with your prepared statement
    BoundStatement boundStatement = new BoundStatement(statement);
    
    session.execute( // this is where the query is executed
      boundStatement.bind( // here you are binding the 'boundStatement'
        "background", TimeUtil.getTimeUUID(),  "lyubent", "png", "130527"));
    

    最近有两篇关于星球cassandra的博客文章,其中包含了驱动程序可以执行的操作的演示,它们包含代码示例,因此请查看它们:

相关问题