How can I check if the given request returned something? ResultSet rs = stmt.executeQuery ("SELECT * FROM USERS WHERE NICK = '" + nick + "';"); (I want to check if there is an entry with this nickname in the table)
4 answers
Can so
int q=0; while (rset.next()) { q++; } or so
int count = -1; if ( resultSet.last() ) { count = resultSet.getRow(); } - It is excessive to make a request for all records with such a login to count the number of the same logins, isn’t it so? - Sckoriy
- I agree, but the question was how to check whether something returned the request ... For what purpose the author needs information about the existing user from the question is not entirely clear, you may need additional information if the user is found. - Z.John pm
First, use parameterized queries.
Secondly, you can use isBeforeFirst () or first () methods:
if (!resultSet.isBeforeFirst()) // no rows if (!resultSet.first()) // no rows Thirdly, if you really only need to check if the given nickname exists in the table, then you do not need to extrude all the information about it: select * , - just one field is enough: for example, select id . It would be even better to use a query that does not return rows from the database at all, but only a Boolean value: is there such a user or not. Use exists or count in the sql query for this.
I wanted to give references to the so-answers on the third point, but there are often gluing strings for sql queries, pulling the entire table to the client and similar horror. And these are the negative answers! It is clear why so many bugs and brakes in the current software ... Hmm, my faith in programming has been greatly shaken.
If you want to check if a record exists in the table in the most convenient and simple way is to use EXISTS . Approximate solution:
private static final int FIRST_ROW = 1; ... public boolean isUserExist(String userName){ String userQuery = "SELECT EXISTS(SELECT * FROM USERS WHERE NICK = " + userName + " LIMIT 1)"; Cursor cursor = mDatabase.rawQuery(userQuery, null); cursor.moveToFirst(); boolean isRecordExist = cursor.getInt(FIRST_ROW) == 1; cursor.close(); return isRecordExist; } EXISTS returns 1 if a similar nickname already exists and 0 if not. The code may be errors, typed in memory in a notebook.
- There is no android tag in question, and the solution is specific only for the android platform - pavlofff
- @pavlofff, re-read my answer and understand that I focused on using EXISTS, and the code is exemplary and I indicated it. - UjinUkr
Likely, you do registration and do check on employment of login. I checked the condition if there is such a login in the table as follows. It is not redundant, as for me.
public boolean login_is_busy(String login) { try { stat = connection.createStatement(); result = stat.executeQuery("SELECT count(login) FROM `users` WHERE login='"+login+"';"); result.next(); int count = result.getInt(1); if(count == 0) { return false; // Таких логинов нету,значит мы возвращаем false,что значит не занятый } }catch(SQLException ex) { ex.printStackTrace(); } // Записи с таким логином есть,значит true ибо он занят return true; } This query returns a single column with a numeric value (Number of records with the same login name) Next, you can do this thing:
public void method(String login){ if(login_is_busy(login)){ // Говорите,что логин занят } if(!login_is_busy(login)){ // Говорите,что логин свободный } //... Thus, you slightly removed a possible govnokod, if you wrote all the logic in one method.
while(rs.next()), and then call methods of the form rs.getXXX (n), where XXX is the data type in the column, n is the column number — harnish28 February