简体   繁体   English

将记录插入相同MySQL模式的不同表中

[英]Inserting records into different tables of the same MySQL Schema

So I can successfully insert information into one table called "parts" using the code: 因此,我可以使用以下代码将信息成功插入一个称为“ parts”的表中:

String sql1 = "INSERT INTO parts(PART1, PART2, PART3, PART4, PART5)" + "VALUES(?,?,?,?,?)";

    PreparedStatement prepareStatement = conn.prepareStatement(sql1);

    prepareStatement.setString(1,strarr.StringArray[0][0]);
    prepareStatement.setString(2,strarr.StringArray[0][1]);
    prepareStatement.setString(3,strarr.StringArray[0][2]);
    prepareStatement.setString(4,strarr.StringArray[0][3]);
    prepareStatement.setString(5,strarr.StringArray[0][4]);

    prepareStatement.execute();

However when I then try to insert some data into a different table, called subsubparts of the same schema in the exact same why it throws up an error: 但是,当我随后尝试将一些数据插入到另一个表中时, subsubparts完全相同地被称为同一模式的子子部分,为什么会引发错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '2.1, 2.2, 2.3, 2.4, 2.5)VALUES('$$$Epidemiology of the Disease\\r\\nLiterature sea' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的'2.1、2.2、2.3、2.4、2.5)VALUES('$$$ Epidemiology of Disease \\ r \\ nLiterature sea'附近使用

and this is on the line which executes the preparedStatement. 这是执行prepareStatement的行。

Below is the code of the second way which fails (if this helps): 下面是第二种失败的代码(如果有帮助):

String sql2 = "INSERT INTO subsubparts(2.1, 2.2, 2.3, 2.4, 2.5)" + "VALUES(?,?,?,?,?)";
    PreparedStatement preparedStatement0 = conn.prepareStatement(sql2);

    preparedStatement0.setString(1, strarr.StringArray[2][0]);
    preparedStatement0.setString(2, strarr.StringArray[2][1]);
    preparedStatement0.setString(3, strarr.StringArray[2][2]);
    preparedStatement0.setString(4, strarr.StringArray[2][3]);
    preparedStatement0.setString(5, strarr.StringArray[2][4]);

    preparedStatement0.execute();

Whats even more confusing is that if throws a com.mysql.jdbc exception when I have imported import java.sql.SQLException . 更令人困惑的是,如果在导入import java.sql.SQLException时抛出com.mysql.jdbc异常。

Any help would be great please!! 任何帮助将是巨大的!

You second query looks wrong: 您的第二个查询看起来不对:

INSERT INTO subsubparts(2.1, 2.2, 2.3, 2.4, 2.5) VALUES(?,?,?,?,?)

Should be changed to: 应更改为:

INSERT 
INTO subsubparts(FieldName1,FieldName2, FieldName3, FieldName4, FieldName5) 
VALUES(?,?,?,?,?)

Or if 2.1 is the name of the column then the name must be quoted eg "2.1" 或者,如果2.1是该列的名称,则该名称必须用引号引起,例如“ 2.1”

If your Columns names are numbers like 2.1 then wrap it with quotes like, 如果您的Columns名称是像2.1这样的数字,那么请用引号将它括起来,

String sql2 = "INSERT INTO subsubparts(`2.1`, `2.2`, `2.3`, `2.4`, `2.5`)" 
              + "VALUES(?,?,?,?,?)";

The major difference I see between the two queries is query two has periods in the column name. 我看到的两个查询之间的主要区别是查询两个列名称中都有句点。 MySQL won't recognize this as-is. MySQL无法按原样识别。 Instead, you need to quote each of the column names with backticks: 相反,您需要用反引号将每个列名称引起来:

(`2.1`, `2.2`, `2.3`, `2.4`, `2.5`)

It may also be possible to use double-quotes. 也可以使用双引号。 See the MySQL documentation on identifiers for more detail. 有关标识符的更多信息,请参见MySQL文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM