【问题标题】:Unknown column in 'field list' MySQLSyntaxErrorException“字段列表”中的未知列 MySQLSyntaxErrorException
【发布时间】:2018-03-28 18:33:09
【问题描述】:

我正在尝试填充远程数据库,但我目前收到此错误,我不确定如何解决此问题。该错误是指我试图放入表中的第一个值。我处理了 no eventID 不会传递到 eventID 表的事件。

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '0654fac7' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

这是我的代码:

protected void loadToDatabase() {
    String dbURL = "jdbc:mysql://remotehost/database";
    String usr = "username";
    String password = "";
    try {
        Connection Conn = DriverManager.getConnection(dbURL, usr, password);
        Statement stmt = Conn.createStatement();
        for (String i : eventIDs) {
            String sql = "INSERT INTO eventID VALUES (" + i + ");";
            stmt.executeUpdate(sql);
        }
        System.out.println("Insert complete");
    } catch (Exception e ) {
        e.printStackTrace();
    }
}

这是我试图传入数据库的数据:

Test Case 0: 0654fac7-7aa8-4c55-bdaf-77b39692aa3f-1413318357238
Test Case 1: f9914577-c60d-49ae-bec6-4db0463676fd-1413318358598
Test Case 2: 6f1b6e4e-9d51-411b-8248-2cd9e1ef2712-1413318360005
Test Case 3: 330860b3-f22b-4b84-9837-bb949c2659ca-1413318361411
Test Case 4: no eventID
Test Case 5: e09f77f3-d816-49ab-90cd-4df56d8c8ef2-1413318366577
Test Case 6: f580e84e-7a3d-4693-bbea-454c5d699070-1413318367992
Test Case 7: no eventID

我的数据库只包含一列。

mysql> show tables in eventID;
+-------------------+
| Tables_in_eventID |
+-------------------+
| eventID           |
+-------------------+

【问题讨论】:

    标签: java mysql jdbc


    【解决方案1】:

    所以我犯的错误就在这行代码

    String sql = "INSERT INTO eventID VALUES (" + i + ");";
    

    正确的功能代码,因为我传递的是一个字符串:

    String sql = "INSERT INTO eventID VALUES (" + "\"" + i + "\"" + ");";
    

    我需要引号!

    【讨论】:

    • PreparedStatement 可以避免您的问题,速度更快,并且可以防止 sql 注入。
    • 像魅力一样工作感谢 String command = "INSERT INTO products (ProductName, UnitPrice) VALUES ("+"\""+name+"\""+","+" \"" +value+"\""+")";
    【解决方案2】:

    我有一个小代码,分享一下。

    这是我的示例表 ++++++++++ 加纳德罗斯 ++++++++++ 鳕鱼 加纳德罗 猿加纳德罗 ++++++++++

            System.out.print("Ingrese codigo del Ganadero : ");
            CodigoGanadero= scn.nextLine();
    
            System.out.print("Ingrese Nombre del Ganadero : ");
            NombreGanadero=scn.nextLine();
    
            System.out.print("Ingrese Apellido del Ganadero : ");
            ApellidoGanadero=scn.nextLine();
            int rs2=0;
            rs2 = s2.executeUpdate ("insert into ganaderos (CodGanadero,NomGanadero,ApeGanadero) "
                    + "values ("+"\""+CodigoGanadero+"\""+","+"\""+NombreGanadero+"\""+","+"\""+ApellidoGanadero+"\""+")");
    

    【讨论】:

    • 是一个3阵营,插入一个表mysql。我希望,对某人有所帮助。
    猜你喜欢
    • 2011-07-27
    • 1970-01-01
    • 2013-02-27
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 2014-03-19
    • 2014-07-19
    相关资源
    最近更新 更多