java “字段列表”中的未知列 MySQLSyntaxErrorException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26369819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unknown column in 'field list' MySQLSyntaxErrorException
提问by Liondancer
I am trying to populate a remote database but I am currently getting this error and I'm not sure how to fix this. The error refers to the first value I am trying to put into my table. I handled the events where no eventID
would not be passed into the eventID table.
我正在尝试填充远程数据库,但我目前收到此错误,我不确定如何解决此问题。该错误是指我尝试放入表中的第一个值。我处理了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)
This is my code:
这是我的代码:
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();
}
}
This is the data I am trying to pass into the database:
这是我试图传递到数据库中的数据:
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
My database simply consists of one column.
我的数据库只包含一列。
mysql> show tables in eventID;
+-------------------+
| Tables_in_eventID |
+-------------------+
| eventID |
+-------------------+
回答by Liondancer
So the error I made was on this line of code
所以我犯的错误是在这行代码上
String sql = "INSERT INTO eventID VALUES (" + i + ");";
The correct functional code, because I am passing a string is:
正确的功能代码,因为我传递的是一个字符串:
String sql = "INSERT INTO eventID VALUES (" + "\"" + i + "\"" + ");";
I need the quotation marks!
我需要引号!
回答by Alvaro
I have a litle code, to share.
我有一个小代码,分享。
This is my example table ++++++++++ ganaderos ++++++++++ CodGanadero NomGanadero ApeGanadero ++++++++++
这是我的示例表 ++++++++++ ganaderos ++++++++++ CodGanadero NomGanadero ApeGanadero ++++++++++
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+"\""+")");