How to do a batch insert in MySQL

后端 未结 5 1766
离开以前
离开以前 2020-11-22 10:29

I have 1-many number of records that need to be entered into a table. What is the best way to do this in a query? Should I just make a loop and insert one record per itera

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 11:17

    Most of the time, you are not working in a MySQL client and you should batch inserts together using the appropriate API.

    E.g. in JDBC:

    connection con.setAutoCommit(false); 
    PreparedStatement prepStmt = con.prepareStatement("UPDATE DEPT SET MGRNO=? WHERE DEPTNO=?");
    prepStmt.setString(1,mgrnum1);                 
    prepStmt.setString(2,deptnum1);
    prepStmt.addBatch();
    
    prepStmt.setString(1,mgrnum2);                        
    prepStmt.setString(2,deptnum2);
    prepStmt.addBatch();
    
    int [] numUpdates=prepStmt.executeBatch();
    

    http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/ad/tjvbtupd.htm

提交回复
热议问题