英文:
MySQL checking if something is already in a column
问题
以下是翻译好的内容:
我对MySQL还不太了解,不太清楚如何检查在“friendCode”列中是否已经存在相同的值,如果是这样的话,还需要检查在同一行的“watched”列中是否存在值“0”。我已经成功地检查了代码是否存在于某一行中。我只是不知道如何查找第二个条件。
这是表格的外观:
这是我已经完成的部分:
public static boolean checkRow(String query) {
boolean ret = false;
try (Connection con = DriverManager.getConnection(url, user, password)) {
Statement stmt = con.createStatement();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs.next() && !rs.getString("watched").equals(0)) {
ret = false;
} else {
ret = true;
}
rs.close();
stmt.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return ret;
}
英文:
I am new to MySQL and don't really know how to check if in the column "friendCode" the same value already exist and if that's the case it should look if in the same row in the "watched" column the value "0" is. I have already managed to check whether the code exists in a row. I just don't know how to look for the second condition.
Here is a picture how the table looks like:
And here that what I already managed to do:
public static boolean checkRow(String query) {
boolean ret = false;
try (Connection con = DriverManager.getConnection(url, user, password)) {
Statement stmt = con.createStatement();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs.next() && !rs.getString("watched").equals(0)) {
ret = false;
} else {
ret = true;
}
rs.close();
stmt.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return ret;
}
答案1
得分: 1
-
我认为你在这个程序中没有编写任何选择查询。如果你是从主方法调用的,只需检查并尝试以下步骤。
-
在给定的表中,"watched" 列具有整数数据类型。
-
你在进行字符串类型的比较。
-
试试我的代码,它能正常工作。如果你有任何疑虑,请回复这条评论。
public static boolean checkRow(String query) {
boolean ret = false;
try (Connection con = DriverManager.getConnection(url, user, password)) {
Statement stmt = con.createStatement();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs.next() && rs.getString("watched")!=0) {
ret = false;
} else {
ret = true;
}
rs.close();
stmt.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return ret;
}
英文:
-
I think you are not written any select query in this program. If you are calling from the main method just check and try the below steps.
-
In Given Table watched column have integer DataType data.
-
Your Compare to String type.
-
Try it my code it's working if you have any concern revert back to this comment.
public static boolean checkRow(String query) { boolean ret = false; try (Connection con = DriverManager.getConnection(url, user, password)) { Statement stmt = con.createStatement(); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); if (rs.next() && rs.getString("watched")!=0) { ret = false; } else { ret = true; } rs.close(); stmt.close(); } catch (SQLException e) { System.out.println(e.getMessage()); } return ret; }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论