我正在创建一个结算应用程序。我想在mySQL中同时将相同的数据插入到两个不同的表中。任何人都可以告诉我它应该是什么查询。我的任务是先将数据插入到calculateinfo表中,然后将相同的数据插入salesinfo表,然后从计算表中选择全部。我正在尝试这个,但得到SQL语法错误。 这是我的查询
"Insert into calculationinfo (`date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtotal`) values(?,?,?,?,?,?) INSERT INTO salesinfo (`date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtotal`) SELECT * From calculationinfo"
我使用的是java语言。这是stackprint
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT salesinfo `date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtot' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
at Restaurant.Calculation.item_tblMouseClicked(Calculation.java:323)
at Restaurant.Calculation.access$600(Calculation.java:34)
at Restaurant.Calculation$7.mouseClicked(Calculation.java:259)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6536)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:0)
使用自动关闭资源,例如
try (PreparedStatement stmt = connection.prepareStatement("Insert into calculationinfo (`date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtotal`) values(?,?,?,?,?,?)") {
//Set Data here
stmt.executeUpdate();
}
// stmt is auto closed here, even if SQLException is thrown
try (PreparedStatement stmt = connection.prepareStatement("INSERT INTO salesinfo (`date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtotal`) VALUES (?, ?, ?,?,?,?)");
//set same data above here
stmt.executeUpdate();
}
答案 1 :(得分:0)
您需要使用终止符分隔这两个查询。
"Insert into calculationinfo"+
"(`date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtotal`) "+
"values(?,?,?,?,?,?);"+
"INSERT INTO salesinfo"+
"(`date`,`prod_name`, `prod_amount`,`total_amount`,`user`, `Gtotal`) "+
"values(?,?,?,?,?,?)";
这可以帮助您解决问题。我在上面做的是在以可读模式格式化查询后附加字符串。
注意:同样看起来我已经将值传递给第二个表,就像将值传递给第一个表一样。这也会奏效。 :)
Insert into table1 Select * from table2
会将table1的所有行插入到table2中,这会弄乱你的数据。