在MySQL中更新列

huangapple go评论78阅读模式
英文:

Updating a column in MySQL

问题

我只是在学习,我不明白如何更新表中的单独列,我使用MySQL。表名叫做card,我想更新pincode列。

java.sql.SQLSyntaxErrorException: SQL语法错误;请检查与您的MySQL服务器版本对应的手册,以查找正确的语法使用方式,附近的错误位置在'UPDATE COLUMN pincode = strPincodeNew'。

        try {
            Connection c = Database.connection();

            Statement stmt11 = c.createStatement();


            String sql12="ALTER TABLE card UPDATE COLUMN pincode = strPincodeNew";

            stmt11.executeUpdate(sql12);
                                    System.out.println("\n\n==== 输入新的PIN码 ====\n");
                                    pincodeNew = scanner.nextInt();
                                }
                                String strPincodeNew = String.valueOf(pincodeNew);
                                operation.newPC1(strPincodeNew, cardNumber);
                                System.out.println("PIN码成功更改");

重写不同的命令

英文:

I'm just learning and I don't understand how to update a separate column in the table, I use MySQL. The table is called card, I want to update the pincode column.
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE COLUMN pincode = strPincodeNew' at line 1

 public void newPC1(String strPincodeNew, String cardNumber) {
        try {
            Connection c = Database.connection();

            Statement stmt11 = c.createStatement();


            String sql12="ALTER TABLE card UPDATE COLUMN pincode = strPincodeNew";

            stmt11.executeUpdate(sql12);


while (pincodeNew>=....) {
                                    System.out.println("\n\n==== Enter a new PIN code ====\n");
                                    pincodeNew = scanner.nextInt();
                                }
                                String strPincodeNew = String.valueOf(pincodeNew);
                                operation.newPC1(strPincodeNew, cardNumber);
                                System.out.println("PIN code successfully changed");

Rewrote different commands

答案1

得分: 2

请尝试以下操作。您需要在必要时更正卡号列名称:

    public void newPC1(String strPincodeNew, String cardNumber) throws SQLException {
        try (Connection c = Database.connection();
            PreparedStatement ps = c.prepareStatement("UPDATE card SET pincode = ? WHERE card_number = ?")) {
            ps.setString(1, strPincodeNew);
            ps.setString(2, cardNumber);
            ps.executeUpdate();
        }
    }

如果您有任何其他问题,请随时提出。

英文:

Try the following. You need to correct the card number column name where necessary:

    public void newPC1(String strPincodeNew, String cardNumber) throws SQLException {
        try (Connection c = Database.connection();
            PreparedStatement ps = c.prepareStatement("UPDATE card SET pincode = ? WHERE card_number = ?")) {
            ps.setString(1, strPincodeNew);
            ps.setString(2, cardNumber);
            ps.executeUpdate();
        }
    }

答案2

得分: 0

你想要的是更改一行中的一个 。因此,您需要同时选择两者。

可能您想要执行类似以下的操作:

UPDATE card SET pincode = '1234' WHERE card_id = 'abcdef';

在更新之前,您需要通过 INSERT 语句在表中拥有一行。

英文:

What you want is to change a column in a row. So you need to select both.

Likely you want to do something like

UPDATE card SET pincode = '1234' WHERE card_id = 'abcdef';

You need to have one row (via an INSERT statement) in the table, before you can update something.

huangapple
  • 本文由 发表于 2023年6月22日 00:37:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定