我在cassandra表中插入一组数据。但是我在那里得到了不匹配的列名/值。我发布了代码以及表结构
public static void aMethod2FromData(DetailsVO detailsVO) {
System.out.println(detailsVO.getA_no());
StringBuilder sb = new StringBuilder();
int num = (int) (Math.random() * 8847);
int radar = (int) (Math.random() * 847);
sb.append(
"INSERT INTO CALL_RATIO_ANALYSYS (A_NO,JAN,FEB,MAR,APR,MAY,JUNE,JULY,AUG,SEP,OCT,NOV,DEC,RADAR_VOICE,RADAR_SMS,RADAR_MMS,RADAR_GPRS,RADAR_OTHER,MMS_INCOMING,MMS_OUTGOING,VOICE_INCOING,VOICE_OUTGOING,SMS_INCOMING,SMS_OUTGOING,GPRS_SOCIAL,GPRS_MAIL,GPRS_GOOGLE,GPRS_ENTERTAINMENT,GPRS_BUSINESS)")
.append("VALUES(");
sb.append("'").append(detailsVO.getA_no()).append("',");
//calculation for Jan to dec
sb.append("'").append(num).append("',");
sb.append("'").append(num).append("',");
sb.append("'").append(num).append("',");
sb.append("'").append(num).append("',");
sb.append("'").append(num).append("',");
sb.append("'").append(num).append("',");
sb.append("'").append(num).append("',");
sb.append("'").append(num).append("',");
sb.append("'").append(num).append("',");
sb.append("'").append(num).append("',");
sb.append("'").append(num).append("',");
sb.append("'").append(num).append("',");
//end here
//start calculation of radar
sb.append("'").append(radar).append("',");
sb.append("'").append(radar).append("',");
sb.append("'").append(radar).append("',");
sb.append("'").append(radar).append("',");
sb.append("'").append(radar).append("',");
//ends here
//mms incoming
sb.append("'").append(radar).append("',");
sb.append("'").append(radar).append("',");
//ends here
//voice incoming outgoing
sb.append("'").append(radar).append("',");
sb.append("'").append(radar).append("',");
//end here
//sms incoming outgoing
sb.append("'").append(radar).append("',");
sb.append("'").append(radar).append("',");
//ends here
//gprs calculation
sb.append("'").append(radar).append("',");
sb.append("'").append(radar).append("',");
sb.append("'").append(radar).append("',");
sb.append("'").append(radar).append("')");
//ends here
System.out.println(sb.toString());
String cql = sb.toString();
session.execute(cql);
}
这是表结构
CREATE TABLE CALL_RATIO_ANALYSYS(
A_NO TEXT,
JAN TEXT,
FEB TEXT,
MAR TEXT,
APR TEXT,
MAY TEXT,
JUNE TEXT,
JULY TEXT,
AUG TEXT,
SEP TEXT,
OCT TEXT,
NOV TEXT,
DEC TEXT,
RADAR_VOICE TEXT,
RADAR_SMS TEXT,
RADAR_MMS TEXT,
RADAR_GPRS TEXT,
RADAR_OTHER TEXT,
MMS_INCOMING TEXT,
MMS_OUTGOING TEXT,
VOICE_INCOMING TEXT,
VOICE_OUTGOING TEXT,
SMS_INCOMING TEXT,
SMS_OUTGOING TEXT,
GPRS_SOCIAL TEXT,
GPRS_MAIL TEXT,
GPRS_GOOGLE TEXT,
GPRS_ENTERTAINING TEXT,
GPRS_BUSINESS TEXT,
PRIMARY KEY(A_NO)
);
我无法理解列与数据不匹配的地方请帮忙
答案 0 :(得分:5)
我建议你使用准备好的陈述。它不是您问题的直接解决方案,但您不应该像这样构建CQL语句。这是低效的,代码变得一团糟。可能有一个逗号或其他缺失的东西,但是尝试检查代码的语法比使用预准备语句重写它需要更多的时间:
PreparedStatement statement = session.prepare("INSERT INTO CALL_RATIO_ANALYSYS (A_NO,JAN,FEB,MAR,APR,MAY,JUNE,JULY,AUG,SEP,OCT,NOV,DEC,RADAR_VOICE,RADAR_SMS,RADAR_MMS,RADAR_GPRS,RADAR_OTHER,MMS_INCOMING,MMS_OUTGOING,VOICE_INCOING,VOICE_OUTGOING,SMS_INCOMING,SMS_OUTGOING,GPRS_SOCIAL,GPRS_MAIL,GPRS_GOOGLE,GPRS_ENTERTAINMENT,GPRS_BUSINESS) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
BoundStatement boundStatement = statement.bind(detailsVO.getA_no(), num, num, num, num, num, num, num, num, num, num, num, num, radar, radar, radar, radar, radar, radar, radar, radar, radar, radar, radar, radar, radar, radar, radar);
session.execute(boundStatement);
保留准备好的陈述并重新使用它。
编辑所以这里真正的问题是你错过了最后一个参数的值。如果错误消息显示您的值数量错误,则计算值时会有所帮助。