当前位置: 首页 > 知识库问答 >
问题:

Cassandra中的预准备语句与绑定语句?

昌博易
2023-03-14

我想知道使用边界语句准备语句有什么好处?

PreparedStatement statement = session.prepare(
                      "INSERT INTO simplex.songs " +
                      "(id, title, album, artist) " +
                      "VALUES (?, ?, ?, ?);");

BoundStatement boundStatement = new BoundStatement(statement);
            session.execute(boundStatement.bind(
                  UUID.fromString("756716f7-2e54-4715-9f00-91debea6cf50"),
                  "La Petite Tonkinoise",
                  "Bye Bye Blackbird",
                  "Joséphine Baker");

最简单的方法是:

PreparedStatement ps = session.prepare(
                      "INSERT INTO simplex.songs " +
                      "(id, title, album, artist, tags) " +
                      "VALUES (?, ?, ?, ?, ?);");
ps.bind(UUID.fromString("756716f7-2e54-4715-9f00-91debea6cf50"),
                      "La Petite Tonkinoise",
                      "Bye Bye Blackbird",
                      "Joséphine Baker");

如您所见,我可以将数据绑定到< code>preparedStatement而无需< code>boundStatements。< code>boundStatement在哪里有用?

共有2个答案

慕容修伟
2023-03-14

对我来说,当您在准备语句上调用bind(…)时,会自动创建边界语句。您也可以直接创建实例Bound语句。

准备语句和绑定语句具有不同的行为,因为准备语句.bind() 返回新的实例绑定语句,而绑定语句.bind() 返回自己。

这是多线程环境中的重要细节。调用方法。共享边界上的bind()语句结果危险

// shared instance BoundStatement on few threads 
BoundStatement bs1 = 
// bs2 is the same as bs1
// param1, param2, ... are different for every thread
BoundStatement bs2 = bs1.bind(param1, param2, ...);
// add some time to wait so other thread can modify your params
// Thread.sleep(RandomUtils.nextInt(100));        
// result2 sometimes may be with incorrect result 
results2 = session.execute(bs2); 

当您在PreparedStatement上调用bind时,您将获得对象的不同实例,并且它是线程安全的。(在与上述相同的场景中)

PreparedStatement ps1 = 
BoundStatement bs2 = ps1.bind(param1, param2, ...);
results2 = session.execute(bs2); 
段溪叠
2023-03-14

没有优势:BoundStatement只不过是一个变量有界的PreparedStatement。实际上,PreparedStatements的< code>bind()方法返回一个BoundStatement。

 类似资料:
  • 我有一个cql查询,我想执行。cql字符串如下所示: 我的应用程序在数据税驱动程序之上有两层抽象。在一个层中,我想绑定前两个参数,而在另一层中我想绑定最后一个参数。问题是,如果我绑定前两个参数,我会得到一个无法绑定另一个参数的BoundStatement。我错过了什么吗?能做到吗? 我们使用的是datastax驱动程序版本2.0.3。 谢谢,安纳托利。

  • 使用卡桑德拉准备语句时,将字段与静态值绑定是否有任何好处? 例如,即使我总是使用静态值“Active”,status是否也应该有一个bind变量?如果是,为什么? 更新用户设置status='Active',其中user_id=:userid

  • 我正在使用我生成的一个准备好的语句,但在Java抛出的语句上出现了语法错误。然而,当我将PS的toString复制并粘贴到数据库的phpmyadmin中时,它的执行完美无缺。有什么想法会出错吗,我很难理解? 编辑:更改为PS.ExecuteUpdate(查询);还是不起作用。

  • 我在使用DataStax php驱动程序1.0.0-rc和Cassandra 2.2.3的Prepared Statements时遇到了一个奇怪的错误。我在这一行遇到了一条异常: 我看到了这个错误: 下面是用于与Cassandra通信的类存根: 如果我将Simple Statemetn与标准select查询一起使用,它会起作用。 有什么建议吗?

  • 问题内容: 我只是想知道是否有一种方法可以在MySQL中使用某种形式的预处理语句,这样就不必逃避所有输入,也不必将所有文件从MySQL切换到MySQLi。我真的不相信转义功能,因此,如果在常规MySQL中有任何可行的替代方法,那就太好了。 问题答案: 使用PDO(PHP数据对象)连接到MySQL数据库。此方法将确保所有数据库输入将始终被视为文本字符串,并且您将不必进行任何手动转义。 结合正确使用h

  • 我在调试中不断看到这个错误。登录cassandra, 在这个里面 因此,根据Cassandra中的这个标签,https://github . com/krasserm/akka-persistence-Cassandra/issues/33,我看到这是由于输入有效负载大小的增加,所以我将cassandra.yml中的< code > commit log _ segment _ size _ in