cassandra - Does anyone have working syntax for CQL UPDATE with preparedStatement() they can share with me? -


when insert things go well:

string inserttext = "insert alpha_screen (alpha, screen) values (?,?)"; preparedstatement preparedstatement = insertwordcount.preparetext(inserttext); boundstatement boundstatement = preparedstatement.bind(keyvalue, newword);  //insert positions getsession().execute(boundstatement);  cqlsh> select * rant.alpha_screen; ==> inserts done expected.  alpha  | screen -------+--------------      |   ['aboard'] c      | ['checking'] p      |     ['pull'] r      |  ['rotting'] t      |     ['time'] 

not preparetext update:

string inserttext = "update alpha_screen set screen = screen + ['newword'] alpha = 'keyvalue' values (?, ?)"; preparedstatement preparedstatement = insertwordcount.preparetext(inserttext); boundstatement boundstatement = preparedstatement.bind(newword, keyvalue);  // update positions getsession().execute(boundstatement);   com.datastax.driver.core.exceptions.syntaxerror: line 1:79 missing eof @ 'values' (...] alpha = 'keyvalue' [values] (...) @ com.datastax.driver.core.exceptions.syntaxerror.copy(syntaxerror.java:35) @ com.datastax.driver.core.defaultresultsetfuture.extractcausefromexecutionexception(defaultresultsetfuture.java:289) @ com.datastax.driver.core.abstractsession.prepare(abstractsession.java:79) @ playlist.model.insertwordcount.preparetext(insertwordcount.java:13) @ playlist.model.countdao.screenword(countdao.java:99) 

it looks should work example in documentation:

append element list switching order of new element data , list name in update command.

update users   set top_places = top_places + [ 'mordor' ] user_id = 'frodo'; 

in fact works fine without values:

string inserttext = "update alpha_screen set screen = screen + ['twoword'] alpha = 'keyvalue' "; preparedstatement preparedstatement = insertwordcount.preparetext(inserttext); boundstatement boundstatement = preparedstatement.bind();  // no values getsession().execute(boundstatement);   cqlsh> select * rant.alpha_screen;  alpha     | screen ----------+-----------------------------------         |                        ['aboard'] c         |                      ['checking'] p         |                          ['pull'] keyvalue  | ['newword', 'oneword', 'twoword'] r         |                       ['rotting'] t         |                          ['time'] 

i'm not sure documentation looking @ (next time, please provide link). also, helps indicate language using, driver , version. reading through error messages, able ascertain using datastax java driver, still unsure version using (i'm assuming 2.1). in case values not valid clause update statement.

this example shows methond inserts list via prepared statement datastax java 2.1 driver:

private static void insertalphascreen(session _session, string _alpha, list<string> _screen) {     preparedstatement statement = _session.prepare("update stackoverflow2.alpha_screen " +         "set screen=? alpha=?");      boundstatement boundstatement = statement.bind(_screen,_alpha);     _session.execute(boundstatement); } 

when prepare list , call main method:

    list<string> screen = new arraylist<string>();     screen.add("aboard");     insertalphascreen(session, "a", screen); 

this result in cql table:

 alpha    | screen ----------+-------------         |  ['aboard'] 

unfortunately, if want add element existing list collection, there isn't way it. there 2 ways accomplish this:

  1. read collection out first, , write entire collection via prepared statement. of course, means have read-in collection, add value, , write cassandra (possibly using above method).
  2. the other option create update statement parsing string together. of course doesn't protect sql (cql) injection attack.

for second option, method looks similar how built cql update statement above:

private static void updatealphascreen(session _session, string _alpha, string _screen) {     preparedstatement statement = _session.prepare("update stackoverflow2.alpha_screen " +         "set screen=screen+['" + _screen + "'] alpha=?");      boundstatement boundstatement = statement.bind(_alpha);     _session.execute(boundstatement); } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -