java - How do I within SQLite, return a row based on 3 values rather than just one? (Already done code for 1) -
within sqlite, can search table particular row contains 1 values instance player name want return row based on 3 values instance player name, team name , favorite color etc.
how alter below code this?
code searching based upon 1 value below:
public backupdatastore getentry(string value, string columnname) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table_name, new string[] { key_id, key_team_name, key_ref_name, key_date, key_player_1, key_player_2, key_player_3, key_player_4, key_player_5, key_player_6, key_player_7 , key_player_8}, columnname + "=?", new string[] { value }, null, null, null, null); if (cursor != null) { boolean move = cursor.movetofirst(); //false value means query returned 0 results if(!move) { return backupdatastore.kemptydata; } } backupdatastore entry = new backupdatastore(integer.parseint(cursor.getstring(0)), cursor.getstring(1), cursor.getstring(2), cursor.getstring(3), cursor.getstring(4), cursor.getstring(5), cursor.getstring(6), cursor.getstring(7), cursor.getstring(8), cursor.getstring(9), cursor.getstring(10), cursor.getstring(11)); // return entry return entry; }
you need change you're passing 3rd , 4th parameters - column names , values passed clause.
for example, filter across 3 columns you'd do:
cursor cursor = db.query(table_name, new string[] { key_id, key_team_name, key_ref_name, key_date, key_player_1, key_player_2, key_player_3, key_player_4, key_player_5, key_player_6, key_player_7 , key_player_8}, column1name + "=? , " + column2name + "=? , " + column3name + "=? ", new string[] { value1, value2, value3 }, null, null, null, null);
this build expression similar select player1, player2, ... teamname firstname = 'x' , lastname = 'y' , teamname = 'z'
you build dynamically using variable length args, or passing in string arrays, rather method defined take 3 columns & 3 values parameters, example:
public backupdatastore getentry(string[] values, string[] columns) { if (values.length != columns.length) { throw new illegalargumentexception("number of columns not match number of values provided."); } stringbuilder sb = new stringbuilder(); (int = 0; < columns.length; i++) { sb.append(columns[i]); sb.append("=?"); if (i != columns.length - 1) { sb.append(" , "); } } string wherecolumns = sb.tostring(); cursor cursor = db.query(table_name, new string[] { key_id, key_team_name, key_ref_name, key_date, key_player_1, key_player_2, key_player_3, key_player_4, key_player_5, key_player_6, key_player_7 , key_player_8}, wherecolumns, values, null, null, null, null); ...
Comments
Post a Comment