Using setDate in PreparedStatement

❐ Using java.sql.Date If your table has a column of type DATE: java.lang.String The method java.sql.Date.valueOf(java.lang.String) received a string representing a date in the format yyyy-[m]m-[d]d. e.g.: ps.setDate(2, java.sql.Date.valueOf(“2013-09-04”)); java.util.Date Suppose you have a variable endDate of type java.util.Date, you make the conversion thus: ps.setDate(2, new java.sql.Date(endDate.getTime()); Current If you want to insert the current … Read more

PreparedStatement with list of parameters in a IN clause [duplicate]

What I do is to add a “?” for each possible value. var stmt = String.format(“select * from test where field in (%s)”, values.stream() .map(v -> “?”) .collect(Collectors.joining(“, “))); Alternative using StringBuilder (which was the original answer 10+ years ago) List values = … StringBuilder builder = new StringBuilder(); for( int i = 0 ; … Read more

How do I use prepared statements in SQlite in Android?

For prepared SQLite statements in Android there is SQLiteStatement. Prepared statements help you speed up performance (especially for statements that need to be executed multiple times) and also help avoid against injection attacks. See this article for a general discussion on prepared statements. SQLiteStatement is meant to be used with SQL statements that do not … Read more

Use an array in a mysqli prepared statement: `WHERE .. IN(..)` query [duplicate]

you could do it this way: $ids = array(1,5,18,25); // creates a string containing ?,?,? $clause = implode(‘,’, array_fill(0, count($ids), ‘?’)); $stmt = $mysqli->prepare(‘SELECT * FROM somewhere WHERE `id` IN (‘ . $clause . ‘) ORDER BY `name`;’); call_user_func_array(array($stmt, ‘bind_param’), $ids); $stmt->execute(); // loop through results Using this you’re calling bind_param for each id and … Read more

Can I bind an array to an IN() condition in a PDO query?

You’ll have to construct the query-string. <?php $ids = array(1, 2, 3, 7, 8, 9); $inQuery = implode(‘,’, array_fill(0, count($ids), ‘?’)); $db = new PDO(…); $stmt = $db->prepare( ‘SELECT * FROM table WHERE id IN(‘ . $inQuery . ‘)’ ); // bindvalue is 1-indexed, so $k+1 foreach ($ids as $k => $id) $stmt->bindValue(($k+1), $id); $stmt->execute(); … Read more