如何在mysql中插入新行b / w现有行?

时间:2015-03-25 07:55:03

标签: mysql sql

我正在尝试插入新行b / w现有行,但是收到错误 我的数据库

RoutePoint      lat              lon                   type
    1      12.9061170343075     77.5724397120948        s 
    2      12.9209711117095     77.5601544553814        v
    3      12.892986142953      77.4662127668475        v
    4      12.9215410271329     77.5607713634547        s

查询 -

String query1 = "UPDATE mytable SET RoutePoint = 
    RoutePoint + 1 where id > 3 "+"insert into rp(lat, lon,type) values (?,?,?)"; 

pstmt = con.prepareStatement(query1); // create a statement
pstmt.setDouble(1, 12.34234234);
pstmt.setDouble(2, 77.423423423); // set input parameter 2
pstmt.setString(3, s); // set input parameter 3
pstmt.executeUpdate(); // execute insert statement

但我收到错误 -

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 
insert into rp(lat, lon,type) values (12.921343123349597,77.5601659236431,'s')
at line 1

请帮忙。

2 个答案:

答案 0 :(得分:0)

query1中你有一个UPDATE和一个INSERT,这是两个语句。您应该使用一些分隔符char将它们分开,或者一个接一个地执行它们(使用两个PreparedStatement s)。

答案 1 :(得分:0)

为什么同时使用UpdateInsert命令?将命令分成两个块:

更新

String query1 = "UPDATE mytable SET RoutePoint = 
    RoutePoint + 1 where id > 3; 

pstmt = con.prepareStatement(query1); // create a statement
pstmt.executeUpdate(); // execute update statement

插入:

query1 = "insert into rp(lat, lon,type) values (?,?,?)"
pstmt = con.prepareStatement(query1); // create a statement
pstmt.setDouble(1, 12.34234234);
pstmt.setDouble(2, 77.423423423); // set input parameter 2
pstmt.setString(3, s); // set input parameter 3
pstmt.executeUpdate(); // execute update statement