我怎么知道该记录是重复的或已成功插入? [英] How will i know that record was duplicate or it was inserted successfully?

查看:74
本文介绍了我怎么知道该记录是重复的或已成功插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的CQL表:

CREATE TABLE user_login (
    userName varchar PRIMARY KEY,
    userId uuid,
    fullName varchar,
    password text,
    blocked boolean
);

我有此datastax Java驱动程序代码

I have this datastax java driver code

PreparedStatement prepareStmt= instances.getCqlSession().prepare("INSERT INTO "+ AppConstants.KEYSPACE+".user_info(userId, userName, fullName, bizzCateg, userType, blocked) VALUES(?, ?, ?, ?, ?, ?);");

batch.add(prepareStmt.bind(userId, userData.getEmail(), userData.getName(), userData.getBizzCategory(), userData.getUserType(), false));


PreparedStatement pstmtUserLogin = instances.getCqlSession().prepare("INSERT INTO "+ AppConstants.KEYSPACE+".user_login(userName, userId, fullName, password, blocked) VALUES(?, ?, ?, ?, ?) IF NOT EXIST");

batch.add(pstmtUserLogin.bind(userData.getEmail(), userId, userData.getName(), passwordEncoder.encode(userData.getPwd()), false));
            instances.getCqlSession().executeAsync(batch);

这里的问题是,如果删除如果不存在都可以正常工作,但是如果放回去,根本不要在表中插入记录,也不会抛出任何错误。

Here the problem is that if I remove IF NOT EXIST all work fine but if put it back it simply do not insert records in table nor throw any error.

所以我怎么知道我要插入重复的 userName

So how will i know that i am inserting duplicate userName ?

我正在使用cassandra 2.0.1

I am using cassandra 2.0.1

推荐答案

使用 INSERT ...如果不存在,则可以使用 ResultSet#wasApplied()检查结果:

Use INSERT... IF NOT EXISTS, then you can use ResultSet#wasApplied() to check the outcome:

ResultSet rs = session.execute("insert into user (name) values ('foo') if not exists");
System.out.println(rs.wasApplied());

注意:


  • 此CQL查询是轻量级事务,具有性能影响。有关更多信息,请参见本文

  • 您的示例中只有一个语句,不需要批处理

这篇关于我怎么知道该记录是重复的或已成功插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆